<?php
/**
 * Installation Script - Phase 5
 * Unified banner: separate desktop + mobile images in one row
 * Path: install-five.php
 */
require_once 'config.php';
header('Content-Type: text/html; charset=utf-8');

if (!$conn) {
    die("<div style='color:red;font-family:sans-serif;padding:20px;'><strong>ত্রুটি:</strong> ডাটাবেজ সংযোগ হয়নি।</div>");
}

$prefix = DB_PREFIX;
$table  = "{$prefix}banners";
$messages = [];
$success = true;

function col_exists($conn, $table, $col) {
    $table = $conn->real_escape_string($table);
    $col   = $conn->real_escape_string($col);
    $r = $conn->query("SHOW COLUMNS FROM `{$table}` LIKE '{$col}'");
    return $r && $r->num_rows > 0;
}

$columns = [
    'desktop_image_url'    => "ALTER TABLE `{$table}` ADD `desktop_image_url` VARCHAR(500) NULL AFTER `image_url`",
    'desktop_image_source' => "ALTER TABLE `{$table}` ADD `desktop_image_source` ENUM('upload','url') NULL DEFAULT 'url' AFTER `desktop_image_url`",
    'mobile_image_url'     => "ALTER TABLE `{$table}` ADD `mobile_image_url` VARCHAR(500) NULL AFTER `desktop_image_source`",
    'mobile_image_source'  => "ALTER TABLE `{$table}` ADD `mobile_image_source` ENUM('upload','url') NULL DEFAULT 'url' AFTER `mobile_image_url`",
];

foreach ($columns as $name => $sql) {
    if (col_exists($conn, $table, $name)) {
        $messages[] = "○ কলাম আগে থেকেই আছে: `{$name}`";
        continue;
    }
    if ($conn->query($sql) === TRUE) {
        $messages[] = "✓ কলাম যোগ: `{$name}`";
    } else {
        $messages[] = "✗ ত্রুটি ({$name}): " . $conn->error;
        $success = false;
    }
}

// পুরনো ডেটা মাইগ্রেট
if ($success) {
    $conn->query("UPDATE `{$table}` SET desktop_image_url = image_url, desktop_image_source = image_source WHERE device_type = 'desktop' AND (desktop_image_url IS NULL OR desktop_image_url = '')");
    $conn->query("UPDATE `{$table}` SET mobile_image_url = image_url, mobile_image_source = image_source WHERE device_type = 'mobile' AND (mobile_image_url IS NULL OR mobile_image_url = '')");
    $conn->query("UPDATE `{$table}` SET desktop_image_url = image_url, desktop_image_source = image_source, mobile_image_url = image_url, mobile_image_source = image_source WHERE device_type = 'both' AND (desktop_image_url IS NULL OR desktop_image_url = '')");
    $messages[] = "✓ পুরনো ব্যানার ডেটা মাইগ্রেট সম্পন্ন";
}
?>
<!DOCTYPE html>
<html lang="bn">
<head><meta charset="UTF-8"><title>Install Five</title>
<style>body{font-family:sans-serif;background:#0f172a;padding:40px;color:#cbd5e1}.box{max-width:520px;margin:0 auto;background:#1e293b;padding:30px;border-radius:12px;border:1px solid #334155}h2{color:#38bdf8}li{padding:8px;font-size:13px}.ok{color:#4ade80;font-weight:bold}</style>
</head>
<body><div class="box"><h2>Phase 5 — Unified Banner Images</h2><ul><?php foreach($messages as $m) echo "<li>".htmlspecialchars($m)."</li>"; ?></ul>
<?php if($success): ?><p class="ok">সফল! এখন admin/banner.php থেকে এক ফর্মে Desktop + Mobile ইমেজ দিতে পারবেন।</p><?php endif; ?>
</div></body></html>
