#!/usr/bin/env perl
use strict;
use warnings;

# use Test::DescribeMe qw(extended);
#
# No Test::Needs gate: Params::Validate::Strict is a hard PREREQ_PM of
# App::Test::Generator itself (see Makefile.PL), not an optional test
# dependency -- use_ok() below already fails loudly if it is missing.
use Test::Most;
use File::Temp qw(tempdir);
use File::Spec;

# Load the module
BEGIN {
	use_ok('App::Test::Generator::SchemaExtractor');
}

# Helper to create a temporary Perl module file
sub create_test_module {
	my ($content) = @_;
	my $dir = tempdir(CLEANUP => 1);
	my $file = File::Spec->catfile($dir, 'TestModule.pm');
	open my $fh, '>', $file or die "Cannot create $file: $!";
	print $fh $content;
	close $fh;
	return $file;
}

# Helper to create an extractor for testing
sub create_extractor {
	my ($module_content) = @_;
	my $module_file = create_test_module($module_content);
	return App::Test::Generator::SchemaExtractor->new(
		input_file => $module_file,
		output_dir => tempdir(CLEANUP => 1),
		verbose	=> 0,
	);
}

# Basic default value patterns
subtest 'Extact schema from Params::Validate::Strict' => sub {
	my $module = <<'END_MODULE';
use Params::Validate::Strict qw(validate_strict);

sub format_user {
    my $args = validate_strict(
        args => @_,
        schema => {
            username => {
                type     => 'string',
                optional => 0,
            },
            roles => {
                type     => 'arrayref',
                optional => 1,
                default  => [],
            },
            uppercase => {
                type     => 'boolean',
                optional => 1,
                default  => 0,
            },
        }
    );

    my $name = $args->{username};
    $name = uc($name) if $args->{uppercase};

    return join(':', $name, @{ $args->{roles} });
}
END_MODULE

	my $extractor = create_extractor($module);

	# Extract all schemas
	my $schemas = $extractor->extract_all();

	ok(defined($schemas));

	# use Data::Dumper;
	# diag(Dumper($schemas));

	# Verify we determined the input for format_user
	my $format_user_schema = $schemas->{format_user};
	ok($format_user_schema, 'Found format_user method schema');

	my $format_user_input = $format_user_schema->{input};
	ok($format_user_input, 'Found input method schema');

	cmp_deeply($format_user_input,  {
		'uppercase' => {
			'optional' => 1,
			'default' => 0,
			'type' => 'boolean'
		}, 'username' => {
			'type' => 'string',
			'optional' => 0
		}, 'roles' => {
			'optional' => 1,
			'type' => 'arrayref',
			'default' => []
		}
	});

	done_testing();
};

done_testing();
