#!/usr/local/cpanel/3rdparty/bin/perl

use cPstrict;
use Test::Cpanel::Policy;

use Test2::V0;
use Test2::Tools::Explain;
use Test2::Tools::Compare;
use Test2::Tools::Exception;
use Test2::Plugin::NoWarnings;

use Test::MockModule;
use Test::MockFile;
use Cpanel::TempFile;

# Pre-load stub modules to prevent Setup.pm dependencies from failing
BEGIN {
    $INC{'Cpanel/Plugins/Components/Rules.pm'} = 1;
    $INC{'Cpanel/Plugins/Components/Rules/Util/NVData.pm'} = 1;
    $INC{'Cpanel/Plugins/Components/Rules/CompanyId.pm'} = 1;
}

use Whostmgr::API::1::CometBackup ();
use Cpanel::JSON ();

# Mock error log file for all tests
my @mock_files = (
    Test::MockFile->file('/usr/local/cpanel/logs/error_log'),
);

subtest 'download_backup_job_log - missing JSON parameter' => sub {
    my $mock_utils = Test::MockModule->new('Whostmgr::API::1::Utils');
    $mock_utils->redefine(
        'set_metadata_not_ok' => sub {
            my ( $metadata, $error ) = @_;
            $metadata->{ok}    = 0;
            $metadata->{error} = $error;
            return 1;
        }
    );

    my $metadata = {};
    my $args     = {};

    my $response = Whostmgr::API::1::CometBackup::download_backup_job_log( $args, $metadata );

    is $metadata->{ok}, 0, 'Metadata is set to not ok';
    like $metadata->{error}, qr/json/i, 'Error mentions json parameter';

    return;
};

subtest 'download_backup_job_log - missing job_uuid' => sub {
    my $mock_utils = Test::MockModule->new('Whostmgr::API::1::Utils');
    $mock_utils->redefine(
        'set_metadata_not_ok' => sub {
            my ( $metadata, $error ) = @_;
            $metadata->{ok}    = 0;
            $metadata->{error} = $error;
            return 1;
        }
    );

    my $metadata = {};
    my $args     = { json => Cpanel::JSON::Dump( { run_uuid => 'a1b2c3d4-e5f6-47a8-b9c0-d1e2f3a4b5c6' } ) };

    my $response = Whostmgr::API::1::CometBackup::download_backup_job_log( $args, $metadata );

    is $metadata->{ok}, 0, 'Metadata is set to not ok';
    like $metadata->{error}, qr/job_uuid.*required/i, 'Error mentions job_uuid is required';

    return;
};

subtest 'download_backup_job_log - missing run_uuid' => sub {
    my $mock_utils = Test::MockModule->new('Whostmgr::API::1::Utils');
    $mock_utils->redefine(
        'set_metadata_not_ok' => sub {
            my ( $metadata, $error ) = @_;
            $metadata->{ok}    = 0;
            $metadata->{error} = $error;
            return 1;
        }
    );

    my $metadata = {};
    my $args     = { json => Cpanel::JSON::Dump( { job_uuid => 'a1b2c3d4-e5f6-47a8-b9c0-d1e2f3a4b5c6' } ) };

    my $response = Whostmgr::API::1::CometBackup::download_backup_job_log( $args, $metadata );

    is $metadata->{ok}, 0, 'Metadata is set to not ok';
    like $metadata->{error}, qr/run_uuid.*required/i, 'Error mentions run_uuid is required';

    return;
};

subtest 'download_backup_job_log - invalid job_uuid format' => sub {
    my $mock_utils = Test::MockModule->new('Whostmgr::API::1::Utils');
    $mock_utils->redefine(
        'set_metadata_not_ok' => sub {
            my ( $metadata, $error ) = @_;
            $metadata->{ok}    = 0;
            $metadata->{error} = $error;
            return 1;
        }
    );

    my $metadata = {};
    my $args     = { json => Cpanel::JSON::Dump( { 
        job_uuid => 'invalid-uuid',
        run_uuid => 'a1b2c3d4-e5f6-47a8-b9c0-d1e2f3a4b5c6'
    } ) };

    my $response = Whostmgr::API::1::CometBackup::download_backup_job_log( $args, $metadata );

    is $metadata->{ok}, 0, 'Metadata is set to not ok';
    like $metadata->{error}, qr/job_uuid.*format/i, 'Error mentions job_uuid format';

    return;
};

subtest 'download_backup_job_log - invalid run_uuid format' => sub {
    my $mock_utils = Test::MockModule->new('Whostmgr::API::1::Utils');
    $mock_utils->redefine(
        'set_metadata_not_ok' => sub {
            my ( $metadata, $error ) = @_;
            $metadata->{ok}    = 0;
            $metadata->{error} = $error;
            return 1;
        }
    );

    my $metadata = {};
    my $args     = { json => Cpanel::JSON::Dump( { 
        job_uuid => 'a1b2c3d4-e5f6-47a8-b9c0-d1e2f3a4b5c6',
        run_uuid => 'not-valid-uuid'
    } ) };

    my $response = Whostmgr::API::1::CometBackup::download_backup_job_log( $args, $metadata );

    is $metadata->{ok}, 0, 'Metadata is set to not ok';
    like $metadata->{error}, qr/run_uuid.*format/i, 'Error mentions run_uuid format';

    return;
};

done_testing();
