<?php
require_once 'common/config.php';
include 'common/header.php';

echo "<div class='max-w-4xl mx-auto px-4 py-10 sm:px-6 lg:px-8'>";
echo "<div class='cyber-card p-8 border-cyan-500/30'>";
echo "<h2 class='text-2xl font-bold logo-font text-cyan-400 mb-6 uppercase tracking-widest'>System Protocol: Database Update</h2>";
echo "<div class='space-y-4 font-mono text-xs'>";

// ১. ইউজার টেবিল তৈরি (যদি না থাকে)
$sql_users = "CREATE TABLE IF NOT EXISTS users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(50) NOT NULL,
    email VARCHAR(100) NOT NULL UNIQUE,
    password VARCHAR(255) NOT NULL,
    balance DECIMAL(10, 2) DEFAULT 0.00,
    role ENUM('user', 'admin') DEFAULT 'user',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";

if ($conn->query($sql_users)) {
    echo "<p class='text-green-500'>[OK] User Registry System Initialized.</p>";
} else {
    echo "<p class='text-red-500'>[ERROR] User Registry Failure: " . $conn->error . "</p>";
}

// ২. বিদ্যমান files টেবিলে প্রাইস এবং ডিসকাউন্ট কলাম যোগ করা (নিরাপদ পদ্ধতি)
// চেক করা হচ্ছে কলামটি আগে থেকেই আছে কিনা যাতে এরর না আসে
$columns = $conn->query("SHOW COLUMNS FROM `files` LIKE 'price'");
if ($columns->num_rows == 0) {
    if ($conn->query("ALTER TABLE files ADD COLUMN price DECIMAL(10, 2) DEFAULT 0.00")) {
        echo "<p class='text-green-500'>[OK] Price Protocol Integrated into Assets.</p>";
    }
} else {
    echo "<p class='text-cyan-500'>[INFO] Price Protocol already active.</p>";
}

$columns_discount = $conn->query("SHOW COLUMNS FROM `files` LIKE 'discount'");
if ($columns_discount->num_rows == 0) {
    if ($conn->query("ALTER TABLE files ADD COLUMN discount DECIMAL(10, 2) DEFAULT 0.00")) {
        echo "<p class='text-green-500'>[OK] Discount Module Synced.</p>";
    }
} else {
    echo "<p class='text-cyan-500'>[INFO] Discount Module already active.</p>";
}

// ৩. কেনাকাটার হিসেব রাখার জন্য টেবিল (যদি না থাকে)
$sql_purchases = "CREATE TABLE IF NOT EXISTS purchases (
    id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT,
    file_id INT,
    purchase_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";

if ($conn->query($sql_purchases)) {
    echo "<p class='text-green-500'>[OK] Transaction Ledger Created.</p>";
}

// ৪. অ্যাড মানি (ডিপোজিট) টেবিল (যদি না থাকে)
$sql_deposits = "CREATE TABLE IF NOT EXISTS deposits (
    id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT,
    amount DECIMAL(10, 2),
    method VARCHAR(50),
    transaction_id VARCHAR(100),
    status ENUM('pending', 'approved', 'rejected') DEFAULT 'pending',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";

if ($conn->query($sql_deposits)) {
    echo "<p class='text-green-500'>[OK] Financial Deposit Gateway Initialized.</p>";
}

echo "</div>";
echo "<div class='mt-8 pt-6 border-t border-white/10'>";
echo "<p class='text-gray-400 text-[10px] uppercase tracking-[2px] mb-4'>System update completed successfully. You can now access all modules.</p>";
echo "<a href='index.php' class='cyber-button px-6 py-2 rounded-lg text-white font-bold text-xs uppercase'>Enter Dashboard</a>";
echo "</div>";
echo "</div>";
echo "</div>";

include 'common/footer.php';
?>