<?php
require_once 'config/db.php';

try {
    // 1. Payment Gateways Table
    $sql_payments = "CREATE TABLE IF NOT EXISTS payment_gateways (
        id INT AUTO_INCREMENT PRIMARY KEY,
        provider VARCHAR(50) NOT NULL UNIQUE, -- 'stripe', 'paypal', 'crypto'
        api_key VARCHAR(255),
        secret_key VARCHAR(255),
        webhook_secret VARCHAR(255),
        is_active BOOLEAN DEFAULT FALSE,
        currency VARCHAR(10) DEFAULT 'USD',
        updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
    )";
    $pdo->exec($sql_payments);
    echo "✅ Table 'payment_gateways' created successfully.<br>";

    // 2. Storage Config Table
    $sql_storage = "CREATE TABLE IF NOT EXISTS storage_config (
        id INT AUTO_INCREMENT PRIMARY KEY,
        provider VARCHAR(50) DEFAULT 'local', -- 'local', 's3', 'r2', 'spaces'
        bucket VARCHAR(100),
        region VARCHAR(50),
        endpoint VARCHAR(255),
        access_key VARCHAR(255),
        secret_key VARCHAR(255),
        public_url_prefix VARCHAR(255), -- e.g. https://cdn.example.com
        updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
    )";
    $pdo->exec($sql_storage);
    echo "✅ Table 'storage_config' created successfully.<br>";

    // 3. Push Devices Table
    $sql_push = "CREATE TABLE IF NOT EXISTS push_devices (
        id INT AUTO_INCREMENT PRIMARY KEY,
        user_id INT NOT NULL,
        token VARCHAR(500) NOT NULL UNIQUE, -- FCM Token
        platform VARCHAR(20) DEFAULT 'android', -- 'android', 'ios', 'web'
        last_active TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
    )";
    $pdo->exec($sql_push);
    echo "✅ Table 'push_devices' created successfully.<br>";

    // 4. Withdrawals Table
    $sql_withdraw = "CREATE TABLE IF NOT EXISTS withdrawals (
        id INT AUTO_INCREMENT PRIMARY KEY,
        user_id INT NOT NULL,
        amount INT NOT NULL, -- Diamonds
        payout_amount DECIMAL(10, 2) NOT NULL, -- USD
        method VARCHAR(50) NOT NULL, -- 'paypal', 'bank', 'crypto'
        details TEXT NOT NULL, -- Account info JSON
        status ENUM('pending', 'approved', 'rejected', 'completed') DEFAULT 'pending',
        admin_note TEXT,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
        FOREIGN KEY (user_id) REFERENCES users(id)
    )";
    $pdo->exec($sql_withdraw);
    echo "✅ Table 'withdrawals' created successfully.<br>";

    // Insert Default Local Storage Config if empty
    $stmt = $pdo->query("SELECT COUNT(*) FROM storage_config");
    if ($stmt->fetchColumn() == 0) {
        $pdo->exec("INSERT INTO storage_config (provider, public_url_prefix) VALUES ('local', 'http://localhost/Juuvix_Shop/KLKAPP TEST/server/uploads')");
        echo "ℹ️ Default Local Storage config inserted.<br>";
    }

    echo "<hr>🎉 Advanced Config DB Setup Complete (Phases 13-16)!";

} catch (PDOException $e) {
    die("❌ DB Error: " . $e->getMessage());
}
?>
