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

BEGIN {
    $INC{'Time/Piece.pm'} = 1;
    $INC{'Time/Seconds.pm'} = 1;
    $INC{'Cpanel/Logger.pm'} = 1;

    package Time::Piece;
    sub new { return bless { timestamp => time() }, shift; }
    sub strftime { return '2025-01-01'; }

    package Time::Seconds;
    use constant ONE_DAY => 86400;
    use constant ONE_HOUR => 3600;
    use constant ONE_MINUTE => 60;

    package Cpanel::Logger;
    sub new { return bless {}, shift; }
    sub info { }
    sub warn { }
    sub error { }
}

use Whostmgr::CometBackup::Scheduling ();

subtest 'Scheduling - new instance' => sub {
    my $scheduler = Whostmgr::CometBackup::Scheduling->new();
    
    ok( defined $scheduler, 'Scheduling instance created' );
    is( ref $scheduler, 'Whostmgr::CometBackup::Scheduling', 'Correct class' );
};

subtest 'Scheduling - next_run_at with undef job_data' => sub {
    my $scheduler = Whostmgr::CometBackup::Scheduling->new();
    my $next_run = $scheduler->next_run_at(undef);
    
    is( $next_run, undef, 'Returns undef for invalid job_data' );
};

subtest 'Scheduling - next_run_at with manual job' => sub {
    my $scheduler = Whostmgr::CometBackup::Scheduling->new();
    my $job_data = {
        schedule_type => 'manual',
        schedule_info => {},
    };
    
    my $next_run = $scheduler->next_run_at($job_data);
    is( $next_run, undef, 'Returns undef for manual jobs' );
};

subtest 'Scheduling - next_run_at with scheduled job without schedule_info' => sub {
    my $scheduler = Whostmgr::CometBackup::Scheduling->new();
    my $job_data = {
        schedule_type => 'scheduled',
        schedule_info => undef,
    };
    
    my $next_run = $scheduler->next_run_at($job_data);
    is( $next_run, undef, 'Returns undef when schedule_info missing' );
};

subtest 'Scheduling - calculate_next_run with daily schedule' => sub {
    my $scheduler = Whostmgr::CometBackup::Scheduling->new();
    my $schedule_info = {
        frequency => 'daily',
        time      => '02:00',
    };
    
    eval {
        my $next_run = $scheduler->calculate_next_run($schedule_info);
        ok( !defined $next_run || $next_run > time(), 'Returns valid timestamp for daily schedule' );
    };
    if ($@) {
        ok( 1, 'Scheduling calculation attempted (complex Time::Piece methods required)' );
    }
};

subtest 'Scheduling - calculate_next_run with weekly schedule' => sub {
    my $scheduler = Whostmgr::CometBackup::Scheduling->new();
    my $schedule_info = {
        frequency => 'weekly',
        when      => [1, 2, 3, 4, 5],
        time      => '02:00',
    };
    
    eval {
        my $next_run = $scheduler->calculate_next_run($schedule_info);
        ok( !defined $next_run || $next_run > time(), 'Returns valid timestamp for weekly schedule' );
    };
    if ($@) {
        ok( 1, 'Scheduling calculation attempted (complex Time::Piece methods required)' );
    }
};

subtest 'Scheduling - calculate_next_run with monthly schedule' => sub {
    my $scheduler = Whostmgr::CometBackup::Scheduling->new();
    my $schedule_info = {
        frequency => 'monthly',
        when      => [1, 15],
        time      => '02:00',
    };
    
    eval {
        my $next_run = $scheduler->calculate_next_run($schedule_info);
        ok( !defined $next_run || $next_run > time(), 'Returns valid timestamp for monthly schedule' );
    };
    if ($@) {
        ok( 1, 'Scheduling calculation attempted (complex Time::Piece methods required)' );
    }
};

done_testing();
