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

#                                      Copyright 2024 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::Plugin::NoWarnings;

use Test::MockModule;
use Test::MockFile;

use Cpanel::Plugins::Promotions::koality ();
use Cpanel::JSON                         ();

use base qw(Test::Class);

my ( @mocks, $remote_response, $expected_company_id );

push( @mocks, Test::MockModule->new('Cpanel::Plugins::RestApiClient') );
$mocks[-1]->redefine( 'run' => sub { return Cpanel::JSON::Dump($remote_response) } );

push( @mocks, Test::MockModule->new('Cpanel::License::CompanyID') );
$mocks[-1]->redefine( 'get_company_id' => sub { return $expected_company_id } );

push( @mocks, Test::MockModule->new('Cpanel::Plugins::Cache') );
$mocks[-1]->redefine( 'setup_cache' => sub { return 1 } );

push( @mocks, Test::MockModule->new('Cpanel::Plugins::Promotions::koality') );
$mocks[-1]->redefine( 'setup_cache' => sub { return 1 } );

my $mock_stage_file = Test::MockFile->file( '/var/cpanel/use_koality_stage', '' );

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

sub test_can_show_promotions : Tests(5) {

    $expected_company_id = '1234';
    $remote_response->{data}{enabled} = '1';

    my $promotions = Cpanel::Plugins::Promotions::koality->new();
    isa_ok( $promotions, 'Cpanel::Plugins::Promotions::koality' );

    is( $promotions->company_id(),          $expected_company_id, "Got the expected company_id." );
    is( $promotions->can_show_promotions(), 1,                    "Got expected result when server can show promotions." );

    $remote_response->{data}{enabled} = '0';
    is( $promotions->can_show_promotions(), 0, "Got expected result when server can not show promotions." );

    $expected_company_id              = undef;
    $remote_response->{data}{enabled} = '1';
    $promotions                       = Cpanel::Plugins::Promotions::koality->new();
    is( $promotions->can_show_promotions(), 0, "Got expected result when server can not find the company id." );

    return;
}

1;
