<?php
$pdo = require __DIR__ . '/config/pdo.php';

try {
    $sql = "CREATE TABLE IF NOT EXISTS shipping_rates (
        id INT AUTO_INCREMENT PRIMARY KEY,
        location_name VARCHAR(150) NOT NULL,
        cost DECIMAL(12,2) NOT NULL DEFAULT 0,
        is_default TINYINT(1) DEFAULT 0,
        is_active TINYINT(1) DEFAULT 1,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";

    $pdo->exec($sql);
    echo "Table 'shipping_rates' created.\n";

    // Seed Data if empty
    $count = $pdo->query("SELECT COUNT(*) FROM shipping_rates")->fetchColumn();
    if ($count == 0) {
        $pdo->exec("INSERT INTO shipping_rates (location_name, cost, is_default) VALUES ('Luar Kota Padang (Flat Rate)', 70000, 1)");
        $pdo->exec("INSERT INTO shipping_rates (location_name, cost, is_default) VALUES ('Kota Padang', 20000, 0)");
        echo "Seeded default rates (Padang: 20k, Outside: 70k).\n";
    }

} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}
