<?php
/**
 * Install Nine — Merge duplicate companies (same name + category)
 * One company → one catalog across all sub-categories
 * Run once in browser, then delete this file.
 */
require_once 'config.php';
require_once 'function.php';

header('Content-Type: text/html; charset=utf-8');

if (!$conn) {
    die('<h2 style="color:red">DB connection failed</h2>');
}

ensure_product_hierarchy();
$stats = merge_duplicate_companies();

$prefix = DB_PREFIX;
$count = 0;
$r = $conn->query("SELECT COUNT(*) AS c FROM `{$prefix}companies`");
if ($r && $row = $r->fetch_assoc()) $count = (int)$row['c'];
?>
<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <title>Install Nine — Merge Companies</title>
    <style>
        body { font-family: system-ui, sans-serif; max-width: 640px; margin: 40px auto; padding: 0 20px; background: #0f172a; color: #e2e8f0; }
        .card { background: #1e293b; border-radius: 16px; padding: 28px; border: 1px solid #334155; }
        h1 { font-size: 1.25rem; margin: 0 0 12px; }
        .ok { color: #34d399; }
        .warn { color: #fbbf24; margin-top: 16px; font-size: 0.875rem; }
        a { color: #38bdf8; }
        ul { line-height: 1.8; }
    </style>
</head>
<body>
<div class="card">
    <h1 class="ok">✓ Duplicate companies merged</h1>
    <ul>
        <li>Groups merged: <strong><?php echo (int)$stats['merged']; ?></strong></li>
        <li>Duplicate rows deleted: <strong><?php echo (int)$stats['deleted']; ?></strong></li>
        <li>Companies kept: <strong><?php echo (int)$stats['kept']; ?></strong></li>
        <li>Total companies now: <strong><?php echo $count; ?></strong></li>
    </ul>
    <p>প্রতিটি কোম্পানি এখন ক্যাটাগরি লেভেলে — সব সাব-ক্যাটাগরিতে একই ব্র্যান্ড + এক ক্যাটালগ।</p>
    <p><a href="admin/categories.php?tab=companies">→ Admin Companies</a></p>
    <p class="warn"><strong>এখন install-nine.php ডিলিট করুন।</strong></p>
</div>
</body>
</html>
