<?php
require_once __DIR__ . '/../config/master_database.php';
session_start();
if (empty($_SESSION['super_admin_id'])) { header('Location: login.php'); exit; }

if (($_GET['action'] ?? '') === 'toggle' && !empty($_GET['id'])) {
    $pdoMaster->prepare("UPDATE tenants SET status = IF(status='active','suspended','active') WHERE id = ?")
        ->execute([(int)$_GET['id']]);
    header('Location: dashboard.php'); exit;
}

$companies = $pdoMaster->query("SELECT * FROM tenants ORDER BY created_at DESC")->fetchAll();
$fullCount = 0; $limitedCount = 0;
foreach ($companies as $c) { $c['edition'] === 'limited' ? $limitedCount++ : $fullCount++; }
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Super Admin - Companies</title>
<link href="https://fonts.googleapis.com/css2?family=Fraunces:wght@600;700&family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="../assets/css/style.css?v=5">
</head>
<body style="padding:40px;max-width:1100px;margin:0 auto;">
    <div style="display:flex;justify-content:space-between;align-items:center;">
        <h2 style="font-family:var(--font-display);">Companies (<?= count($companies) ?>) <span class="text-faint" style="font-size:14px;font-weight:400;">— <?= $fullCount ?> Full, <?= $limitedCount ?> Limited</span></h2>
        <div>
            <a href="company_create.php" class="btn btn-primary">+ New Company</a>
            <a href="run_tenant_migration.php" class="btn" onclick="return confirm('Run the license/single-device-login migration on every existing company database? Safe to re-run.')">Run Migration on All Companies</a>
            <a href="logout.php" class="btn">Logout</a>
        </div>
    </div>

    <table style="width:100%;border-collapse:collapse;margin-top:20px;">
        <thead>
            <tr style="text-align:left;border-bottom:2px solid #ddd;">
                <th style="padding:10px;">Company</th>
                <th style="padding:10px;">Code</th>
                <th style="padding:10px;">Edition</th>
                <th style="padding:10px;">License</th>
                <th style="padding:10px;">Database</th>
                <th style="padding:10px;">Status</th>
                <th style="padding:10px;">Created</th>
                <th style="padding:10px;"></th>
            </tr>
        </thead>
        <tbody>
        <?php foreach ($companies as $c): ?>
            <tr style="border-bottom:1px solid #eee;">
                <td style="padding:10px;"><?= htmlspecialchars($c['company_name']) ?></td>
                <td style="padding:10px;"><?= htmlspecialchars($c['company_code']) ?></td>
                <td style="padding:10px;">
                    <span class="badge <?= $c['edition'] === 'limited' ? 'badge-info' : 'badge-success' ?>">
                        <?= $c['edition'] === 'limited' ? 'Limited' : 'Full' ?>
                    </span>
                </td>
                <td style="padding:10px;"><?= (int)$c['license_count'] ?> users</td>
                <td style="padding:10px;font-family:monospace;font-size:12px;"><?= htmlspecialchars($c['db_name']) ?></td>
                <td style="padding:10px;"><?= htmlspecialchars($c['status']) ?></td>
                <td style="padding:10px;"><?= htmlspecialchars($c['created_at']) ?></td>
                <td style="padding:10px;white-space:nowrap;">
                    <a href="company_edit.php?id=<?= $c['id'] ?>">Edit License</a> |
                    <a href="?action=toggle&id=<?= $c['id'] ?>" onclick="return confirm('Toggle status for this company?')">
                        <?= $c['status'] === 'active' ? 'Suspend' : 'Activate' ?>
                    </a>
                </td>
            </tr>
        <?php endforeach; ?>
        <?php if (!$companies): ?>
            <tr><td colspan="8" style="padding:20px;text-align:center;color:#888;">No companies yet. Create your first one.</td></tr>
        <?php endif; ?>
        </tbody>
    </table>
</body>
</html>
