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

#                                      Copyright 2025 WebPros International, LLC
#                                                           All rights reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited.

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::JSON;

# Pre-load stub modules to prevent 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;

    $INC{'Cpanel/Server/Type.pm'} = 1;
    package Cpanel::Server::Type;
    sub get_products { return { 'cometbackup' => 1 }; }

    $INC{'Cpanel/PwCache.pm'} = 1;
    package Cpanel::PwCache;
    sub new { return bless {}, shift; }

    $INC{'Cpanel/Config/Users.pm'} = 1;
    package Cpanel::Config::Users;
    sub getcpusers { return qw(user1 user2); }

    $INC{'Whostmgr/CometBackup/Setup.pm'} = 1;
    package Whostmgr::CometBackup::Setup;
    our @EXPORT = qw(is_comet_agent_installed is_agent_logged_in setup_comet_agent install_comet_agent);
    sub is_comet_agent_installed { return 0; }
    sub is_agent_logged_in       { return 0; }
    sub setup_comet_agent        { return 1; }
    sub install_comet_agent      { return 1; }

    $INC{'Whostmgr/CometBackup/AgentManager.pm'} = 1;
    package Whostmgr::CometBackup::AgentManager;
    our @EXPORT = qw(get_status start);
    sub get_status { return ( 1, 'active' ); }
    sub start      { return ( 1, '' ); }

    $INC{'Whostmgr/CometBackup/CometBackupAPI.pm'} = 1;
    package Whostmgr::CometBackup::CometBackupAPI;
    sub new  { return bless {}, shift; }
    sub call { return {}; }

    $INC{'Whostmgr/CometBackup/Constants.pm'} = 1;
    package Whostmgr::CometBackup::Constants;
    our $RUN_LOG_DB = '/nonexistent/db.db';
    sub get_text_log_file_path { return '/var/log/comet.log'; }

    $INC{'Whostmgr/API/1/Utils.pm'} = 1;
    package Whostmgr::API::1::Utils;
    sub set_metadata_ok    { $_[0]->{result} = 1; }
    sub set_metadata_not_ok { $_[0]->{result} = 0; }

    $INC{'Whostmgr/CometBackup/BackupRunLogs.pm'} = 1;
    package Whostmgr::CometBackup::BackupRunLogs;
    sub new { return bless {}, shift; }
    sub get_backup_runs { return (); }
}

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

my @mock_files = (
    Test::MockFile->file('/usr/local/cpanel/logs/error_log'),
    Test::MockFile->file('/nonexistent/db.db'),
);

subtest 'list_backup_run_history - no arguments returns empty history when database does not exist' => sub {
    my $metadata = {};
    my $args     = {};

    my $result = Whostmgr::API::1::CometBackup::list_backup_run_history( $args, $metadata );

    is( $metadata->{result}, 1, 'Metadata result is ok' );
    is( ref $result->{history}, 'ARRAY', 'History is an array' );
    is( scalar @{ $result->{history} }, 0, 'History is empty' );
    is( $result->{total_entries}, 0, 'Total entries is 0' );
};

subtest 'list_backup_run_history - with JSON parameter and empty database' => sub {
    my $metadata = {};
    my $args     = { json => Cpanel::JSON::Dump( { limit => 100 } ) };

    my $result = Whostmgr::API::1::CometBackup::list_backup_run_history( $args, $metadata );

    is( $metadata->{result}, 1, 'Metadata result is ok' );
    is( scalar @{ $result->{history} }, 0, 'History is empty' );
    is( $result->{total_entries}, 0, 'Total entries is 0' );
};

subtest 'list_backup_run_history - returns response structure' => sub {
    my $metadata = {};
    my $args     = {};

    my $result = Whostmgr::API::1::CometBackup::list_backup_run_history( $args, $metadata );

    is( ref $result->{history}, 'ARRAY', 'Response has history array' );
    ok( defined $result->{total_entries}, 'Response has total_entries' );
    ok( defined $result->{filters_applied}, 'Response has filters_applied' );
    is( ref $result->{filters_applied}, 'HASH', 'filters_applied is a hash' );
};

subtest 'list_backup_run_history - with various filter parameters' => sub {
    my $metadata = {};
    my $args = { json => Cpanel::JSON::Dump( {
        status  => 'Completed',
        limit   => 50,
        name    => 'Backup',
        job_type => 'automatic'
    } ) };

    my $result = Whostmgr::API::1::CometBackup::list_backup_run_history( $args, $metadata );

    is( $metadata->{result}, 1, 'Metadata result is ok' );
    is( $result->{filters_applied}{status}, 'Completed', 'Status filter captured' );
    is( $result->{filters_applied}{name}, 'Backup', 'Name filter captured' );
    is( $result->{filters_applied}{job_type}, 'automatic', 'Job type filter captured' );
};

subtest 'list_backup_run_history - time_range filters work' => sub {
    my @time_ranges = qw(last_24_hours last_week last_month last_3_months last_6_months last_year);

    for my $range (@time_ranges) {
        my $metadata = {};
        my $args = { json => Cpanel::JSON::Dump( { time_range => $range } ) };

        my $result = Whostmgr::API::1::CometBackup::list_backup_run_history( $args, $metadata );

        is( $metadata->{result}, 1, "Time range '$range' processed successfully" );
    }
};

subtest 'list_backup_run_history - returns filters_applied in response' => sub {
    my $metadata = {};
    my $args = { json => Cpanel::JSON::Dump( { status => 'Completed' } ) };

    my $result = Whostmgr::API::1::CometBackup::list_backup_run_history( $args, $metadata );

    is( ref $result->{filters_applied}, 'HASH', 'filters_applied is a hash' );
    ok( exists $result->{filters_applied}{status}, 'Status filter in filters_applied' );
};

done_testing();
