#!/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;
    
    # Pre-load Cpanel modules so require statements don't fail
    $INC{'Cpanel/FileUtils/TouchFile.pm'} = 1;
    package Cpanel::FileUtils::TouchFile;
    sub touchfile { return 1; }
    
    $INC{'Cpanel/Mkdir.pm'} = 1;
    package Cpanel::Mkdir;
    sub ensure_directory_existence_and_mode { return 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 ();

# Mock the error_log file for tests that log errors
Test::MockFile->file('/usr/local/cpanel/logs/error_log');

# 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 'disable_comet_backup - mkdir fails' => sub {
    my $mock_utils = Test::MockModule->new('Whostmgr::API::1::Utils');
    $mock_utils->redefine(
        'set_metadata_not_ok' => sub {
            my ( $metadata, $error ) = @_;
            $metadata->{ok} = 0;
            $metadata->{error} = $error;
            return 1;
        }
    );
    
    my $mock_mkdir = Test::MockModule->new('Cpanel::Mkdir');
    $mock_mkdir->redefine(
        'ensure_directory_existence_and_mode' => sub {
            die "Failed to create directory\n";
        }
    );
    
    my $args = {};
    my $metadata = {};
    
    my $response = Whostmgr::API::1::CometBackup::disable_comet_backup( $args, $metadata );
    
    is $response, hash {
        field disabled => 0;
        end();
    }, 'Returns disabled=0 when mkdir fails';
    
    is $metadata->{ok}, 0, 'Metadata is set to not ok when mkdir fails';
    like $metadata->{error}, qr/Failed to disable Comet Backup/, 'Error message indicates failure';
    
    return;
};

subtest 'disable_comet_backup - touchfile creation fails' => sub {
    my $mock_utils = Test::MockModule->new('Whostmgr::API::1::Utils');
    $mock_utils->redefine(
        'set_metadata_not_ok' => sub {
            my ( $metadata, $error ) = @_;
            $metadata->{ok} = 0;
            $metadata->{error} = $error;
            return 1;
        }
    );
    
    my $mock_mkdir = Test::MockModule->new('Cpanel::Mkdir');
    $mock_mkdir->redefine(
        'ensure_directory_existence_and_mode' => sub {
            return 1;
        }
    );
    
    my $mock_touch = Test::MockModule->new('Cpanel::FileUtils::TouchFile');
    $mock_touch->redefine(
        'touchfile' => sub {
            die "Failed to create touchfile: Permission denied\n";
        }
    );
    
    my $args = {};
    my $metadata = {};
    
    my $response = Whostmgr::API::1::CometBackup::disable_comet_backup( $args, $metadata );
    
    is $response, hash {
        field disabled => 0;
        end();
    }, 'Returns disabled=0 when touchfile creation fails';
    
    is $metadata->{ok}, 0, 'Metadata is set to not ok when touchfile fails';
    like $metadata->{error}, qr/Failed to disable Comet Backup/, 'Error message indicates failure';
    
    return;
};

done_testing();
