<?php

declare(strict_types=1);

use Core\Database;
use Core\Session;

require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/bootstrap/helpers.php';

loadEnv(__DIR__ . '/.env');

date_default_timezone_set(env('APP_TIMEZONE', 'America/Sao_Paulo'));

$requestTimeoutSeconds = \Core\RequestLimits::requestTimeoutSeconds();
@ini_set('max_execution_time', (string) $requestTimeoutSeconds);
@ini_set('max_input_time', (string) $requestTimeoutSeconds);
if (function_exists('set_time_limit')) {
    @set_time_limit($requestTimeoutSeconds);
}

if (session_status() === PHP_SESSION_NONE) {
    $sessionCookieParams = session_get_cookie_params();
    $isHttps = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off')
        || (isset($_SERVER['SERVER_PORT']) && (int) $_SERVER['SERVER_PORT'] === 443);

    session_set_cookie_params([
        'lifetime' => (int) ($sessionCookieParams['lifetime'] ?? 0),
        'path' => (string) ($sessionCookieParams['path'] ?? '/'),
        'domain' => (string) ($sessionCookieParams['domain'] ?? ''),
        'secure' => $isHttps,
        'httponly' => true,
        'samesite' => 'Lax',
    ]);

    session_start();
}

Session::sweepFlash();

Database::boot([
    'host' => env('DB_HOST', '127.0.0.1'),
    'port' => env('DB_PORT', '3306'),
    'database' => env('DB_NAME', 'ficha_admissao'),
    'username' => env('DB_USER', 'root'),
    'password' => env('DB_PASS', ''),
    'charset' => 'utf8mb4',
]);
