#!/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 Example1;

use cPstrict;
use Moo;

extends 'Cpanel::Plugins::Components::Base';

has "+markup" => ( default => '<a>hello</a>' );

package Example2;

use cPstrict;
use Moo;

extends 'Cpanel::Plugins::Components::Base';

has '+name' => (
    default => "name",
);

has '+description' => (
    default => "description",
);

has '+source' => (
    default => 'source',
);

has '+keywords' => (
    default => sub { [qw(name description source)] },
);

has '+markup' => (
    default => 'markup',
);

has '+js' => (
    default => 'console.log("hello");',
);

has '+css' => (
    default => '.class { color: red }',
);

has '+meta' => (
    default => 'fullscreen=1',
);

has '+slot' => (
    default => 'complex',
);

package Example3;

use cPstrict;
use Moo;

extends 'Cpanel::Plugins::Components::Base';

has '+markup' => (
    default => 'markup',
);

package Example4;

use cPstrict;
use Moo;

extends 'Cpanel::Plugins::Components::Base';
with 'Cpanel::Plugins::Components::Role::Template';

has '+process' => ( default => 1 );

has '+markup' => (
    default => 'markup[% a %]',
);

package main;

use cPstrict;

use Test2::V0;
use Test2::Tools::Explain;
use Test2::Plugin::NoWarnings;
use Test2::Tools::Subtest;
use Test2::Tools::Exception;

use Test::MockModule qw/strict/;
use Test::MockFile   qw/nostrict/;

use Test::Cpanel::Policy ();
use Capture::Tiny;

use Cpanel::Plugins::Components::Base ();

subtest 'Test Basic Class Inheritance' => sub {

    my $component = Example1->new();
    is $component->name,        '',             "Name is an empty string by default";
    is $component->description, '',             "Description is an empty string by default";
    is $component->source,      '',             "Source is an empty string by default";
    is $component->keywords,    [],             "Keywords is an empty array by default";
    is $component->markup,      '<a>hello</a>', "Markup is overridden";
    is $component->js,          undef,          "JS is undef by default";
    is $component->js_url,      undef,          "JS url is undef by default";
    is $component->css,         undef,          "CSS is undef by default";
    is $component->css_url,     undef,          "CSS url is undef by default";
    is $component->meta,        undef,          "Meta is undef bydefault";
    is $component->slot,        'default',      "Slot is 'default' by default";
    is $component->is_enabled,  1,             "Is Enabled is true by default";
    is $component->priority,    1,              "Priority is 1 by default";
};

subtest 'Test Class Inheritance with most overrides' => sub {
    my $component = Example2->new();
    is $component->name,        'name',                        "Name is 'name'";
    is $component->description, 'description',                 "Description is 'description'";
    is $component->source,      'source',                      "Source is 'source'";
    is $component->keywords,    [qw/name description source/], "Keywords array is filled";
    is $component->markup,      'markup',                      "Markup is 'markup'";
    is $component->js,          'console.log("hello");',       "JS has overrides";
    is $component->js_url,      undef,                         "JS url is undef by default";
    is $component->css,         '.class { color: red }',       "CSS has overrides";
    is $component->css_url,     undef,                         "CSS url is undef by default";
    is $component->meta,        'fullscreen=1',                "Meta has overrides";
    is $component->slot,        'complex',                     "Slot is overridden";
    is $component->is_enabled,  1,                            "Is Enabled is true by default";
    is $component->priority,    1,                             "Priority is 1 by default";
};

subtest 'Test render without tt' => sub {
    my $component = Example3->new();
    my $output    = $component->render( {}, {} );
    is $output, 'markup', "No template processing of markup";
};

subtest 'Test render with tt' => sub {
    my $component = Example4->new();
    my $output    = $component->render( { a => "a" }, {} );
    is $output, 'markupa', "Has template processing of markup";
};

done_testing;
