<?php
$host = "localhost";
$dbname = "dewanmar_gadgetbd"; 
$username = "dewanmar_gadgetbd"; 
$password = "gadgetbd123";

try {
    $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8mb4", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // শুধু টেবিলগুলো তৈরি করার কোড থাকবে
    $queries =[
        "CREATE TABLE IF NOT EXISTS users (
            id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) NOT NULL, phone VARCHAR(20) UNIQUE NOT NULL, email VARCHAR(100) NULL,
            address TEXT NULL, password VARCHAR(255) NOT NULL, role ENUM('admin', 'customer') DEFAULT 'customer',
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
        )",
        "CREATE TABLE IF NOT EXISTS categories (
            id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) NOT NULL, slug VARCHAR(100) NOT NULL UNIQUE,
            image VARCHAR(255) DEFAULT NULL, status TINYINT(1) DEFAULT 1, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
        )",
        "CREATE TABLE IF NOT EXISTS products (
            id INT AUTO_INCREMENT PRIMARY KEY, category_id INT NOT NULL, name VARCHAR(200) NOT NULL, slug VARCHAR(200) NOT NULL UNIQUE,
            description TEXT, image VARCHAR(255), base_price DECIMAL(10,2) NOT NULL DEFAULT 0.00, price_unit VARCHAR(100) DEFAULT 'পিস',
            options JSON, status TINYINT(1) DEFAULT 1, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            FOREIGN KEY (category_id) REFERENCES categories(id) ON DELETE CASCADE
        )",
        "CREATE TABLE IF NOT EXISTS orders (
            id INT AUTO_INCREMENT PRIMARY KEY, user_id INT NULL, order_number VARCHAR(50) NOT NULL UNIQUE, customer_name VARCHAR(100) NOT NULL,
            customer_phone VARCHAR(20) NOT NULL, customer_address TEXT NOT NULL, total_amount DECIMAL(10,2) NOT NULL,
            status ENUM('Pending', 'Processing', 'Completed', 'Cancelled') DEFAULT 'Pending', order_note TEXT,
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE SET NULL
        )",
        "CREATE TABLE IF NOT EXISTS order_items (
            id INT AUTO_INCREMENT PRIMARY KEY, order_id INT NOT NULL, product_id INT NOT NULL, quantity INT NOT NULL,
            unit_price DECIMAL(10,2) NOT NULL, selected_options JSON, file_path VARCHAR(255), 
            FOREIGN KEY (order_id) REFERENCES orders(id) ON DELETE CASCADE, FOREIGN KEY (product_id) REFERENCES products(id) ON DELETE CASCADE
        )",
        "CREATE TABLE IF NOT EXISTS settings (
            id INT AUTO_INCREMENT PRIMARY KEY, site_name VARCHAR(100), contact_calls VARCHAR(20), 
            contact_email VARCHAR(100), address TEXT, trade_license VARCHAR(100), logo VARCHAR(255)
        )"
    ];

    foreach ($queries as $query) { $pdo->exec($query); }

    // সেটিংস টেবিল ফাঁকা থাকলে শুধু একবার ডিফল্ট ডাটা ঢুকবে, বারবার নয়
    $check = $pdo->query("SELECT COUNT(*) FROM settings")->fetchColumn();
    if($check == 0) {
        $pdo->exec("INSERT INTO settings (id, site_name, contact_calls, contact_email, trade_license) VALUES (1, 'IzhaanPrinters', '01700000000', 'info@izhaanprinters.com', 'TRAD-12345678')");
    }

} catch(PDOException $e) {
    die("Database Connection Failed: " . $e->getMessage());
}
?>