#!/usr/local/cpanel/3rdparty/bin/perl

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;

# Pre-load stub modules to prevent dependencies from failing
BEGIN {
    $INC{'Cpanel/Plugins/Components/Rules.pm'} = 1;
    $INC{'Cpanel/Plugins/Components/Rules/Util/NVData.pm'} = 1;
    $INC{'Cpanel/Plugins/Components/Rules/CompanyId.pm'} = 1;
}

# Mock the Constants module BEFORE importing CometBackup
BEGIN {
    $INC{'Whostmgr/CometBackup/Constants.pm'} = 1;
    package Whostmgr::CometBackup::Constants;
    our $DISABLED_TOUCHFILE = '/var/cpanel/comet/comet_backup_disabled';
    1;
}

use Whostmgr::API::1::CometBackup ();

# Create mock file at module scope so it persists
my $mock_file = Test::MockFile->file('/var/cpanel/comet/comet_backup_disabled', 'disabled');

# Mock the logger globally to prevent file access
my $mock_logger_global = Test::MockModule->new('Cpanel::Logger');
$mock_logger_global->redefine(
    'info' => sub {
        return 1;
    },
    'error' => sub {
        return 1;
    },
    'warn' => sub {
        return 1;
    },
    'debug' => sub {
        return 1;
    }
);

subtest 'is_comet_backup_enabled - returns correct touchfile path' => sub {
    my $mock_utils = Test::MockModule->new('Whostmgr::API::1::Utils');
    $mock_utils->redefine(
        'set_metadata_ok' => sub {
            my ($metadata) = @_;
            $metadata->{ok} = 1;
            return 1;
        }
    );
    
    my $args = {};
    my $metadata = {};
    
    my $response = Whostmgr::API::1::CometBackup::is_comet_backup_enabled( $args, $metadata );
    
    is $response->{touchfile}, '/var/cpanel/comet/comet_backup_disabled', 'Touchfile path is set correctly';
    ok defined $response->{enabled}, 'enabled field is defined';
    ok defined $response->{disabled}, 'disabled field is defined';
    
    return;
};

subtest 'is_comet_backup_enabled - always calls set_metadata_ok' => sub {
    my $metadata_ok_called = 0;
    
    my $mock_utils = Test::MockModule->new('Whostmgr::API::1::Utils');
    $mock_utils->redefine(
        'set_metadata_ok' => sub {
            my ($metadata) = @_;
            $metadata_ok_called = 1;
            $metadata->{ok} = 1;
            return 1;
        }
    );
    
    my $args = {};
    my $metadata = {};
    
    my $response = Whostmgr::API::1::CometBackup::is_comet_backup_enabled( $args, $metadata );
    
    ok $metadata_ok_called, 'set_metadata_ok was called';
    is $metadata->{ok}, 1, 'Metadata ok flag is set to 1';
    
    return;
};

subtest 'is_comet_backup_enabled - returns expected fields' => sub {
    my $mock_utils = Test::MockModule->new('Whostmgr::API::1::Utils');
    $mock_utils->redefine(
        'set_metadata_ok' => sub {
            my ($metadata) = @_;
            $metadata->{ok} = 1;
            return 1;
        }
    );
    
    my $args = {};
    my $metadata = {};
    
    my $response = Whostmgr::API::1::CometBackup::is_comet_backup_enabled( $args, $metadata );
    
    ok ref($response) eq 'HASH', 'Response is a hash reference';
    ok exists $response->{enabled}, 'Response has enabled field';
    ok exists $response->{disabled}, 'Response has disabled field';
    ok exists $response->{touchfile}, 'Response has touchfile field';
    
    return;
};

done_testing();
