package Dist::Zilla::Plugin::AutomationPolicy;

use v5.24;

# ABSTRACT: add an automation policy to a distribution

use Moose;
with qw( Dist::Zilla::Role::FileGatherer Dist::Zilla::Role::PrereqSource Dist::Zilla::Role::FilePruner );

use Dist::AutomationPolicy v0.2.0;
use Dist::Zilla::File::InMemory;
use Dist::Zilla::Pragmas;
use MooseX::Types::Moose qw( HashRef );
use MooseX::Types::Perl  qw( StrictVersionStr );

use namespace::autoclean;

use experimental qw( postderef signatures );

our $VERSION = 'v0.1.1';


has version => (
    is      => 'ro',
    isa     => StrictVersionStr,
    default => 'v0.2.0',
);

has _policy_args => (
    is      => 'ro',
    isa     => HashRef,
    default => sub { {} },
);

has _policy => (
    is         => 'ro',
    lazy_build => 1,
    builder    => '_build_policy',
    handles    => [qw/ filename to_json /],
);

sub _build_policy ($self) {
    my $zilla = $self->zilla;
    my %args = $self->_policy_args->%*;
    $args{distribution} ||= join( "-", $zilla->name, $zilla->version );
    return Dist::AutomationPolicy->new(%args);
}


sub mvp_multivalue_args { qw( models ) }

around plugin_from_config => sub( $orig, $class, $name, $args, $section ) {
    my %module_args;

    for my $key ( keys $args->%* ) {
        if ( $key =~ s/^-// ) {
            die "$key cannot be set" if $key eq "_policy_args";
            $module_args{$key} = $args->{"-$key"};
        }
        else {
            $module_args{_policy_args}{$key} = $args->{$key};
        }
    }

    return $class->$orig( $name, \%module_args, $section );
};


sub gather_files($self) {

    $self->add_file(
        Dist::Zilla::File::InMemory->new(
            name            => $self->filename,
            encoded_content => $self->to_json,
        )
    );

    return;
}


sub register_prereqs($self) {
    $self->zilla->register_prereqs( { phase => 'develop' }, "Dist::AutomationPolicy" => $self->version );
    return;
}


sub prune_files($self) {
    my @files    = @{ $self->zilla->files };
    my $filename = $self->filename;
    for my $file (@files) {
        $self->zilla->prune_file($file) if $file->name eq $filename && $file->added_by !~ __PACKAGE__;
    }
}

__PACKAGE__->meta->make_immutable;

__END__

=pod

=encoding UTF-8

=for stopwords LLMs

=head1 NAME

Dist::Zilla::Plugin::AutomationPolicy - add an automation policy to a distribution

=head1 VERSION

version v0.1.1

=head1 SYNOPSIS

In the F<dist.ini>:

    [AutomationPolicy]
    -version    = v0.2.2
    description = The automation policy for Foo-Bar-Baz
    template    = human_supervised
    models      = claude-opus-4-7
    models      = claude-opus-4-8

=head1 DESCRIPTION

This plugin will create a machine-readable F<CPAN-META/automation-policy.json> file in your distribution, using
L<Dist::AutomationPolicy>.

It allows authors to specify the use of automation in generating their modules, and what automated contributions they
will accept.

=head1 OPTIONS

The options correspond to attributes from L<Dist::AutomationPolicy>.
The more important ones are listed here.

=head2 -version

This lets you specify the minimum version of L<Dist::AutomationPolicy> to use.
(Do not confuse this with the C<version> attribute.)

=head2 description

This is an optional short description of the automation policy.

=head2 document

This is an optional name of the human-readable F<AI_POLICY.md> file.

It is I<not> generated by current versions of L<Dist::AutomationPolicy>.
You have to create it yourself, and ensure it is consistent with this file.

=head2 template

This is an attribute for specifying common use-cases:

=over

=item no_automation

No code is generated beyond the toolchain.

No automated contributions are accepted.

No automated actions beyond basic scripting that might comment on issues or pull requests.

=item issues_only

No code is generated beyond the toolchain.

Automated contributions or actions may submit comments and issues.

=item human_supervised

Code may be generated by AI or LLMs, and patches or pull requests may be submitted by contributors or agents run by the
maintainers.

However, all code changes and releases must be reviewed and approved by the maintainers.

=item data_driven_updates

A use case for this is a cron job that checks for an updated external data source, adapts that data into a new version
of a module, and uploads a new release to CPAN.

Code is updated from external sources, and released automatically.

Automated contributions may post comments or issues.

=item full_automation

Code is generated by AI or LLMs,
automated contributions can submit patches or pull requests,
and agents operated by the maintainers can make changes to the code and release automatically.

This is discouraged.

=back

See L<Dist::AutomationPolicy> for all supported values and an explanation of what they mean.

=head2 code_generation

This outlines how automated tools will generate or update the code and documentation.
See L<Dist::AutomationPolicy> for values.

=head2 automated_contributions

This outlines what is accepted from users or agents that are not controlled by or explicitly granted access by the
maintainers.  See L<Dist::AutomationPolicy> for values.

=head2 automated_actions

This outlines automated changes made by users or agents that are controlled or explicitly granted access by the project
maintainers.  See L<Dist::AutomationPolicy> for values.

=head2 models

This is an optional name of models used for Model IDs used for L</automated_actions>, and (when known)
L<automated_contributions>.

Model IDs should come from L<https://docs.aimlapi.com/api-references/model-database>.

=for Pod::Coverage mvp_multivalue_args

=for Pod::Coverage gather_files

=for Pod::Coverage register_prereqs

=for Pod::Coverage prune_files

=head1 SEE ALSO

L<Metadata v3 Automation Policy|https://github.com/CPAN-Security/cpan-metadata-v3/blob/main/automation-policy.md>

=head1 SOURCE

The development version is on github at L<https://github.com/robrwo/perl-Dist-Zilla-Plugin-AutomationPolicy>
and may be cloned from L<https://github.com/robrwo/perl-Dist-Zilla-Plugin-AutomationPolicy.git>

=head1 SUPPORT

Only the latest version of this module will be supported.

This module requires Perl v5.24 or later, based on the minimum Perl supported by L<Dist::Zilla>.

=head2 Reporting Bugs and Submitting Feature Requests

Please report any bugs or feature requests on the bugtracker website
L<https://github.com/robrwo/perl-Dist-Zilla-Plugin-AutomationPolicy/issues>

When submitting a bug or request, please include a test-file or a
patch to an existing test-file that illustrates the bug or desired
feature.

If the bug you are reporting has security implications which make it inappropriate to send to a public issue tracker,
then see F<SECURITY.md> for instructions how to report security vulnerabilities.

=head1 AUTHOR

Robert Rothenberg <perl@rhizomnic.com>

=head1 CONTRIBUTOR

=for stopwords Leon Timmermans

Leon Timmermans <fawaka@gmail.com>

=head1 COPYRIGHT AND LICENSE

This software is Copyright (c) 2026 by Robert Rothenberg.

This is free software, licensed under:

  The Artistic License 2.0 (GPL Compatible)

=cut
