#!/usr/bin/env perl

use v5.36;
use Test::Most;

use_ok('App::GHGen::PerlCustomizer', qw(generate_custom_perl_workflow));

# ── Lint step present by default ──────────────────────────────────────────

subtest 'lint step generated by default' => sub {
    my $yaml = generate_custom_perl_workflow({});

    like($yaml, qr/Lint and syntax check/, 'lint step is present by default');
    like($yaml, qr/shell: perl \{0\}/,     'step uses perl {0} shell');
    like($yaml, qr/File::Find/,            'step uses File::Find for cross-platform discovery');
    like($yaml, qr/push \@INC.*\\qq\{1/s,  'step stubs missing deps with in-memory filehandle');
    like($yaml, qr/do \$file/,              'step syntax-checks each file via do');
};

# ── Lint step absent when disabled ────────────────────────────────────────

subtest 'lint step absent when enable_linter => 0' => sub {
    my $yaml = generate_custom_perl_workflow({ enable_linter => 0 });

    unlike($yaml, qr/Lint and syntax check/, 'lint step is absent');
    # shell: perl {0} is also used by the Perl-version detection step, so we
    # cannot assert its global absence — just verify the lint step itself is gone.
};

# ── Lint step runs before tests ────────────────────────────────────────────

subtest 'lint step comes before Run tests' => sub {
    my $yaml = generate_custom_perl_workflow({});

    my $lint_pos  = index($yaml, 'Lint and syntax check');
    my $tests_pos = index($yaml, 'Run tests');

    ok($lint_pos  > 0, 'lint step is present');
    ok($tests_pos > 0, '"Run tests" step is present');
    ok($lint_pos < $tests_pos, 'lint step comes before Run tests');
};

# ── warnings::unused present by default ──────────────────────────────────

subtest 'unused-variable check present by default' => sub {
    my $yaml = generate_custom_perl_workflow({});

    like($yaml, qr/warnings::unused/,        'warnings::unused present by default');
    like($yaml, qr/PERL5OPT=-Mwarnings::unused/, 'PERL5OPT set by default');
};

subtest 'unused-variable check absent when explicitly disabled' => sub {
    my $yaml = generate_custom_perl_workflow({ enable_linter_unused => 0 });

    unlike($yaml, qr/warnings::unused/, 'warnings::unused absent when disabled');
    unlike($yaml, qr/PERL5OPT/,         'PERL5OPT not set when disabled');
};

# ── warnings::unused embedded in lint step when enabled ───────────────────

subtest 'warnings::unused run via prove when enable_linter_unused => 1' => sub {
    my $yaml = generate_custom_perl_workflow({ enable_linter_unused => 1 });

    like($yaml, qr/Lint and syntax check/,       'lint step still present');
    like($yaml, qr/warnings::unused/,            'warnings::unused referenced');
    like($yaml, qr/PERL5OPT=-Mwarnings::unused/, 'warnings::unused loaded via PERL5OPT');
    like($yaml, qr/prove -lr t\//,               'runs the full test suite');
    like($yaml, qr/\|\| echo/,                   'check is non-blocking');
};

# ── warnings::unused conditioned on Linux at runtime ──────────────────────

subtest 'warnings::unused check is gated on Linux at runtime' => sub {
    my $yaml = generate_custom_perl_workflow({ enable_linter_unused => 1 });

    like($yaml, qr/RUNNER_OS.*Linux/s, 'check gated on RUNNER_OS eq Linux');
};

# ── warnings::unused appears inside lint step, before Run tests ───────────

subtest 'warnings::unused position is inside lint step' => sub {
    my $yaml = generate_custom_perl_workflow({
        enable_linter_unused => 1,
        enable_critic        => 1,
    });

    my $find_pos    = index($yaml, 'File::Find');
    my $unused_pos  = index($yaml, 'warnings::unused');
    my $tests_pos   = index($yaml, 'Run tests');
    my $critic_pos  = index($yaml, 'Run Perl::Critic');

    ok($find_pos   > 0, 'File::Find present');
    ok($unused_pos > 0, 'warnings::unused present');
    ok($tests_pos  > 0, '"Run tests" present');
    ok($critic_pos > 0, '"Run Perl::Critic" present');
    ok($find_pos   < $unused_pos, 'warnings::unused comes after syntax check');
    ok($unused_pos < $tests_pos,  'warnings::unused comes before Run tests');
    ok($unused_pos < $critic_pos, 'warnings::unused comes before Run Perl::Critic');
};

# ── Both linter options disabled independently ─────────────────────────────

subtest 'options are independent' => sub {
    my $yaml_neither = generate_custom_perl_workflow({
        enable_linter        => 0,
        enable_linter_unused => 0,
    });
    unlike($yaml_neither, qr/Lint and syntax check/, 'no lint step');
    unlike($yaml_neither, qr/warnings::unused/,      'no warnings::unused');

    my $yaml_lint_only = generate_custom_perl_workflow({
        enable_linter        => 1,
        enable_linter_unused => 0,
    });
    like($yaml_lint_only,   qr/Lint and syntax check/, 'lint step present');
    unlike($yaml_lint_only, qr/warnings::unused/,      'no warnings::unused when not requested');

    my $yaml_both = generate_custom_perl_workflow({
        enable_linter        => 1,
        enable_linter_unused => 1,
    });
    like($yaml_both, qr/Lint and syntax check/, 'lint step present');
    like($yaml_both, qr/warnings::unused/,      'warnings::unused present when both enabled');
};

# ── perlimports present by default ───────────────────────────────────────────

subtest 'perlimports step present by default' => sub {
    my $yaml = generate_custom_perl_workflow({});

    like($yaml, qr/Check imports with perlimports/, 'perlimports step present by default');
    like($yaml, qr/App::perlimports/,               'installs App::perlimports');
    like($yaml, qr/perlimports --lint/,             'runs perlimports --lint');
};

subtest 'perlimports step absent when explicitly disabled' => sub {
    my $yaml = generate_custom_perl_workflow({ enable_perlimports => 0 });

    unlike($yaml, qr/Check imports with perlimports/, 'perlimports step absent when disabled');
    unlike($yaml, qr/App::perlimports/,               'App::perlimports not installed when disabled');
};

subtest 'perlimports step is advisory and scoped to latest Perl + ubuntu' => sub {
    my $yaml = generate_custom_perl_workflow({ enable_perlimports => 1 });

    like($yaml, qr/continue-on-error: true/,                      'perlimports is non-blocking');
    like($yaml, qr/ubuntu-latest/,                                'perlimports scoped to ubuntu-latest');
    like($yaml, qr/Check imports with perlimports.*continue-on-error/s, 'continue-on-error on the right step');
};

subtest 'perlimports comes after critic and before coverage' => sub {
    my $yaml = generate_custom_perl_workflow({
        enable_critic      => 1,
        enable_perlimports => 1,
        enable_coverage    => 1,
    });

    my $critic_pos     = index($yaml, 'Perl::Critic');
    my $perlimports_pos = index($yaml, 'App::perlimports');
    my $coverage_pos   = index($yaml, 'Devel::Cover');

    ok($critic_pos      > 0, 'critic step present');
    ok($perlimports_pos > 0, 'perlimports step present');
    ok($coverage_pos    > 0, 'coverage step present');
    ok($critic_pos      < $perlimports_pos, 'perlimports comes after critic');
    ok($perlimports_pos < $coverage_pos,    'perlimports comes before coverage');
};

done_testing();
