<?php
// Calculate application base path dynamically
if (php_sapi_name() === 'cli') {
    define('APP_BASE_PATH', '/');
} else {
    $project_root = str_replace('\\', '/', __DIR__);
    $script_filename = str_replace('\\', '/', $_SERVER['SCRIPT_FILENAME'] ?? '');
    $script_name = str_replace('\\', '/', $_SERVER['SCRIPT_NAME'] ?? '');

    $relative_path = '';
    if ($project_root && $script_filename) {
        $relative_path = str_replace($project_root, '', $script_filename);
    }

    $app_base_path = '/';
    if ($script_name && $relative_path) {
        $pos = strrpos($script_name, $relative_path);
        if ($pos !== false) {
            $app_base_path = substr($script_name, 0, $pos);
        } else {
            $app_base_path = dirname($script_name);
        }
    }
    $app_base_path = rtrim(str_replace('\\', '/', $app_base_path), '/') . '/';
    define('APP_BASE_PATH', $app_base_path);
}

// Database configuration
define('DB_HOST', 'localhost');
define('DB_PORT', '3306');
define('DB_NAME', 'nexstore_apparel_shs');
define('DB_USER', 'nexstore_apparel_shs');
define('DB_PASS', 'apparel_shs@2026');

// Application settings
define('APP_NAME', 'NexCore Apparel');
define('BASE_URL', 'https://shs.apparel.nexstoreapps.com/');

// Error reporting (disable in production)
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Session start
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// Timezone initialization based on client header or session
$userTimezone = $_SERVER['HTTP_X_USER_TIMEZONE'] ?? $_SESSION['user_timezone'] ?? null;
if ($userTimezone) {
    if (in_array($userTimezone, timezone_identifiers_list())) {
        date_default_timezone_set($userTimezone);
        if (session_status() !== PHP_SESSION_NONE) {
            $_SESSION['user_timezone'] = $userTimezone;
        }
    }
}
