package AccessCheck::App;

use Mojo::Base qw(Mojolicious);

use English qw(-no_match_vars);
use Template::Constants qw(:chomp);
use Syntax::Keyword::Try;

use AccessCheck::Data::DB;

use constant {
    ACCESSCHECK_VERSION => '2.2.0'
};

sub startup {
    my $self = shift;

    $self->helper(
        string_to_list => sub {
            my $self = shift;
            my $value = shift;

            return defined $value ? split(/, */, $value) : ();
        }
    );

    $self->plugin('INIConfig', { file => $ENV{ACCESS_CHECK_CONFIG} || 'conf/manager.conf' });

    my $config = $self->config();

    $self->plugin(
        'TemplateToolkit',
        {
            name     => 'tt2',
            template => {
                ABSOLUTE     => 1,
                ENCODING     => 'utf8',
                PRE_CHOMP    => CHOMP_ONE,
                PLUGIN_BASE => 'AccessCheck::Template::Plugin',
            }
        }
    );

    $self->plugin(
        'ForwardedFor',
    );

    $self->log(
        Mojo::Log->new(
            path  =>  $config->{logger}->{file},
            level => ($config->{logger}->{level} || 'trace')
        )
    );

    AccessCheck::Data::DB->register_db(
        driver   => $config->{database}->{type},
        database => $config->{database}->{name},
        host     => $config->{database}->{host},
        password => $config->{database}->{password},
        username => $config->{database}->{username},
        options  => $config->{database}->{options}
    );

    my $theme = $config->{setup}->{templates_theme} || 'default';
    my $base_templates_dir  = $self->home()->child('templates');

    my $renderer = $self->renderer();
    $renderer->default_handler('tt2');
    $renderer->paths([
        $base_templates_dir->child('web', $theme),
        $base_templates_dir->child('web'),
        $base_templates_dir->child('accounts'),
        $base_templates_dir->child('other'),
    ]);

    $self->defaults(
        app => {
            support_url   => $config->{app}->{support_url},
            support_email => $config->{app}->{support_email},
            login_url     => $config->{app}->{login_url},
            logout_url    => $config->{app}->{logout_url},
            name          => $config->{app}->{name},
            version       => ACCESSCHECK_VERSION
        },
    );

    my $routes = $self->routes();

    $routes->get('/')->to(controller => 'controller', action => 'home')->name('home');
    $routes->get('/status')->to(controller => 'controller', action => 'status')->name('status');
    $routes->get('/select_entity')->to(controller => 'controller', action => 'select_entity')->name('select_entity');
    $routes->get('/select_email')->to(controller => 'controller', action => 'select_email')->name('select_email');
    $routes->get('/send_challenge')->to(controller => 'controller', action => 'send_challenge')->name('send_challenge');
    $routes->get('/validate_challenge')->to(controller => 'controller', action => 'validate_challenge')->name('validate_challenge');
    $routes->get('/show_accounts_html')->to(controller => 'controller', action => 'show_accounts_html')->name('show_accounts_html');
    $routes->get('/show_accounts_csv')->to(controller => 'controller', action => 'show_accounts_csv')->name('show_accounts_csv');

}

1;