<?php
// config.php
// เริ่มต้นใช้งาน Session หากยังไม่มีการเปิดใช้งาน
if (session_status() == PHP_SESSION_NONE) {
    session_start();
}

// -------------------------------------------------------------
// [1] ดึงข้อมูลสินค้าจากฐานข้อมูลจริง SQLite (Real Database Connection)
// -------------------------------------------------------------
require_once 'db.php';

try {
    $stmt = $pdo->query("SELECT * FROM products ORDER BY id DESC");
    $dbProducts = $stmt->fetchAll();
    
    $clothing_products = [];
    foreach ($dbProducts as $row) {
        $sizes = array_filter(array_map('trim', explode(',', $row['sizes'] ?? '')));
        if (empty($sizes)) {
            $sizes = ['S', 'M', 'L', 'XL'];
        }
        
        $colors_th = array_filter(array_map('trim', explode(',', $row['colors_th'] ?? '')));
        if (empty($colors_th)) {
            $colors_th = ['ขาวคลาสสิก', 'ดำคาร์บอน'];
        }
        
        $colors_en = array_filter(array_map('trim', explode(',', $row['colors_en'] ?? '')));
        if (empty($colors_en)) {
            $colors_en = ['Classic White', 'Carbon Black'];
        }

        $gallery = json_decode($row['gallery_images'] ?? '[]', true);
        if (empty($gallery)) {
            $gallery = [$row['image']];
        }

        $clothing_products[] = [
            'id' => (int)$row['id'],
            'shop_id' => (int)$row['shop_id'],
            'name_th' => $row['name_th'] ?: $row['name'],
            'name_en' => $row['name_en'] ?: $row['name'],
            'price' => (float)$row['price'],
            'image' => $row['image'] ?: 'https://images.unsplash.com/photo-1584917865442-de89df76afd3?w=800',
            'category' => $row['category'] ?: 'General',
            'sizes' => $sizes,
            'colors_th' => $colors_th,
            'colors_en' => $colors_en,
            'stock' => (int)$row['stock'],
            'rating' => isset($row['rating']) ? (float)$row['rating'] : 4.9,
            'sold_count' => isset($row['sold_count']) ? (int)$row['sold_count'] : 128,
            'brand' => $row['brand'] ?? 'VIVY BRANDNAME',
            'model' => $row['model'] ?? $row['name'],
            'dimensions' => $row['dimensions'] ?? '19 x 12 x 8 ซม.',
            'year' => $row['year'] ?? '2024',
            'equipment' => $row['equipment'] ?? 'ถุงผ้า, กล่อง',
            'condition' => $row['condition'] ?? 'Like New',
            'product_code' => $row['product_code'] ?? ('26070' . $row['id']),
            'gallery_images' => $gallery
        ];
    }
    $_SESSION['clothing_products'] = $clothing_products;
} catch (PDOException $e) {
    $_SESSION['clothing_products'] = [];
}

// -------------------------------------------------------------
// [2] ตะกร้าสินค้าจำลองใน Session (Cart Session DB)
// -------------------------------------------------------------
if (!isset($_SESSION['cart'])) {
    $_SESSION['cart'] = [];
}

// -------------------------------------------------------------
// [3] ตั้งค่าเริ่มต้นระบบแอปพลิเคชัน (App Initialization)
// -------------------------------------------------------------
if (!isset($_SESSION['lang'])) { $_SESSION['lang'] = 'th'; }
if (!isset($_SESSION['theme'])) { $_SESSION['theme'] = 'light'; }
if (!isset($_SESSION['is_logged_in'])) { $_SESSION['is_logged_in'] = false; }
if (!isset($_SESSION['username'])) { $_SESSION['username'] = ''; }
if (!isset($_SESSION['user_role'])) { $_SESSION['user_role'] = 'buyer'; } // buyer = ลูกค้าทั่วไป, admin = แอดมินหลังบ้าน

