<?php
// Global Application Configuration

// ============================================================
// Environment Detection: auto-detect local vs. production
// ============================================================
$httpHost = $_SERVER['HTTP_HOST'] ?? 'localhost';
$isLocal = ($httpHost === 'localhost' || $httpHost === '127.0.0.1' || strpos($httpHost, 'localhost:') === 0 || strpos($httpHost, '127.0.0.1:') === 0);

// Base URL — ตั้งค่าตายตัวสำหรับแต่ละ environment เพื่อความเสถียร
if ($isLocal) {
    // Local: XAMPP subfolder
    $scriptDir = str_replace('\\', '/', dirname($_SERVER['SCRIPT_NAME'] ?? ''));
    $baseUrl = 'http://' . $httpHost . rtrim($scriptDir, '/');
} else {
    // Production: hardcode เพื่อป้องกัน SCRIPT_NAME ผิดพลาดบน shared hosting
    $baseUrl = 'https://webapp69.cm.in.th/u69319090039/Shop39';
}

define('APP_NAME', 'Shop 39');
define('APP_URL', $baseUrl);
define('SITE_ROOT', dirname(__DIR__));

// ============================================================
// Database Settings — แก้ไขตรงนี้สำหรับโฮสติ้ง
// ============================================================
if ($isLocal) {
    // ---- Local (XAMPP) ----
    define('DB_HOST', '127.0.0.1');
    define('DB_PORT', '3306');
    define('DB_NAME', 'marketplace_db');
    define('DB_USER', 'root');
    define('DB_PASS', '');
} else {
    // ---- Production Hosting (webapp69.cm.in.th) ----
    define('DB_HOST', 'localhost');
    define('DB_PORT', '3306');
    define('DB_NAME', 'shop69319090039');      // ชื่อฐานข้อมูลจริงจาก phpMyAdmin
    define('DB_USER', 'u69319090039');         // Hosting / phpMyAdmin Username
    define('DB_PASS', '@6939');
}

define('DB_CHARSET', 'utf8mb4');

// Session Settings
define('SESSION_NAME', 'MARKETPLACE_SESSID');
define('SESSION_LIFETIME', 86400); // 24 hours

// Upload Directories
define('UPLOAD_DIR', SITE_ROOT . '/public/uploads/');
define('UPLOAD_URL', APP_URL . '/public/uploads/');

// Error Display Settings (Development mode)
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

// Set default timezone
date_default_timezone_set('Asia/Bangkok');

// Force UTF-8 encoding for multibyte string handling
mb_internal_encoding('UTF-8');
mb_http_output('UTF-8');
