<?php
namespace Misuzu;

use Index\Db\DbBackends;
use Index\Config\Db\DbConfig;
use Index\Config\Fs\FsConfig;

define('MSZ_STARTUP', microtime(true));
define('MSZ_ROOT', __DIR__);
define('MSZ_CLI', PHP_SAPI === 'cli');
define('MSZ_DEBUG', is_file(MSZ_ROOT . '/.debug'));
define('MSZ_PUBLIC', MSZ_ROOT . '/public');
define('MSZ_SOURCE', MSZ_ROOT . '/src');
define('MSZ_CONFIG', MSZ_ROOT . '/config');
define('MSZ_TEMPLATES', MSZ_ROOT . '/templates');
define('MSZ_MIGRATIONS', MSZ_ROOT . '/database');
define('MSZ_ASSETS', MSZ_ROOT . '/assets');

require_once MSZ_ROOT . '/vendor/autoload.php';

error_reporting(MSZ_DEBUG ? -1 : 0);
mb_internal_encoding('UTF-8');
date_default_timezone_set('GMT');

$cfg = FsConfig::fromFile(MSZ_CONFIG . '/config.cfg');

if($cfg->hasValues('sentry:dsn'))
    (function($cfg) {
        \Sentry\init([
            'dsn' => $cfg->getString('dsn'),
            'traces_sample_rate' => $cfg->getFloat('tracesRate', 0.2),
            'profiles_sample_rate' => $cfg->getFloat('profilesRate', 0.2),
        ]);

        set_exception_handler(function(\Throwable $ex) {
            \Sentry\captureException($ex);
        });
    })($cfg->scopeTo('sentry'));

$db = DbBackends::create($cfg->getString('database:dsn', 'null:'));
$db->execute('SET SESSION time_zone = \'+00:00\', sql_mode = \'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION\';');

$cfg = new DbConfig($db, 'msz_config');

Mailer::init($cfg->scopeTo('mail'));

$msz = new MisuzuContext($db, $cfg);