// -------------------------------------------------------------
// [4] พจนานุกรมแปลภาษา (Localization Dictionary)
// -------------------------------------------------------------
$translations = [
    'th' => [
        'shop_name' => 'Noa Shop 👕',
        'home' => 'หน้าแรก',
        'admin_panel' => 'แผงหลังบ้านแอดมิน',
        'login' => 'เข้าสู่ระบบ',
        'logout' => 'ออกจากระบบ',
        'booking_title' => 'จองเสื้อผ้าแฟชั่นพรีออเดอร์',
        'booking_desc' => 'เสื้อผ้านำเข้าสไตล์มินิมอลและสตรีทแวร์เกาหลี ราคาพิเศษสุดคุ้ม!',
        'book_btn' => 'เพิ่มลงตะกร้า',
        'booking_confirm' => 'ทำรายการสั่งซื้อเสร็จเรียบร้อยแล้ว!',
        'select_size' => 'เลือกขนาดไซส์ (Size)',
        'select_color' => 'เลือกเฉดสี (Color)',
        'select_qty' => 'จำนวน (Quantity)',
        'stock_avail' => 'สินค้าในคลังคงเหลือ: ',
        'price_unit' => 'บาท',
        'welcome' => 'ยินดีต้อนรับคุณ',
        'cart_title' => 'ตะกร้าสินค้าของคุณ',
        'cart_empty' => 'ไม่มีสินค้าในตะกร้า',
        'cart_total' => 'ยอดรวมทั้งหมด',
        'cart_checkout' => 'ไปหน้าชำระเงิน',
        'qty' => 'จำนวน',
        'required_login' => 'กรุณาเข้าสู่ระบบเพื่อสั่งซื้อสินค้า'
    ],
    'en' => [
        'shop_name' => 'Noa Shop 👕',
        'home' => 'Home',
        'admin_panel' => 'Admin Dashboard',
        'login' => 'Log In',
        'logout' => 'Log Out',
        'booking_title' => 'Pre-order Trendy Clothing',
        'booking_desc' => 'Imported minimalist and Korean streetwear outfits at exclusive prices!',
        'book_btn' => 'Add to Cart',
        'booking_confirm' => 'Order placed successfully!',
        'select_size' => 'Select Size',
        'select_color' => 'Select Color',
        'select_qty' => 'Quantity',
        'stock_avail' => 'Available Stock: ',
        'price_unit' => 'THB',
        'welcome' => 'Welcome,',
        'cart_title' => 'Your Shopping Cart',
        'cart_empty' => 'Your cart is empty.',
        'cart_total' => 'Grand Total',
        'cart_checkout' => 'Proceed to Checkout',
        'qty' => 'Qty',
        'required_login' => 'Please log in to purchase items.'
    ]
];

$lang = $_SESSION['lang'];
$theme = $_SESSION['theme'];
$txt = $translations[$lang];

// -------------------------------------------------------------
// [5] การจัดการธีมสี High Contrast (Utility Tailwind CSS Classes)
// -------------------------------------------------------------
$theme_classes = [
    'light' => [
        'bg' => 'bg-slate-50 text-slate-900',
        'card' => 'bg-white border border-slate-200 shadow-md hover:shadow-2xl transition-all duration-300',
        'navbar' => 'bg-white/80 border-b border-slate-200 text-slate-900',
        'footer' => 'bg-slate-100 border-t border-slate-200 text-slate-600',
        'input' => 'bg-white border border-slate-300 text-slate-900 focus:ring-2 focus:ring-amber-500 focus:border-amber-500',
        'table_header' => 'bg-slate-100 text-slate-700 font-bold',
        'badge' => 'bg-amber-100 text-amber-800'
    ],
    'dark' => [
        'bg' => 'bg-neutral-950 text-neutral-100',
        'card' => 'bg-zinc-900 border border-zinc-800 shadow-none hover:border-zinc-700 transition-all duration-300',
        'navbar' => 'bg-neutral-950/90 border-b border-zinc-800 text-neutral-100',
        'footer' => 'bg-zinc-900 border-t border-zinc-800 text-zinc-400',
        'input' => 'bg-zinc-800 border border-zinc-700 text-white focus:ring-2 focus:ring-amber-500 focus:border-amber-500',
        'table_header' => 'bg-zinc-800 text-zinc-300 font-bold',
        'badge' => 'bg-amber-950/50 text-amber-400'
    ]
];

$tc = $theme_classes[$theme];
?>
