<?php
// Centralized product data source for the T-shirt e-commerce website.
// PHP 7.4 compatible syntax.

$data_dir = __DIR__ . '/data';
$data_file = $data_dir . '/products.json';

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

$default_products = array(
    'basic' => array(
        'id' => 'basic',
        'name' => 'The Essential Tee',
        'thai_name' => 'เสื้อยืดรุ่น Essential',
        'tier' => 'Basic',
        'price' => 29.00,
        'tagline' => 'Everyday comfort, refined.',
        'thai_tagline' => 'ความสบายในทุกวัน ที่เรียบง่ายและลงตัว',
        'description' => 'Crafted from 100% combed cotton, The Essential Tee offers daily-wear perfection with a ribbed crew neck and a modern regular fit.',
        'thai_description' => 'ผลิตจากผ้าคอตตอนคอมบ์ 100% มอบความสบายในการสวมใส่ทุกวัน ด้วยคอกลมทอละเอียดและทรงที่ทันสมัย',
        'image_url' => 'https://images.unsplash.com/photo-1574180566232-aaad1b5b8450?auto=format&fit=crop&q=80&w=800',
        'specs' => array(
            '100% Long-staple Combed Cotton',
            'Mid-weight 180 GSM fabric',
            'Reinforced shoulder-to-shoulder taping',
            'Preshrunk to minimize shrinkage'
        ),
        'thai_specs' => array(
            'ผ้าคอตตอนคอมบ์เส้นใยยาว 100%',
            'ความหนาปานกลาง 180 GSM',
            'เย็บด้ายคู่เสริมตะเข็บช่วงไหล่',
            'ผ่านการหดตัวล่วงหน้าเพื่อลดการหดหลังซัก'
        )
    ),
    'pro' => array(
        'id' => 'pro',
        'name' => 'The Heavyweight Pro',
        'thai_name' => 'เสื้อยืดรุ่น Heavyweight Pro',
        'tier' => 'Pro',
        'price' => 49.00,
        'tagline' => 'Structured silhouette. Uncompromised durability.',
        'thai_tagline' => 'ทรงเสื้อที่เป็นโครงสวย ทนทานอย่างไร้ขีดจำกัด',
        'description' => 'Engineered from premium heavyweight organic cotton, The Heavyweight Pro features a thick, structured drape and a dense weave built to last years.',
        'thai_description' => 'ถักทอจากผ้าฝ้ายออร์แกนิกเกรดพรีเมียมหนาพิเศษ ให้ทรงที่สวยงาม หนา มีน้ำหนัก และมีความคงทนสูงเพื่อการใช้งานที่ยาวนาน',
        'image_url' => 'https://images.unsplash.com/photo-1522706604291-210a56c3b376?auto=format&fit=crop&q=80&w=800',
        'specs' => array(
            '100% Premium Organic Cotton',
            'Heavyweight 260 GSM fabric',
            'Tight, dense knit construction',
            'Thick ribbed collar that holds shape'
        ),
        'thai_specs' => array(
            'ผ้าฝ้ายออร์แกนิกเกรดพรีเมียม 100%',
            'ความหนาพิเศษ 260 GSM',
            'โครงสร้างการทอแบบหนาแน่นพิเศษ',
            'คอเสื้อทอหนาพิเศษ ไม่ย้วยง่ายและอยู่ทรง'
        )
    ),
    'luxury' => array(
        'id' => 'luxury',
        'name' => 'The Silk-Cashmere Luxury',
        'thai_name' => 'เสื้อยืดรุ่น Silk-Cashmere Luxury',
        'tier' => 'Luxury',
        'price' => 99.00,
        'tagline' => 'Pure indulgence. Unrivaled feel.',
        'thai_tagline' => 'ความหรูหราที่แท้จริง สัมผัสที่เหนือระดับ',
        'description' => 'An exquisite blend of long-staple cotton, mulberry silk, and fine cashmere. The Silk-Cashmere Luxury tee offers an incredibly soft touch and a subtle luster.',
        'thai_description' => 'การผสมผสานอันประณีตของผ้าฝ้ายใยยาว เส้นไหมหม่อน และแคชเมียร์ชั้นดี มอบสัมผัสที่นุ่มนวลอย่างเหลือเชื่อและความเงางามอย่างเป็นธรรมชาติ',
        'image_url' => 'https://images.unsplash.com/photo-1590999659195-e64a988eaf4f?auto=format&fit=crop&q=80&w=800',
        'specs' => array(
            '70% Long-staple Cotton, 20% Mulberry Silk, 10% Cashmere',
            'Ultra-fine, lightweight 140 GSM drape',
            'Silky smooth and breathable texture',
            'Discreet, low-profile blind stitched hems'
        ),
        'thai_specs' => array(
            'ผ้าฝ้ายใยยาว 70%, ไหมหม่อน 20%, แคชเมียร์ 10%',
            'ผ้าทอละเอียด บางเบาพิเศษ 140 GSM',
            'เนื้อสัมผัสเรียบเนียนแบบไหมและระบายอากาศได้ดีเยี่ยม',
            'การเย็บตะเข็บซ่อนประณีต ไร้รอยสะดุด'
        )
    )
);

require_once __DIR__ . '/db.php';

$products = array();
try {
    $db = get_db_connection();
    if ($db) {
        $stmt = $db->query("SELECT * FROM products");
        while ($row = $stmt->fetch()) {
            $row['specs'] = json_decode($row['specs'], true) ?: array();
            $row['thai_specs'] = json_decode($row['thai_specs'], true) ?: array();
            $products[$row['id']] = $row;
        }
    }
} catch (Exception $e) {
    error_log("Database load failed: " . $e->getMessage() . ". Using default fallback products.");
}

// If database load failed or returned no results, use default list
if (empty($products)) {
    $products = $default_products;
}

// Helper function to query product by ID
function get_product_by_id($id) {
    try {
        $db = get_db_connection();
        if ($db) {
            $stmt = $db->prepare("SELECT * FROM products WHERE id = :id");
            $stmt->execute(array('id' => $id));
            $product = $stmt->fetch();
            if ($product) {
                $product['specs'] = json_decode($product['specs'], true) ?: array();
                $product['thai_specs'] = json_decode($product['thai_specs'], true) ?: array();
                return $product;
            }
        }
    } catch (Exception $e) {
        error_log("Failed to fetch product from DB: " . $e->getMessage());
    }
    
    global $products;
    return isset($products[$id]) ? $products[$id] : null;
}

