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

#                                      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.

package t::medium::Cpanel::Sitejet::MoveWebsite::UpdateAssets;

use cPstrict;

use Test2::V0 -no_utf8 => 1;
use Test2::Tools::Explain;
use Test2::Plugin::NoWarnings;
use Test2::Tools::Exception qw/dies/;

use Test::MockFile;
use Test::MockObject;
use Test::MockTime;
use Test::Warn;

use Try::Tiny;

use Test::Cpanel::Policy;

# must go below T:C:Policy
use Mock::Cpanel::Logger;

use parent qw(
  Cpanel::TestObj
);

use Cpanel();

use Cpanel::Sitejet::MoveWebsite ();

__PACKAGE__->new()->runtests() if !caller;

#----------------------------------------------------------------------

#e.g., num_method_tests()
sub new ( $class, @args ) {
    my $self = $class->SUPER::new(@args);
    return $self;
}

#e.g., set environs before running tests
sub runtests ( $self, @args ) {
    return $self->SUPER::runtests(@args);
}

sub test_customer_company_field_updated_when_matching_old_domain : Tests(5) ($self) {
    my $mocks = $self->_get_mocks();
    local $Cpanel::user    = $mocks->{'user'};
    local $Cpanel::homedir = $mocks->{'homedir'};

    my $mock_assets = {
        'customer.company' => $mocks->{'old_domain'},
        'customer.email'   => 'test@example.com',
        'customer.city'    => 'Test City',
    };

    my $updated_assets_called = 0;
    my $captured_updates      = {};

    my @mocks = (
        $self->mock_subs(
            'Cpanel::Sitejet::Connector::_check_domain_config' => sub { return; },
            'Cpanel::Sitejet::Connector::getWebsiteAssets'     => sub {
                my ( $self, $domain ) = @_;
                is( $domain, $mocks->{'old_domain'}, 'getWebsiteAssets called with old domain' );
                return $mock_assets;
            },
            'Cpanel::Sitejet::Connector::updateWebsiteAssets' => sub {
                my ( $self, $domain, $updates ) = @_;
                is( $domain, $mocks->{'old_domain'}, 'updateWebsiteAssets called with old domain' );
                $captured_updates      = $updates;
                $updated_assets_called = 1;
                return { success => 1 };
            },
        )
    );

    my $move_obj = Cpanel::Sitejet::MoveWebsite->new( $mocks->{'old_domain'}, $mocks->{'new_domain'} );
    my $result   = $move_obj->update_website_assets_domain();

    is( $result,                                 1,                      'Method returns success' );
    is( $updated_assets_called,                  1,                      'updateWebsiteAssets was called' );
    is( $captured_updates->{'customer.company'}, $mocks->{'new_domain'}, 'customer.company updated to new domain' );

    return;
}

sub test_customer_company_field_not_updated_when_not_matching_old_domain : Tests(2) ($self) {
    my $mocks = $self->_get_mocks();
    local $Cpanel::user    = $mocks->{'user'};
    local $Cpanel::homedir = $mocks->{'homedir'};

    my $mock_assets = {
        'customer.company' => 'Custom Company Name',
        'customer.email'   => 'test@example.com',
        'customer.city'    => 'Test City',
    };

    my $updated_assets_called = 0;

    my @mocks = (
        $self->mock_subs(
            'Cpanel::Sitejet::Connector::_check_domain_config' => sub { return; },
            'Cpanel::Sitejet::Connector::getWebsiteAssets'     => sub {
                my ( $self, $domain ) = @_;
                return $mock_assets;
            },
            'Cpanel::Sitejet::Connector::updateWebsiteAssets' => sub {
                $updated_assets_called = 1;
                return { success => 1 };
            },
        )
    );

    my $move_obj = Cpanel::Sitejet::MoveWebsite->new( $mocks->{'old_domain'}, $mocks->{'new_domain'} );
    my $result   = $move_obj->update_website_assets_domain();

    is( $result,                1, 'Method returns success' );
    is( $updated_assets_called, 0, 'updateWebsiteAssets was not called' );

    return;
}

sub test_other_fields_with_old_domain_are_updated : Tests(5) ($self) {
    my $mocks = $self->_get_mocks();
    local $Cpanel::user    = $mocks->{'user'};
    local $Cpanel::homedir = $mocks->{'homedir'};

    my $mock_assets = {
        'customer.company' => $mocks->{'old_domain'},
        'customer.email'   => 'contact@' . $mocks->{'old_domain'},
        'some.url.field'   => 'https://' . $mocks->{'old_domain'} . '/path',
    };

    my $updated_assets_called = 0;
    my $captured_updates      = {};

    my @mocks = (
        $self->mock_subs(
            'Cpanel::Sitejet::Connector::_check_domain_config' => sub { return; },
            'Cpanel::Sitejet::Connector::getWebsiteAssets'     => sub {
                return $mock_assets;
            },
            'Cpanel::Sitejet::Connector::updateWebsiteAssets' => sub {
                my ( $self, $domain, $updates ) = @_;
                $captured_updates      = $updates;
                $updated_assets_called = 1;
                return { success => 1 };
            },
        )
    );

    my $move_obj = Cpanel::Sitejet::MoveWebsite->new( $mocks->{'old_domain'}, $mocks->{'new_domain'} );
    my $result   = $move_obj->update_website_assets_domain();

    is( $result,                                 1,                                             'Method returns success' );
    is( $updated_assets_called,                  1,                                             'updateWebsiteAssets was called' );
    is( $captured_updates->{'customer.company'}, $mocks->{'new_domain'},                        'customer.company updated' );
    is( $captured_updates->{'customer.email'},   'contact@' . $mocks->{'new_domain'},           'customer.email updated' );
    is( $captured_updates->{'some.url.field'},   'https://' . $mocks->{'new_domain'} . '/path', 'URL field updated' );

    return;
}

sub test_no_assets_found : Tests(1) ($self) {
    my $mocks = $self->_get_mocks();
    local $Cpanel::user    = $mocks->{'user'};
    local $Cpanel::homedir = $mocks->{'homedir'};

    my @mocks = (
        $self->mock_subs(
            'Cpanel::Sitejet::Connector::_check_domain_config' => sub { return; },
            'Cpanel::Sitejet::Connector::getWebsiteAssets'     => sub {
                return undef;
            },
        )
    );

    my $move_obj = Cpanel::Sitejet::MoveWebsite->new( $mocks->{'old_domain'}, $mocks->{'new_domain'} );
    my $result   = $move_obj->update_website_assets_domain();

    is( $result, 1, 'Method returns success when no assets found' );

    return;
}

sub _get_mocks ($self) {
    my %mocks = ();

    # Test data for MoveWebsite update_website_assets_domain method
    # Provides mock domains and basic test setup

    $mocks{'old_domain'} = 'old-domain.com';
    $mocks{'new_domain'} = 'new-domain.com';
    $mocks{'user'}       = 'testuser';
    $mocks{'homedir'}    = '/tmp/test_home';

    return \%mocks;
}

1;
