package CPAN::MetaPackager::Config;

use 5.36.0;
use boolean;
use warnings qw(FATAL utf8); # Fatalize encoding glitches.

use Config::Tiny;

use File::Spec;

use Mojo::Log;
use Moo;

use Types::Standard qw/ArrayRef HashRef Object Str/;

has config =>
(
	default		=> sub{return {} },
	is			=> 'rw',
	isa			=> HashRef,
	required	=> 1,
);

has config_path =>
(
	default		=> sub{return 'data/cpan.metapackager.conf'},
	is			=> 'rw',
	isa			=> Str,
	required	=> 0,
);


has database_path =>
(
	default		=> sub{return 'data/cpan.metapackager.sqlite'},
	is			=> 'rw',
	isa			=> Str,
	required	=> 0,
);

has error =>
(
	default		=> sub{return ''},
	is			=> 'rw',
	isa			=> Str,
	required	=> 0,
);

has home_path =>
(
	default		=> '',
	is			=> 'rw',
	isa			=> Str,
	required	=> 0,
);

# Available log levels are trace, debug, info, warn, error and fatal, in that order.

has log_level =>
(
	default		=> sub{return 'info'},
	is			=> 'rw',
	isa			=> Str,
	required	=> 0,
);

has logger =>
(
	is			=> 'rw',
	isa			=> Object,
	required	=> 0,
);

has separator =>
(
	default		=> '-' x 50,
	is			=> 'ro',
	isa			=> Str,
	required	=> 0,
);

# Warning. Order is important because of foreign key constraints.
# The tables are created in this order, and dropped in reverse order.
# See also Database.build_pad().

has table_names =>
(
	default		=> sub{return [qw/log packages/]},
	is			=> 'rw',
	isa			=> ArrayRef,
	required	=> 0,
);

our $VERSION = '1.01';

# -----------------------------------------------

sub init_config
{
	my($self)				= @_;
	my($path)				= File::Spec -> catfile($self -> home_path, $self -> config_path);
	my($config)				= $self -> config($self -> _init_config($path) );
	$$config{config_path}	= $path;
	$$config{log_path}		= File::Spec -> catfile($self -> home_path, $$config{log_path});

	$self -> config($config);
	$self -> logger(Mojo::Log -> new(level => $self -> log_level, path => $$config{log_path}) );

} # End of init_config.

# -----------------------------------------------

sub _init_config
{
	my($self, $path) = @_;

	# Section: [global].

	my($config) = Config::Tiny -> read($path);

	die 'Error: ' . Config::Tiny -> errstr . "\n" if (Config::Tiny -> errstr);

	# Sections: [localhost] and [webhost].

	my($section);

	for my $i (1 .. 2)
	{
		$section = $i == 1 ? 'global' : $$config{$section}{host};

		$self -> error("Error: Config file '$path' does not contain the section [$section]") if (! $$config{$section});
	}

	return $$config{$section};

}	# End of _init_config.

# --------------------------------------------------

1;

=head1 NAME

CPAN::MetaPackager::Config - Manage the cpan.metapackager.sqlite database

=head1 Instructions

See POD in MetaPackager.pm.

=head1 Author

Ron Savage I<E<lt>ron@savage.net.auE<gt>>.

My homepage: L<https://savage.net.au/>.

=head1 License

Perl 5.

=head1 Copyright

Australian copyright (c) 2025, Ron Savage.
	All Programs of mine are 'OSI Certified Open Source Software';
	you can redistribute them and/or modify them under the terms of
	The Artistic License V 2, a copy of which is available at:
	http://www.opensource.org/licenses/index.html

=cut
