package Dist::Zilla::Plugin::Docker::API::Context;
# ABSTRACT: Build context resolver for Docker image builds
our $VERSION = '0.100';
use Moo;
use Types::Standard qw(Str InstanceOf);
use Path::Tiny;

has zilla => (
    is       => 'ro',
    required => 1,
);

has mode => (
    is      => 'ro',
    isa     => Str,
    default => 'build',
);

has file => (
    is      => 'ro',
    default => 'Dockerfile',
);

sub resolve {
    my ($self, %arg) = @_;

    my $build_root = $arg{build_root};
    my $archive = $arg{archive};

    if ($self->mode eq 'build') {
        return $self->_context_build($build_root);
    }
    elsif ($self->mode eq 'source') {
        return $self->_context_source;
    }
    elsif ($self->mode eq 'archive') {
        return $self->_context_archive($archive);
    }
    else {
        die "Unknown context mode: " . $self->mode;
    }
}

sub _context_build {
    my ($self, $build_root) = @_;

    my $root = Path::Tiny->new($build_root // $self->zilla->root);

    my $dockerfile_path = $root->child($self->file);
    unless ($dockerfile_path->exists) {
        die "Dockerfile '" . $self->file . "' not found in build context: " . $root->stringify;
    }

    return {
        type => 'dir',
        path => $root->stringify,
        dockerfile => $self->file,
    };
}

sub _context_source {
    my ($self) = @_;

    my $root = Path::Tiny->new($self->zilla->root);

    my $dockerfile_path = $root->child($self->file);
    unless ($dockerfile_path->exists) {
        die "Dockerfile '" . $self->file . "' not found in source root";
    }

    return {
        type => 'dir',
        path => $root->stringify,
        dockerfile => $self->file,
    };
}

sub _context_archive {
    my ($self, $archive) = @_;

    die "Archive path required for context=archive"
        unless $archive && -f $archive;

    return {
        type => 'archive',
        path => $archive,
        dockerfile => $self->file,
    };
}

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

Dist::Zilla::Plugin::Docker::API::Context - Build context resolver for Docker image builds

=head1 VERSION

version 0.100

=head1 SUPPORT

=head2 Issues

Please report bugs and feature requests on GitHub at
L<https://github.com/Getty/p5-dist-zilla-plugin-docker-api/issues>.

=head1 CONTRIBUTING

Contributions are welcome! Please fork the repository and submit a pull request.

=head1 AUTHOR

Torsten Raudssus <getty@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2026 by Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut
