#!/usr/local/cpanel/3rdparty/bin/perl -w
#                                      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 Test2::V0;
use Test2::Tools::Explain;
use Test2::Plugin::NoWarnings;
use Test2::Tools::Subtest;
use Test2::Tools::Exception;

use Test::MockModule qw/strict/;

use Test::Cpanel::Policy testing => 'Cpanel::Plugins::Components::Rules::DateRange';
use Capture::Tiny;

use Cpanel::Plugins::Components::Rules::DateRange;

sub format_date ($date) {
    my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = gmtime($date);
    $year = $year + 1900;
    $mon += 1;

    return sprintf( "%02d-%02d-%d", $mon, $mday, $year );
}

my $now            = time;
my $today          = format_date($now);
my $yesterday      = format_date( $now - 86400 );
my $twodaysago     = format_date( $now - ( 2 * 86400 ) );
my $tommorow       = format_date( $now + 86400 );
my $twodaysfromnow = format_date( $now + ( 2 * 86400 ) );

diag "2 DAYS AGO:      $twodaysago";
diag "YESTERDAY:       $yesterday";
diag "TODAY:           $today";
diag "TOMMOROW:        $tommorow";
diag "2 DAYS FROM NOW: $twodaysfromnow";

subtest 'Check when rule provides only a start-date' => sub {

    my $got = Cpanel::Plugins::Components::Rules::DateRange->new( { "start_date" => $yesterday } )->is_allowed();
    is $got, !!1, "got true value when $today is > the start date: $yesterday.";

    $got = Cpanel::Plugins::Components::Rules::DateRange->new( { "start_date" => $tommorow } )->is_allowed();
    is $got, !!0, "got false value when $today is < the start date: $tommorow.";

};

subtest 'Check when rule provides only a end-date' => sub {
    my $got = Cpanel::Plugins::Components::Rules::DateRange->new( { "end_date" => $yesterday } )->is_allowed();
    is $got, !!0, "got false value when $today is > the end date: $yesterday.";

    $got = Cpanel::Plugins::Components::Rules::DateRange->new( { "end_date" => $tommorow } )->is_allowed();
    is $got, !!1, "got true value when $today is < the start date: $tommorow.";
};

subtest 'Check when rule provides both a start-date and end-date' => sub {
    my $got = Cpanel::Plugins::Components::Rules::DateRange->new(
        {
            "start_date" => $yesterday,
            "end_date"   => $tommorow
        }
    )->is_allowed();
    is $got, !!1, "got true value when $today is > the start date: $yesterday and < end date of $tommorow.";

    $got = Cpanel::Plugins::Components::Rules::DateRange->new(
        {
            "start_date" => $twodaysago,
            "end_date"   => $yesterday
        }
    )->is_allowed();
    is $got, !!0, "got false value when $today is after the end date: $yesterday.";

    $got = Cpanel::Plugins::Components::Rules::DateRange->new(
        {
            "start_date" => $tommorow,
            "end_date"   => $twodaysfromnow
        }
    )->is_allowed();
    is $got, !!0, "got false value when $today is before the start date: $tommorow.";
};

done_testing;
