#!/usr/bin/env perl
use Mojo::Base -strict, -signatures;
use feature qw(unicode_strings);
use Mojo::Util qw(sha1_sum encode);
my $home             = $app->home;
my $mode             = $app->mode;
my $moniker          = $app->moniker;
my $rsc              = $app->resources;
my $mode_routes_file = $rsc->child("etc/routes.$mode.conf");
my $routes_file
  = (-e $mode_routes_file ? $mode_routes_file : $rsc->child("etc/routes.conf"));
my $db_file = $rsc->child("data/$moniker.$mode.sqlite")->to_string;
$app->defaults(

  #layout => 'default'
  'ѩꙁыкъ' => 'bg-bg'
);
$app->load_class('Slovo::Controller::Auth');

my $mail_cfg = {
  token_valid_for => 24 * 3600,
  'Net::SMTP'     => {
    new => {
      Host => 'mail.example.org',

      #Debug          => 1,
      SSL             => 1,
      SSL_version     => 'TLSv1',
      SSL_verify_mode => 0,
      Timeout         => 60,
    },
    auth => ['slovo@example.org', 'Pa55w03D'],
    mail => 'slovo@example.org',
  },
};

{
  # Hypnotoad Settings (optimized for mostly blocking operations)
  # See /perldoc/Mojo/Server/Hypnotoad#SETTINGS
  # and /perldoc/Mojolicious/Guides/Cookbook#Hypnotoad
  hypnotoad => {
    accepts          => 200,
    graceful_timeout => 15,
    listen           => ['http://127.0.0.1:9090', 'http://[::1]:9090'],
    proxy            => 1,
    pid_file         => -d $home->child('bin')
    ? $home->child('bin',    $moniker . '.pid')
    : $home->child('script', $moniker . '.pid'),
    spare   => 4,
    workers => 10,
    clients => 2,
  },

  # Some classes which plugins or the application expect to be loaded to enable
  # some functionality or to refer to its functions.
  load_classes => [qw()],

# Plugins can be Mojolicious and Slovo plugins.
# Every Slovo::Plugin:: ISA Mojolicious::Plugin
# Plugin order is important. Any plugin depending on another
# must come after the plugin it depends on.
# A plugin may be loaded twice if it will do different
# things depending on configuration variables.
  plugins => [
    $mode =~ /^dev/ ? {PODViewer => {default_module => 'Slovo'}} : (),
    {
      MojoDBx => {
        adaptor       => 'SQLite',
        new           => $db_file,
        sql_debug     => 0,          #4,
        on_connection => [
          'PRAGMA synchronous = OFF', 'PRAGMA foreign_keys = ON',
          'PRAGMA cache_size = 80000',    #80M cache size

#        sub($dbh) {
#          $app->log->debug('SQLite version: '
#                    . $dbh->selectrow_arrayref('select sqlite_version()')->[0]);
#
#          # $dbh->{TraceLevel} = "3|SQL";
#        }
        ],
        max_connections => 3,
        auto_migrate    => 1,
        migration_file  => $rsc->child("data/migrations.sql")->to_string,

        # Which helpers for Models to load:
        # Slovo::Model::Users,Slovo::Model::Groups... etc.
        tables => ['users', 'groups', 'domove', 'stranici', 'celini'],
      }
    },
    {
      Authentication => {
        autoload_user   => 1,
        session_key     => 'u',
        current_user_fn => Slovo::Controller::Auth::current_user_fn(),
        load_user       => \&Slovo::Controller::Auth::load_user,
        validate_user   => \&Slovo::Controller::Auth::validate_user,
      }
    },
    {RoutesConfig => {file => $routes_file}},
    {
      'Minion::Admin' =>
        sub { {
        route => $app->routes->lookup('home_minion'), return_to => 'home_upravlenie',
        } }
    },
    {
      Minion => sub { {SQLite => $app->dbx} }
    },

    {OpenAPI => {url => $app->resources->child("api-v1.0.json")->to_string}},

    #Tasks
    {'Task::SendOnboardingEmail' => $mail_cfg},
    {'Task::SendPasswEmail'      => $mail_cfg},
  ],

  secrets => [sha1_sum(encode('utf8', $home . 'тайна')),],

  # See also /perldoc/Mojolicious/Sessions
  sessions => [

    #attribute => value
    {default_expiration => 3600 * 24 * 5},    #five days
    {cookie_name        => 'slovo'},
  ],

  # Root folder where domain specific files will reside. Each domain has it's
  # own folder there.
  domove_root => $app->home->child('domove')->to_string,

  # Cache published pages for non authenticated users
  cache_pages => 1,

  # $mode =~ /^dev/ ? () : (cache_pages => 1),
  # Cache-Control header for non authenticated users. For authenticated users
  # the max-age is the same, but 'public' is replaced with 'private'.
  cache_control        => 'public, max-age=' . (24 * 3600),    # 1 day
  response_compression => 0,
}