#!/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
    $INC{'Cpanel/Server/Type.pm'} = 1;
    package Cpanel::Server::Type;
    sub get_products { return { 'cometbackup' => 1 }; }
    
    $INC{'Cpanel/PwCache.pm'} = 1;
    package Cpanel::PwCache;
    sub new { return bless {}, shift; }
    
    # Pre-load CometBackup modules with stubs that can be overridden
    $INC{'Whostmgr/CometBackup/Setup.pm'} = 1;
    package Whostmgr::CometBackup::Setup;
    our @EXPORT = qw(is_comet_agent_installed is_agent_logged_in setup_comet_agent install_comet_agent);
    sub is_comet_agent_installed { return 0; }
    sub is_agent_logged_in { return 0; }
    sub setup_comet_agent { return 1; }
    sub install_comet_agent { return 1; }
    
    $INC{'Whostmgr/CometBackup/AgentManager.pm'} = 1;
    package Whostmgr::CometBackup::AgentManager;
    our @EXPORT = qw(get_status start);
    sub get_status { return (1, 'active'); }
    sub start { return (1, ''); }
    
    $INC{'Whostmgr/CometBackup/CometBackupAPI.pm'} = 1;
    package Whostmgr::CometBackup::CometBackupAPI;
    our @EXPORT = qw(get_server_config);
    sub get_server_config { return {}; }
    
    $INC{'Whostmgr/CometBackup/BackupRunLogs.pm'} = 1;
    package Whostmgr::CometBackup::BackupRunLogs;
    sub new { return bless {}, shift; }
}

# 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 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 'get_comet_backup_configuration_status - error checking agent status' => 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_setup = Test::MockModule->new('Whostmgr::CometBackup::Setup');
    $mock_setup->redefine(
        'is_comet_agent_installed' => sub {
            return 1;
        }
    );
    
    my $mock_agent_manager = Test::MockModule->new('Whostmgr::CometBackup::AgentManager');
    $mock_agent_manager->redefine(
        'get_status' => sub {
            return (0, 'Failed to check status');
        }
    );
    
    my $args = {};
    my $metadata = {};
    
    my $response = Whostmgr::API::1::CometBackup::get_comet_backup_configuration_status( $args, $metadata );
    
    is $response->{configuration}->{status}, 'error', 'Status is error when checking agent fails';
    is $metadata->{ok}, 0, 'Metadata is set to not ok on error';
    like $metadata->{error}, qr/Failed/, 'Error message is set';
    
    return;
};

subtest 'get_comet_backup_configuration_status - returns all configuration 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::get_comet_backup_configuration_status( $args, $metadata );
    
    ok exists $response->{configuration}, 'Response has configuration key';
    ok exists $response->{configuration}->{progress}, 'Response has progress field';
    ok exists $response->{configuration}->{status}, 'Response has status field';
    ok exists $response->{configuration}->{status_message}, 'Response has status_message field';
    ok exists $response->{configuration}->{has_license}, 'Response has has_license field';
    ok exists $response->{configuration}->{agent_installed}, 'Response has agent_installed field';
    ok exists $response->{configuration}->{agent_running}, 'Response has agent_running field';
    
    return;
};

done_testing();
