#!/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/Mkdir.pm'} = 1;
    $INC{'DBD/SQLite.pm'} = 1;
    $INC{'Time/Piece.pm'} = 1;
    $INC{'Data/UUID.pm'} = 1;

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

    package Cpanel::Mkdir;
    sub ensure_directory_existence_and_mode { return 1; }

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

    package Time::Piece;
    sub new { return bless {}, shift; }
    sub strftime { return '2025-01-01 00:00:00'; }

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

    package Data::UUID;
    our $UUID_COUNTER = 0;
    sub new { return bless {}, shift; }
    sub create_str {
        $UUID_COUNTER++;
        return sprintf( '00000000-0000-0000-0000-%012d', $UUID_COUNTER );
    }

    $INC{'Whostmgr/CometBackup/Constants.pm'} = 1;
    package Whostmgr::CometBackup::Constants;
    our $BACKUP_JOBS_DB = '/tmp/backup_jobs.db';
    our $RUN_LOG_DB = '/tmp/restore_runs.db';

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

use Whostmgr::CometBackup::RestoreRunLogs ();

my $db_file = '/tmp/restore_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 'RestoreRunLogs - new instance' => sub {
    my $mock_module = Test::MockModule->new('Whostmgr::CometBackup::RestoreRunLogs');
    $mock_module->redefine( '_ensure_database' => sub { } );

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

subtest 'RestoreRunLogs - create_restore_job missing restore_type' => sub {
    my $mock_module = Test::MockModule->new('Whostmgr::CometBackup::RestoreRunLogs');
    $mock_module->redefine( '_ensure_database' => sub { } );

    my $logs = Whostmgr::CometBackup::RestoreRunLogs->new();

    like(
        dies { $logs->create_restore_job(
            users => ['user1'],
        ); },
        qr/restore_type parameter required/,
        'Dies when restore_type is missing'
    );
};

subtest 'RestoreRunLogs - create_restore_job missing users' => sub {
    my $mock_module = Test::MockModule->new('Whostmgr::CometBackup::RestoreRunLogs');
    $mock_module->redefine( '_ensure_database' => sub { } );

    my $logs = Whostmgr::CometBackup::RestoreRunLogs->new();

    like(
        dies { $logs->create_restore_job(
            restore_type => 'full',
        ); },
        qr/users parameter required/,
        'Dies when users is missing'
    );
};

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

    my $logs = Whostmgr::CometBackup::RestoreRunLogs->new();
    my $formatted = $logs->_format_timestamp(1704067200);

    ok( defined $formatted, 'Returns formatted timestamp' );
    like( $formatted, qr/\d{4}-\d{2}-\d{2}/, 'Timestamp format looks correct' );
};

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

    my $logs = Whostmgr::CometBackup::RestoreRunLogs->new();
    
    my $seconds = $logs->_format_duration(45);
    is( $seconds, '45s', 'Formats seconds correctly' );

    my $minutes = $logs->_format_duration(125);
    like( $minutes, qr/^2m \d+s$/, 'Formats minutes correctly' );

    my $hours = $logs->_format_duration(3725);
    like( $hours, qr/^1h \d+m \d+s$/, 'Formats hours correctly' );
};

done_testing();
