<?php
declare(strict_types=1);

function app_request_is_secure(): bool
{
    if (!empty($_SERVER['HTTPS'])) {
        $https = strtolower((string) $_SERVER['HTTPS']);
        if ($https !== 'off' && $https !== '0') {
            return true;
        }
    }

    return (int) ($_SERVER['SERVER_PORT'] ?? 0) === 443;
}

function runtime_schema_bootstrap_version(): string
{
    return '2026-05-30-1';
}

function runtime_schema_bootstrap_marker_path(): string
{
    return dirname(__DIR__) . '/storage/settings/runtime_schema_bootstrap.json';
}

function runtime_schema_bootstrap_is_current(): bool
{
    static $cached = null;

    if (is_bool($cached)) {
        return $cached;
    }

    $path = runtime_schema_bootstrap_marker_path();
    if (!is_file($path) || !is_readable($path)) {
        $cached = false;

        return $cached;
    }

    $contents = @file_get_contents($path);
    if (!is_string($contents) || trim($contents) === '') {
        $cached = false;

        return $cached;
    }

    $decoded = json_decode($contents, true);
    $cached = is_array($decoded)
        && (string) ($decoded['version'] ?? '') === runtime_schema_bootstrap_version();

    return $cached;
}

function mark_runtime_schema_bootstrap_complete(): void
{
    $path = runtime_schema_bootstrap_marker_path();
    $directory = dirname($path);

    if (!is_dir($directory)) {
        @mkdir($directory, 0777, true);
    }

    $payload = array(
        'version' => runtime_schema_bootstrap_version(),
        'completed_at' => date('c'),
    );

    $encoded = json_encode($payload, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
    if (is_string($encoded)) {
        @file_put_contents($path, $encoded, LOCK_EX);
    }
}

function ensure_runtime_schema_bootstrap(mysqli $conn): void
{
    static $bootstrapped = false;

    if ($bootstrapped) {
        return;
    }

    $bootstrapped = true;

    if (runtime_schema_bootstrap_is_current()) {
        return;
    }

    ensure_service_category_schema($conn);
    ensure_service_catalog_schema($conn);
    ensure_customer_group_column($conn);
    ensure_auth_schema($conn);
    ensure_audit_log_schema($conn);
    ensure_order_reporting_indexes($conn);
    ensure_system_maintenance_schema($conn);
    normalize_branch_names_in_database($conn);
    mark_runtime_schema_bootstrap_complete();
}

if (session_status() === PHP_SESSION_NONE) {
    $sessionPath = dirname(__DIR__) . '/storage/sessions';

    session_set_cookie_params(array(
        'lifetime' => 0,
        'path' => '/',
        'secure' => app_request_is_secure(),
        'httponly' => true,
        'samesite' => 'Lax',
    ));

    if (!is_dir($sessionPath)) {
        @mkdir($sessionPath, 0777, true);
    }

    if (is_dir($sessionPath) && is_writable($sessionPath)) {
        session_save_path($sessionPath);
    }

    session_start();
}

date_default_timezone_set('Asia/Bangkok');

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

function app_button_icon_svg(string $icon): string
{
    static $icons = null;

    if ($icons === null) {
        $icons = array(
            'search' => '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><circle cx="11" cy="11" r="7"></circle><path d="m20 20-3.5-3.5"></path></svg>',
            'user-plus' => '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M16 21v-2a4 4 0 0 0-4-4H7a4 4 0 0 0-4 4v2"></path><circle cx="9.5" cy="7" r="4"></circle><path d="M19 8v6"></path><path d="M16 11h6"></path></svg>',
            'rotate-ccw' => '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M3 12a9 9 0 1 0 3-6.7"></path><path d="M3 3v6h6"></path></svg>',
            'sliders' => '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M4 21v-7"></path><path d="M4 10V3"></path><path d="M12 21v-4"></path><path d="M12 13V3"></path><path d="M20 21v-9"></path><path d="M20 8V3"></path><path d="M2 14h4"></path><path d="M10 9h4"></path><path d="M18 12h4"></path></svg>',
            'plus-square' => '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><rect x="3" y="3" width="18" height="18" rx="4"></rect><path d="M12 8v8"></path><path d="M8 12h8"></path></svg>',
            'eye' => '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M2 12s3.5-6 10-6 10 6 10 6-3.5 6-10 6-10-6-10-6Z"></path><circle cx="12" cy="12" r="3"></circle></svg>',
            'play' => '<svg viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><path d="M8 6.5v11l9-5.5-9-5.5Z"></path></svg>',
            'x' => '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M18 6 6 18"></path><path d="m6 6 12 12"></path></svg>',
            'check' => '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m5 12 4.2 4.2L19 6.5"></path></svg>',
            'arrow-left' => '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M19 12H5"></path><path d="m12 19-7-7 7-7"></path></svg>',
            'refresh-cw' => '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M21 2v6h-6"></path><path d="M3 12a9 9 0 0 1 15.5-6.3L21 8"></path><path d="M3 22v-6h6"></path><path d="M21 12a9 9 0 0 1-15.5 6.3L3 16"></path></svg>',
            'wallet' => '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M3 7a2 2 0 0 1 2-2h11l3 3v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V7Z"></path><path d="M16 13h2"></path></svg>',
            'wrench' => '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M14.7 6.3a4 4 0 0 0-5.4 5.4L3 18l3 3 6.3-6.3a4 4 0 0 0 5.4-5.4l-2.1 2.1-3.6-3.6 2.1-2.1Z"></path></svg>',
            'clock' => '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><circle cx="12" cy="12" r="8.5"></circle><path d="M12 7.5v5.1l3.4 2"></path></svg>',
            'loader' => '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M12 3.8a8.2 8.2 0 1 0 8.2 8.2"></path><path d="M20.2 5.5v5.7h-5.7"></path></svg>',
            'check-circle' => '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><circle cx="12" cy="12" r="8.5"></circle><path d="m8.3 12.1 2.6 2.6 4.8-4.8"></path></svg>',
            'x-circle' => '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><circle cx="12" cy="12" r="8.5"></circle><path d="m9.5 9.5 5 5"></path><path d="m14.5 9.5-5 5"></path></svg>',
            'edit-2' => '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M12 20h9"></path><path d="M16.5 3.5a2.12 2.12 0 1 1 3 3L7 19l-4 1 1-4 12.5-12.5Z"></path></svg>',
            'trash-2' => '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M3 6h18"></path><path d="M8 6V4h8v2"></path><path d="M19 6l-1 14a2 2 0 0 1-2 2H8a2 2 0 0 1-2-2L5 6"></path><path d="M10 11v6"></path><path d="M14 11v6"></path></svg>',
            'printer' => '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M6 9V3h12v6"></path><path d="M6 18H4a2 2 0 0 1-2-2v-5a2 2 0 0 1 2-2h16a2 2 0 0 1 2 2v5a2 2 0 0 1-2 2h-2"></path><path d="M6 14h12v7H6z"></path></svg>',
            'chart' => '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M3 3v18h18"></path><path d="M7 14v3"></path><path d="M12 10v7"></path><path d="M17 6v11"></path></svg>',
            'download' => '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M12 3v12"></path><path d="m7 10 5 5 5-5"></path><path d="M5 21h14"></path></svg>',
        );
    }

    return $icons[$icon] ?? $icons['plus-square'];
}

function app_button_content(string $icon, string $label): string
{
    return '<span class="app-action-button-icon" aria-hidden="true">'
        . app_button_icon_svg($icon)
        . '</span><span class="app-action-button-label">'
        . e($label)
        . '</span>';
}

function redirect(string $path): void
{
    header("Location: {$path}");
    exit();
}
