#!/usr/bin/env perl
use Mojo::Base -strict, -signatures;
use Mojo::Util qw(sha1_sum encode);
my $home      = $app->home;
my $mode      = $app->mode;
my $moniker   = $app->moniker;
my $resources = $app->resources;
my $rmf       = $resources->child("etc/routes.$mode.conf");


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 their 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.
  load_plugins => [
    $mode =~ /^dev/ ? {PODViewer => {default_module => 'Slovo'}} : (),

    # In CGI mode startup and runtime are the same thing, so we check here if we
    # need this plugin and load it to handle the request.
    $ENV{GATEWAY_INTERFACE} ? 'CGI' : (),
    {MojoDBx => {auto_migrate => 0}},
    {
      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 => (-e $rmf ? $rmf : $resources->child("etc/routes.conf"))}},
    {
      'Minion::Admin' =>
        sub { {
        route => $app->routes->lookup('home_minion'), return_to => 'home_upravlenie',
        } }
    },
    {
      Minion => sub { {SQLite => $app->dbx} }
    },

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

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

    # Themes. The precedence is depending on the order here.
    "Themes::Malka"
  ],

  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 => $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,
}