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 File::Temp;
use File::Slurper;

use Test::Cpanel::Policy ();

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

my $temp_dir = File::Temp->newdir();
my $yaml     = <<~ "YAML";
    ---
    markup: Welcome to the muppet show!
    YAML

File::Slurper::write_text( "$temp_dir/minimal.yaml", $yaml );

$yaml = <<~ "YAML";
    ---
    name: muppet-show
    description: A weekly variety show run by muppets
    source: Jim Henson
    keywords:
    - muppets
    - guest-stars
    markup: its inspirational...
    js: console.log('go muppets!!!')
    js_url: http://muppets.com/stage.js
    css: ".curtain { color: 'red' }"
    css_url: http://muppets.com/stage.css
    meta: from the 80s
    slot: stage-left
    process: 1
    is_enabled: 0
    priority: 5
    YAML

File::Slurper::write_text( "$temp_dir/everything.yaml", $yaml );

subtest 'Test Minimal YAML' => sub {

    my $component = Cpanel::Plugins::Components::YAML->new("$temp_dir/minimal.yaml");

    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,      'Welcome to the muppet show!', "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->process,     0,                            "Process is false by default";
    is $component->is_enabled,  1,                            "Is Enabled is true by default";
    is $component->priority,    1,                             "Priority is 1 by default";
};

subtest 'Test Maximal YAML' => sub {
    my $component = Cpanel::Plugins::Components::YAML->new("$temp_dir/everything.yaml");

    is $component->name,        'muppet-show',                          "Name is correct.";
    is $component->description, 'A weekly variety show run by muppets', "Description is correct.";
    is $component->source,      'Jim Henson',                           "Source is correct.";
    is $component->keywords,    [ "muppets", "guest-stars" ],           "Keywords  is correct.";
    is $component->markup,      'its inspirational...',                 "Markup is correct.";
    is $component->js,          "console.log('go muppets!!!')",         "JS is correct.";
    is $component->js_url,      "http://muppets.com/stage.js",          "JS URL is correct.";
    is $component->css,         ".curtain { color: 'red' }",            "CSS is correct.";
    is $component->css_url,     "http://muppets.com/stage.css",         "CSS URL is correct.";
    is $component->meta,        "from the 80s",                         "Meta is correct.";
    is $component->slot,        'stage-left',                           "Slot is correct.";
    is $component->process,     1,                                     "Process is true now.";
    is $component->is_enabled,  0,                                     "Is Enabled is false now.";
    is $component->priority,    5,                                      "Priority is 5 ";
};

done_testing;
