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

#                                      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 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;
use Cpanel::JSON;

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;

    $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; }

    $INC{'Cpanel/Config/Users.pm'} = 1;
    package Cpanel::Config::Users;
    sub getcpusers { return qw(user1 user2 user3); }

    $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;
    sub new  { return bless {}, shift; }
    sub call { return {}; }

    $INC{'Whostmgr/CometBackup/Constants.pm'} = 1;
    package Whostmgr::CometBackup::Constants;
    our $RUN_LOG_DB = '/nonexistent/db.db';
    sub get_text_log_file_path { return '/var/log/comet.log'; }

    $INC{'Whostmgr/API/1/Utils.pm'} = 1;
    package Whostmgr::API::1::Utils;
    sub set_metadata_ok    { $_[0]->{result} = 1; }
    sub set_metadata_not_ok { $_[0]->{result} = 0; }

    $INC{'Whostmgr/CometBackup/BackupRunLogs.pm'} = 1;
    package Whostmgr::CometBackup::BackupRunLogs;
    sub new { return bless {}, shift; }
    sub get_restorable_items { return (); }

    $INC{'Whostmgr/CometBackup/BackupJobs.pm'} = 1;
    package Whostmgr::CometBackup::BackupJobs;
    sub new { return bless {}, shift; }
    sub get_backup_jobs { return (); }
}

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

my @mock_files = (
    Test::MockFile->file('/usr/local/cpanel/logs/error_log'),
    Test::MockFile->file('/nonexistent/db.db'),
    Test::MockFile->file('/var/cpanel/server_locale'),
);

subtest 'list_accounts_for_restore - missing JSON parameter' => sub {
    my $metadata = {};
    my $args     = {};

    my $result = Whostmgr::API::1::CometBackup::list_accounts_for_restore( $args, $metadata );

    is( $metadata->{result}, 0, 'Metadata result is not ok' );
    ok( !defined $result, 'Result is undef' );
};

subtest 'list_accounts_for_restore - single mode empty results' => sub {
    my $metadata = {};
    my $args     = { json => Cpanel::JSON::Dump( { mode => 'single' } ) };

    my $result = Whostmgr::API::1::CometBackup::list_accounts_for_restore( $args, $metadata );

    is( $metadata->{result}, 1, 'Metadata result is ok' );
    is( $result->{mode}, 'single', 'Mode is single' );
    is( ref $result->{accounts}, 'ARRAY', 'Accounts is an array' );
    is( scalar @{ $result->{accounts} }, 0, 'Accounts list is empty' );
};

subtest 'list_accounts_for_restore - multi mode empty results' => sub {
    my $metadata = {};
    my $args     = { json => Cpanel::JSON::Dump( { mode => 'multi' } ) };

    my $result = Whostmgr::API::1::CometBackup::list_accounts_for_restore( $args, $metadata );

    is( $metadata->{result}, 1, 'Metadata result is ok' );
    is( $result->{mode}, 'multi', 'Mode is multi' );
    is( ref $result->{accounts}, 'ARRAY', 'Accounts is an array' );
};

subtest 'list_accounts_for_restore - defaults to single mode' => sub {
    my $metadata = {};
    my $args     = { json => Cpanel::JSON::Dump( {} ) };

    my $result = Whostmgr::API::1::CometBackup::list_accounts_for_restore( $args, $metadata );

    is( $metadata->{result}, 1, 'Metadata result is ok' );
    is( $result->{mode}, 'single', 'Mode defaults to single' );
};

subtest 'list_accounts_for_restore - invalid mode' => sub {
    my $metadata = {};
    my $args     = { json => Cpanel::JSON::Dump( { mode => 'invalid' } ) };

    my $result = Whostmgr::API::1::CometBackup::list_accounts_for_restore( $args, $metadata );

    is( $metadata->{result}, 0, 'Metadata result is not ok' );
    is( ref $result->{accounts}, 'ARRAY', 'Accounts is an array' );
    is( scalar @{ $result->{accounts} }, 0, 'Accounts list is empty' );
};

done_testing();
