<?php
declare(strict_types=1);

define('APP_ROOT', dirname(__DIR__));
define('CONFIG_FILE', APP_ROOT . '/config/config.php');

if (!is_file(CONFIG_FILE)) {
    header('Location: install/');
    exit;
}

$config = require CONFIG_FILE;
date_default_timezone_set('Asia/Riyadh');

if (session_status() !== PHP_SESSION_ACTIVE) {
    $secure = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off');
    session_name('rakaez_session');
    session_set_cookie_params([
        'lifetime' => 0,
        'path' => '/',
        'secure' => $secure,
        'httponly' => true,
        'samesite' => 'Lax',
    ]);
    session_start();
}

require_once APP_ROOT . '/app/functions.php';

if (!empty($_SESSION['user'])) {
    if (!empty($_SESSION['last_activity']) && time() - (int)$_SESSION['last_activity'] > 7200) {
        $_SESSION = [];
        session_destroy();
        header('Location: ' . rtrim((string)$config['app_url'], '/') . '/login.php?expired=1');
        exit;
    }
    $_SESSION['last_activity'] = time();
}
