<?php
$host = 'localhost';
$db   = 'carsfory_locis';
$user = 'carsfory_locis';
$pass = '8QYP2!S?ONk1HQ5_';
$charset = 'utf8mb4';

$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$options = [
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES   => false,
];

try {
    $pdo = new PDO($dsn, $user, $pass, $options);
} catch (PDOException $e) {
    http_response_code(500);
    exit('Database connection failed. Check config.php and import schema.sql.');
}

$branches = [
    'ZAD_AP' => 'Zadar Airport',
    'ZAD_DT' => 'Zadar Downtown',
    'SPU_AP' => 'Split Airport',
    'ZAG_AP' => 'Zagreb Airport',
    'PUY_AP' => 'Pula Airport',
    'DBV_AP' => 'Dubrovnik Airport',
    'SIB'    => 'Šibenik',
    'MUR'    => 'Murter'
];

/*
 * EXISTING ADMIN LOGIN INTEGRATION
 * --------------------------------
 * This project does not create a second admin login.
 * When you place it behind your existing Avia admin, set this to true and
 * change the session key/value below to match your existing login session.
 */
$useExistingAdminSession = true;
$existingAdminSessionKey = 'uloga';
$existingAdminSessionValue = 'admin';

function require_existing_admin(): void
{
    global $useExistingAdminSession, $existingAdminSessionKey, $existingAdminSessionValue;
    if (!$useExistingAdminSession) {
        return;
    }
    if (session_status() !== PHP_SESSION_ACTIVE) {
        session_start();
    }
    if (!isset($_SESSION[$existingAdminSessionKey]) || $_SESSION[$existingAdminSessionKey] != $existingAdminSessionValue) {
        http_response_code(403);
        exit('Access denied. Please sign in through the existing Avia admin.');
    }
}

function e(?string $value): string
{
    return htmlspecialchars((string)$value, ENT_QUOTES, 'UTF-8');
}
