#!/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;

BEGIN {
    $INC{'Cpanel/DBI/SQLite.pm'} = 1;
    $INC{'Cpanel/FileUtils/TouchFile.pm'} = 1;
    $INC{'Cpanel/JSON.pm'} = 1;
    $INC{'Cpanel/Mkdir.pm'} = 1;
    $INC{'DBD/SQLite.pm'} = 1;
    $INC{'Time/Piece.pm'} = 1;

    package Cpanel::DBI::SQLite;
    sub connect { return bless {}, shift; }
    sub prepare { return bless {}, 'MockSTH'; }
    sub disconnect { }

    package Cpanel::FileUtils::TouchFile;
    sub new { return bless {}, shift; }

    package Cpanel::JSON;
    sub Load { return eval { JSON::PP::decode_json( $_[0] ) } || {}; }
    sub Dump { return JSON::PP::encode_json( $_[0] ); }

    package JSON::PP;
    sub encode_json { return shift; }
    sub decode_json { return {}; }

    package Cpanel::Mkdir;
    sub new { return bless {}, shift; }

    package DBD::SQLite;
    sub new { return bless {}, shift; }

    package Time::Piece;
    sub new { return bless {}, shift; }

    package MockSTH;
    sub execute { return 1; }
    sub fetchrow_hashref { return undef; }

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

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

    $INC{'Whostmgr/CometBackup/Logger.pm'} = 1;
    package Whostmgr::CometBackup::Logger;
    sub new { return bless {}, shift; }
    sub info { }
    sub warn { }
    sub error { }
}

use Whostmgr::CometBackup::BackupRunLogs ();

my $db_file = '/tmp/backup_runs.db';
my @mock_files = (
    Test::MockFile->file('/usr/local/cpanel/logs/error_log'),
    Test::MockFile->file('/var/cpanel/server_locale'),
    Test::MockFile->file($db_file),
);

subtest 'BackupRunLogs - new instance' => sub {
    my $mock_module = Test::MockModule->new('Whostmgr::CometBackup::BackupRunLogs');
    $mock_module->redefine( '_ensure_database' => sub { } );

    my $logs = Whostmgr::CometBackup::BackupRunLogs->new();
    
    ok( defined $logs, 'BackupRunLogs instance created' );
    is( ref $logs, 'Whostmgr::CometBackup::BackupRunLogs', 'Correct class' );
    ok( defined $logs->{logger}, 'Logger initialized' );
};

subtest 'BackupRunLogs - get_backup_runs returns array' => sub {
    my $mock_module = Test::MockModule->new('Whostmgr::CometBackup::BackupRunLogs');
    $mock_module->redefine( '_ensure_database' => sub { } );

    my $logs = Whostmgr::CometBackup::BackupRunLogs->new();
    my @runs = $logs->get_backup_runs();

    is( ref \@runs, 'ARRAY', 'Returns array' );
};

subtest 'BackupRunLogs - get_recent_runs' => sub {
    my $mock_module = Test::MockModule->new('Whostmgr::CometBackup::BackupRunLogs');
    $mock_module->redefine( '_ensure_database' => sub { } );
    $mock_module->redefine( 'get_backup_runs' => sub { return (); } );

    my $logs = Whostmgr::CometBackup::BackupRunLogs->new();
    my @runs = $logs->get_recent_runs();

    is( ref \@runs, 'ARRAY', 'Returns array' );
};

subtest 'BackupRunLogs - get_runs_for_job' => sub {
    my $mock_module = Test::MockModule->new('Whostmgr::CometBackup::BackupRunLogs');
    $mock_module->redefine( '_ensure_database' => sub { } );
    $mock_module->redefine( 'get_backup_runs' => sub { return (); } );

    my $logs = Whostmgr::CometBackup::BackupRunLogs->new();
    my $test_uuid = '00000000-0000-0000-0000-000000000001';
    my @runs = $logs->get_runs_for_job($test_uuid);

    is( ref \@runs, 'ARRAY', 'Returns array' );
};

done_testing();
