<?= $this->extend('admin/layouts/main') ?>
<?= $this->section('content') ?>

<div class="row g-3 mb-4">
    <div class="col-6 col-lg-3">
        <div class="stat-card d-flex align-items-start justify-content-between">
            <div>
                <div class="stat-label">Hotels</div>
                <div class="stat-value"><?= (int) $totalHotels ?></div>
                <div class="text-muted small"><?= (int) $activeHotels ?> active · <?= (int) $suspendedHotels ?> suspended</div>
            </div>
            <div class="stat-icon" style="background: color-mix(in srgb, var(--brand) 12%, white); color: var(--brand);">
                <i class="bi bi-buildings"></i>
            </div>
        </div>
    </div>
    <div class="col-6 col-lg-3">
        <div class="stat-card d-flex align-items-start justify-content-between">
            <div>
                <div class="stat-label">Active Subscriptions</div>
                <div class="stat-value"><?= (int) $activeSubs ?></div>
                <div class="text-muted small"><?= (int) $trialSubs ?> trial · <?= (int) $pastDueSubs ?> past due</div>
            </div>
            <div class="stat-icon" style="background: #dcfce7; color: #16a34a;">
                <i class="bi bi-check-circle"></i>
            </div>
        </div>
    </div>
    <div class="col-6 col-lg-3">
        <div class="stat-card d-flex align-items-start justify-content-between">
            <div>
                <div class="stat-label">Est. MRR</div>
                <div class="stat-value"><?= format_money($mrr) ?></div>
                <div class="text-muted small">Active subscriptions only</div>
            </div>
            <div class="stat-icon" style="background: #fef3c7; color: #d97706;">
                <i class="bi bi-graph-up-arrow"></i>
            </div>
        </div>
    </div>
    <div class="col-6 col-lg-3">
        <div class="stat-card d-flex align-items-start justify-content-between">
            <div>
                <div class="stat-label">Outstanding</div>
                <div class="stat-value"><?= format_money($totalOutstanding) ?></div>
                <div class="text-muted small"><?= (int) $overdueCount ?> overdue invoice(s)</div>
            </div>
            <div class="stat-icon" style="background: #fee2e2; color: #dc2626;">
                <i class="bi bi-exclamation-circle"></i>
            </div>
        </div>
    </div>
</div>

<div class="row g-3 mb-4">
    <div class="col-lg-4">
        <div class="card h-100">
            <div class="card-body">
                <div class="d-flex align-items-center gap-2 mb-2">
                    <i class="bi bi-cash-stack text-success"></i>
                    <span class="fw-semibold small text-uppercase text-muted">Total Collected</span>
                </div>
                <div class="fs-3 fw-bold"><?= format_money($totalCollected) ?></div>
                <div class="text-muted small">Lifetime, all paid invoices</div>
            </div>
        </div>
    </div>
    <div class="col-lg-8">
        <div class="card h-100">
            <div class="card-header">Quick Actions</div>
            <div class="card-body d-flex flex-wrap gap-2">
                <a href="<?= site_url('admin/tenants/new') ?>" class="btn btn-primary btn-sm"><i class="bi bi-plus-lg me-1"></i>Onboard Hotel</a>
                <a href="<?= site_url('admin/plans/new') ?>" class="btn btn-outline-primary btn-sm"><i class="bi bi-tag me-1"></i>New Plan</a>
                <a href="<?= site_url('admin/tenants') ?>" class="btn btn-outline-primary btn-sm"><i class="bi bi-buildings me-1"></i>Manage Hotels</a>
                <a href="<?= site_url('admin/invoices') ?>" class="btn btn-outline-primary btn-sm"><i class="bi bi-receipt me-1"></i>View Invoices</a>
                <a href="<?= site_url('admin/system-health') ?>" class="btn btn-outline-primary btn-sm"><i class="bi bi-heart-pulse me-1"></i>System Health</a>
            </div>
        </div>
    </div>
</div>

<div class="row g-3">
    <div class="col-lg-7">
        <div class="card">
            <div class="card-header d-flex justify-content-between align-items-center">
                Recent Hotels
                <a href="<?= site_url('admin/tenants') ?>" class="small text-decoration-none">View all</a>
            </div>
            <div class="table-responsive">
                <table class="table mb-0 align-middle">
                    <thead><tr><th>Hotel</th><th>Slug</th><th>Status</th><th>Onboarded</th></tr></thead>
                    <tbody>
                    <?php if (empty($recentHotels)): ?>
                        <tr><td colspan="4" class="text-center text-muted py-4">No hotels yet.</td></tr>
                    <?php endif; ?>
                    <?php foreach ($recentHotels as $t): ?>
                        <tr>
                            <td><a href="<?= site_url('admin/tenants/' . $t->id) ?>" class="text-decoration-none fw-semibold"><?= esc($t->hotel_name) ?></a></td>
                            <td class="text-muted"><?= esc($t->slug) ?></td>
                            <td><span class="badge bg-<?= $t->status === 'active' ? 'success' : 'danger' ?>"><?= esc(ucfirst($t->status)) ?></span></td>
                            <td class="text-muted small"><?= esc(date('d M Y', strtotime($t->created_at))) ?></td>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <div class="col-lg-5">
        <div class="card">
            <div class="card-header d-flex justify-content-between align-items-center">
                Recent Activity
                <a href="<?= site_url('admin/activity-log') ?>" class="small text-decoration-none">View all</a>
            </div>
            <ul class="list-group list-group-flush">
                <?php if (empty($recentActivity)): ?>
                    <li class="list-group-item text-center text-muted py-4">No activity yet.</li>
                <?php endif; ?>
                <?php foreach ($recentActivity as $entry): ?>
                    <li class="list-group-item small">
                        <div><?= esc($entry->action) ?></div>
                        <div class="text-muted" style="font-size: 0.75rem;"><?= esc(date('d M Y, g:i a', strtotime($entry->created_at))) ?></div>
                    </li>
                <?php endforeach; ?>
            </ul>
        </div>
    </div>
</div>

<?= $this->endSection() ?>
