<?php
declare(strict_types=1);

require_once dirname(__DIR__) . '/lib/tenant.php';
require_once dirname(__DIR__) . '/lib/admin_layout.php';

require_admin();
$tenant = get_tenant();
$metrics = db()->prepare('SELECT * FROM license_metrics WHERE tenant_id = ?');
$metrics->execute([current_tenant_id()]);
$m = $metrics->fetch() ?: [];

$counts = [
    'services' => (int) db()->query('SELECT COUNT(*) FROM services WHERE tenant_id = ' . (int) current_tenant_id())->fetchColumn(),
    'staff' => (int) db()->query('SELECT COUNT(*) FROM staff_barbers WHERE tenant_id = ' . (int) current_tenant_id())->fetchColumn(),
    'styles' => (int) db()->query('SELECT COUNT(*) FROM style_gallery WHERE tenant_id = ' . (int) current_tenant_id())->fetchColumn(),
    'media' => (int) db()->query('SELECT COUNT(*) FROM media_assets WHERE tenant_id = ' . (int) current_tenant_id())->fetchColumn(),
];

admin_header('Dashboard', 'dashboard');
?>
<div class="stats">
  <div class="stat"><strong><?= $counts['services'] ?></strong><span>Services</span></div>
  <div class="stat"><strong><?= $counts['staff'] ?></strong><span>Barbers</span></div>
  <div class="stat"><strong><?= $counts['styles'] ?></strong><span>Style looks</span></div>
  <div class="stat"><strong><?= $counts['media'] ?></strong><span>Assets</span></div>
</div>

<div class="panel" style="margin-top:1.25rem">
  <h2>New here?</h2>
  <p class="muted" style="margin:0 0 .75rem">Follow the startup checklist for branding, media, services, styles, and the booking calendar.</p>
  <a class="btn" href="<?= APP_BASE ?>/admin/help.php">Open Getting Started</a>
</div>

<div class="panel" style="margin-top:1.25rem">
  <h2>License metrics</h2>
  <div class="grid-3">
    <div>
      <label>Shop</label>
      <p><strong><?= htmlspecialchars((string) ($tenant['name'] ?? '')) ?></strong></p>
    </div>
    <div>
      <label>Subscription</label>
      <p><strong><?= htmlspecialchars((string) ($m['subscription_type'] ?? 'starter')) ?></strong>
        · <?= htmlspecialchars((string) ($tenant['license_status'] ?? '')) ?></p>
    </div>
    <div>
      <label>Renewal</label>
      <p><strong><?= htmlspecialchars((string) ($m['renewal_date'] ?? '—')) ?></strong></p>
    </div>
  </div>
  <div class="stats" style="margin-top:1rem">
    <div class="stat"><strong><?= (int) ($m['sms_count_month'] ?? 0) ?></strong><span>SMS this month</span></div>
    <div class="stat"><strong><?= (int) ($m['email_count_month'] ?? 0) ?></strong><span>Emails this month</span></div>
    <div class="stat"><strong><?= (int) ($m['booking_count_month'] ?? 0) ?></strong><span>Bookings this month</span></div>
    <div class="stat"><strong><?= htmlspecialchars((string) ($tenant['subdomain'] ?? '')) ?></strong><span>Subdomain</span></div>
  </div>
</div>

<div class="panel">
  <h2>Structured content rule</h2>
  <p class="muted" style="margin:0">
    This admin uses fixed forms and media pickers — not a free-form page builder.
    Data feeds modular public components for hero, gallery, services, and staff.
  </p>
</div>
<?php admin_footer(); ?>
