#!/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
package Cpanel::Plugins::Components::Rules::Util::FindStartDate;
use cPstrict;

use Test2::V0;
use Test2::Tools::Explain;
use Test2::Plugin::NoWarnings;
use Test2::Tools::Subtest;
use Test2::Tools::Exception;
use experimental 'builtin';
use Test::MockModule qw/strict/;

use Cpanel::Plugins::Components::Rules::Util::FindStartDate;

subtest 'Verify process date range' => sub {
    my $tests = [
        {
            given => {
                'date-range' => {
                    'start_date' => '18-10-2024',
                    'end_date'   => '10-15-2024'
                },
                'company-id' => [7]
            },
            expect => {
                title => 'multiple rules including date-range rule',
                key   => 'start_date',
                value => '18-10-2024',
            },
        },
        {
            given => {
                'date-range' => [
                    'start_date' => '10-10-2024',
                    'end_date'   => '10-15-2024'
                ],
            },
            expect => {
                title => 'date-range rule array form [deprecated]',
                key   => 'start_date',
                value => '10-10-2024',
            },
        },
        {
            given => {
                'date-range' => {
                    'start_date' => '10-11-2024',
                    'end_date'   => '10-22-2024'
                },
            },
            expect => {
                title => 'date-range rule hash form',
                key   => 'start_date',
                value => '10-11-2024',
            },
        },
        {
            given  => { 'company-id' => [7] },
            expect => {
                title => 'no date-range rule found',
                key   => undef,
                value => undef,
            },
        }
    ];

    my ( $output, $nv_campid, $nv_key, $nv_value );
    my $mock = Test::MockModule->new('Cpanel::Plugins::Components::Rules::Util::NVData');
    $mock->redefine(
        set_nv_data => sub {
            ( $nv_campid, $nv_key, $nv_value ) = @_;
            return 1;
        }
    );
    my $campaign_id = 'campaign.360-monitoring.prerelease';
    foreach my $test (@$tests) {
        my $given_rule = $test->{given};
        my $expect     = $test->{expect};

        note "When processing: $expect->{title}";
        ok(
            lives {
                Cpanel::Plugins::Components::Rules::Util::FindStartDate::process_start_date( $campaign_id, $given_rule );
            },
            "process_start_date lives when called with a campaign and rule"
        ) or print "$@";

        is $nv_value, $expect->{value}, "process_start_date captures expected value.";
        is $nv_key,   $expect->{key},   "process_start_date captures expected key `start_date`.";
        ( $nv_campid, $nv_key, $nv_value ) = '';
    }

    # when no campaign_id is sent, it returns immediately
    $mock        = '';
    $campaign_id = '';
    ok(
        lives {
            Cpanel::Plugins::Components::Rules::Util::FindStartDate::process_start_date( $campaign_id, $tests->[0]->{given} );
        },
        "process_start_date returns early without campaign_id"
    ) or print "$@";

};

subtest 'Verify compare_start_date' => sub {
    my @sample_data = (
        {
            nvdate    => '10-10-2024',
            rule_date => '10-10-2024',
            result    => 1,
        },
        {
            nvdate    => '11-10-2024',
            rule_date => '10-10-2024',
            result    => 0,
        },
        {
            nvdate    => '',
            rule_date => '',
            result    => 0,

        },
    );

    foreach my $data (@sample_data) {
        my $result = Cpanel::Plugins::Components::Rules::Util::FindStartDate::compare_start_date( $data->{nvdate}, $data->{rule_date} );
        is( $result, $data->{result}, "compare_start_date returns the expected output" );
    }

};
subtest 'Verify get_start_date' => sub {
    my ( $output, $nv_campid, $nv_key, $nv_value );
    my $expected_date = '10-24-2019';
    my $mock          = Test::MockModule->new('Cpanel::Plugins::Components::Rules::Util::NVData');
    $mock->redefine(
        get_nv_data => sub {
            ( $nv_campid, $nv_key ) = @_;
            return $expected_date;
        }
    );
    my $i           = 0;
    my $campaign_id = 'campaign.360-monitoring.prerelease';

    $output = Cpanel::Plugins::Components::Rules::Util::FindStartDate::get_start_date($campaign_id);
    is( $output, $expected_date, "get_start_date returns expected start_date" );

    # when no campaign_id is sent, it returns immediately
    $mock        = '';
    $campaign_id = '';
    ok(
        lives {
            Cpanel::Plugins::Components::Rules::Util::FindStartDate::get_start_date($campaign_id);
        },
        "get_start_date returns early without campaign_id"
    ) or print "$@";

};

done_testing;
