#!/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 {}; }
    sub get_user_profile_and_hash { return { Profile => { Destinations => {} } }; }
    sub get_destination_ids { return []; }

    $INC{'Whostmgr/CometBackup/Constants.pm'} = 1;
    package Whostmgr::CometBackup::Constants;
    our $RUN_LOG_DB = '/tmp/backup_runs.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 $db_file = '/tmp/backup_runs.db';
my @mock_files = (
    Test::MockFile->file('/usr/local/cpanel/logs/error_log'),
    Test::MockFile->file('/var/cpanel/server_locale'),
    Test::MockFile->file($db_file),
);

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

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

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

subtest 'list_backups_for_user - missing username' => sub {
    my $metadata = {};
    my $args     = { json => Cpanel::JSON::Dump( {} ) };

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

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

subtest 'list_backups_for_user - response structure' => sub {
    my $metadata = {};
    my $args     = { json => Cpanel::JSON::Dump( { username => 'user2' } ) };

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

    ok( exists $result->{backups}, 'Response has backups key' );
    ok( exists $result->{username}, 'Response has username key' );
    is( ref $result->{backups}, 'ARRAY', 'backups is array' );
    is( $result->{username}, 'user2', 'username is correct' );
};

subtest 'list_backups_for_user - valid username' => sub {
    my $metadata = {};
    my $args     = { json => Cpanel::JSON::Dump( { username => 'user1' } ) };

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

    is( $metadata->{result}, 1, 'Metadata result is ok' );
    is( $result->{username}, 'user1', 'username in response' );
};

subtest 'list_backups_for_user - database error handling' => sub {
    my $mock_logs = Test::MockModule->new('Whostmgr::CometBackup::BackupRunLogs');
    $mock_logs->redefine( 'get_restorable_items' => sub {
        die "no such table: backup_runs";
    } );

    my $metadata = {};
    my $args     = { json => Cpanel::JSON::Dump( { username => 'user1' } ) };

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

    is( $metadata->{result}, 1, 'Metadata result is ok (empty backups treated as ok)' );
    is( scalar @{ $result->{backups} }, 0, 'backups is empty on error' );
};

done_testing();
