use strict;
use warnings;

use Config::Model;

use Config::Model::Tester::Setup qw/init_test setup_test_dir/;
use Path::Tiny;
use 5.10.0;

use Test::More;
use Test::Exception;
use Software::LicenseMoreUtils;
use Test::LongString;


my ($model, $trace, $opts) = init_test('rdhint','rdtrace');

{
    no warnings qw/once/;
    $::RD_HINT  = 1 if $opts->{rdhint};
    $::RD_TRACE = 1 if $opts->{rdtrace};
}

my $wr_dir = setup_test_dir();

$wr_dir->child("debian")->mkpath() ;

subtest "Main license text" => sub {
    my $root = $model->instance (
        root_class_name => 'Dpkg::Copyright',
        root_dir        => $wr_dir,
        instance_name   => "license-text",
    )->config_root;

    my $art_2_text = Software::LicenseMoreUtils->new_from_short_name({
        short_name =>'Artistic-2',
    })->summary_or_text;
    chomp($art_2_text);
    $art_2_text =~ s/\t/    /g;
    $art_2_text =~ s!\n +\n!\n\n!g;
    is_string($root->grab_value("License:Artistic-2 text"), $art_2_text, "check that license text was not reformatted");
};

subtest "complex license short names" => sub {
    # instance to check one dependency at a time
    my $root = $model->instance (
        root_class_name => 'Dpkg::Copyright',
        root_dir        => $wr_dir,
        instance_name   => "short-name-test",
    )->config_root;

    my $lic = $root->fetch_element('License');

    # create required main licenses
    my $gpl3_ex = "GPL-3+ with Font exception";
    foreach my $name (qw/GPL-3+ GPL-2+ Artistic-2.0 BSD-3-clause Expat/, $gpl3_ex) {
        ok($lic->fetch_with_id($name),"fetch «$name» license object");
    }

    # this license index is not a valid short name. But the check is done
    # by short name object, not by this hash element
    ok($lic->fetch_with_id("GPL-3+ with Font-exception-2.0"), "check license with bad font exception");

    print $root->dump_tree if $trace;

    my $short = $root->grab('Files:"etc/fonts/*" License short_name');
    ok($short, "got short_name object");

    foreach my $name ("GPL-3+",$gpl3_ex, "GPL-2+ or Artistic-2.0, and BSD-3-clause") {
        ok($short->store($name),"store «$name» short name");
        is($short->has_warning,0,"short name triggers has no warning");
    }

    my $andor = "BSD-3-clause and/or Expat";
    is($short->has_warning,0,"short name triggers has no warning");
    ok($short->store($andor),"store «$andor» short name");
    ok($short->has_warning,"«$andor» short name triggered a warning");


    throws_ok {
        $short->store("GPL-3+ with Font-exception-2.0");
    } qr/does not match grammar from model/, "bad short name rejected when storing";
};

# test for https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1129476
subtest "rename MIT to Expat in a complex license" => sub {
    # license short_name is "MIT or CC0-1.0" should be renamed to "Expat or CC0-1.0"
    # and the matching license entry should be renamed to Expat as well
    my $root = $model->instance (
        root_class_name => 'Dpkg::Copyright',
        root_dir        => $wr_dir,
        instance_name   => "expat-cc-rename",
    )->config_root;

    # load main licenses
    my $mit = $root->load('License:MIT text="MIT lic" ! License:"CC0-1.0" text="CC0 lic"');

    # create composed license short_name
    my $short = $root->grab('Files:foo.cc License short_name');
    my $orlic = 'MIT or CC0-1.0';
    $short->store($orlic);
    print $root->dump_tree if $trace;
    is($short->has_warning,1,"«$orlic» short name triggered a warning");

    ok($root->apply_fixes, "applied fix");

    my @ids = $root->fetch_element('License')->fetch_all_indexes;
    is_deeply(\@ids, ['CC0-1.0','Expat'], "renamed License");
    is($short->fetch(), 'Expat or CC0-1.0', 'renamed short_name');
};

done_testing();
