<?php
require_once __DIR__.'/../../config/app.php';
require_once __DIR__.'/../../app/Models/Admin.php';
require_once __DIR__.'/../../app/Models/Document.php';

$user = requireAuth(['admin']);
Document::refreshStatuses();

$c   = Admin::counts();
$ru  = Admin::recentUsers(6);
$rv  = Admin::recentViolations(6);

$roleLabels = ['owner'=>t('owner_role'),'driver'=>t('driver_role'),'officer'=>t('officer_role'),'admin'=>t('admin_role')];

$pageTitle  = t('dashboard');
$activePage = BASE_URL.'/admin/dashboard.php';
require __DIR__.'/../../app/Views/shared/layout.php';
?>

<div class="page-header" style="display:flex;justify-content:space-between;align-items:center;flex-wrap:wrap;gap:10px">
  <div>
    <h2><?= t('platform_overview') ?> 🛠️</h2>
    <p style="color:var(--text-muted);font-size:14px"><?= date('Y-m-d H:i') ?></p>
  </div>
  <a href="<?= BASE_URL ?>/admin/analytics.php" class="btn btn-primary">📈 <?= t('analytics') ?></a>
</div>

<?php if(($c['docs']['expired']??0) > 0 || ($c['docs']['expiring']??0) > 0): ?>
<div class="alert alert-warning" style="display:flex;gap:14px;flex-wrap:wrap;align-items:center">
  🔔 <strong><?= t('docs_need_attention') ?>:</strong>
  <?php if(($c['docs']['expired']??0)>0): ?><span class="badge badge-danger"><?= t('expired_docs') ?>: <?= (int)$c['docs']['expired'] ?></span><?php endif; ?>
  <?php if(($c['docs']['expiring']??0)>0): ?><span class="badge badge-warning"><?= t('expiring_soon') ?>: <?= (int)$c['docs']['expiring'] ?></span><?php endif; ?>
  <a href="<?= BASE_URL ?>/admin/documents.php?status=expired" class="btn btn-sm btn-secondary"><?= t('view') ?> →</a>
</div>
<?php endif; ?>

<!-- Primary stats -->
<div class="stats-grid">
  <div class="stat-card">
    <div class="stat-icon blue">👥</div>
    <div><div class="stat-value"><?= $c['users_total'] ?></div><div class="stat-label"><?= t('total_users') ?></div></div>
  </div>
  <div class="stat-card">
    <div class="stat-icon blue">🚗</div>
    <div><div class="stat-value"><?= $c['vehicles'] ?></div><div class="stat-label"><?= t('all_vehicles') ?></div></div>
  </div>
  <div class="stat-card">
    <div class="stat-icon green">📄</div>
    <div><div class="stat-value"><?= $c['documents'] ?></div><div class="stat-label"><?= t('all_documents') ?></div></div>
  </div>
  <div class="stat-card">
    <div class="stat-icon red">📋</div>
    <div><div class="stat-value"><?= $c['violations'] ?></div><div class="stat-label"><?= t('all_violations') ?></div></div>
  </div>
  <div class="stat-card">
    <div class="stat-icon green">💰</div>
    <div><div class="stat-value"><?= number_format($c['revenue'],0) ?></div><div class="stat-label"><?= t('total_revenue') ?> (DZD)</div></div>
  </div>
  <div class="stat-card">
    <div class="stat-icon red">⚠️</div>
    <div><div class="stat-value"><?= $c['unpaid'] ?></div><div class="stat-label"><?= t('unpaid_violations') ?></div></div>
  </div>
  <div class="stat-card">
    <div class="stat-icon blue">🧩</div>
    <div><div class="stat-value"><?= $c['templates'] ?></div><div class="stat-label"><?= t('templates_count') ?></div></div>
  </div>
  <div class="stat-card">
    <div class="stat-icon red">🚛</div>
    <div><div class="stat-value"><?= $c['tows_active'] ?></div><div class="stat-label"><?= t('active_tows') ?></div></div>
  </div>
</div>

<div style="display:grid;grid-template-columns:1fr 1fr;gap:20px;margin-top:4px">

  <!-- Users by role -->
  <div class="card">
    <div class="card-header"><span class="card-title"><?= t('users_by_role') ?></span></div>
    <div class="card-body" style="display:flex;flex-direction:column;gap:10px">
      <?php foreach($c['roles'] as $role=>$cnt): ?>
      <div class="verify-row">
        <div class="font-bold"><?= e($roleLabels[$role] ?? $role) ?></div>
        <span class="badge badge-info"><?= $cnt ?></span>
      </div>
      <?php endforeach; ?>
    </div>
  </div>

  <!-- Recent users -->
  <div class="card">
    <div class="card-header">
      <span class="card-title"><?= t('recent_users') ?></span>
      <a href="<?= BASE_URL ?>/admin/users.php" class="btn btn-sm btn-secondary"><?= t('all') ?></a>
    </div>
    <div style="padding:0">
      <?php if(empty($ru)): ?>
        <div class="empty-state" style="padding:24px"><p><?= t('no_data') ?></p></div>
      <?php else: foreach($ru as $u): ?>
        <div class="verify-row">
          <div>
            <div class="font-bold"><?= e($u['name']) ?></div>
            <div style="font-size:12px;color:var(--text-muted)"><?= e($u['email']) ?></div>
          </div>
          <span class="badge badge-info"><?= e($roleLabels[$u['role']] ?? $u['role']) ?></span>
        </div>
      <?php endforeach; endif; ?>
    </div>
  </div>
</div>

<!-- Recent violations -->
<div class="card" style="margin-top:20px">
  <div class="card-header">
    <span class="card-title"><?= t('recent_violations') ?></span>
    <a href="<?= BASE_URL ?>/admin/violations.php" class="btn btn-sm btn-secondary"><?= t('all') ?></a>
  </div>
  <div style="padding:0">
    <?php if(empty($rv)): ?>
      <div class="empty-state" style="padding:24px"><p><?= t('no_data') ?></p></div>
    <?php else: foreach($rv as $v): ?>
      <div class="verify-row">
        <div>
          <span class="vehicle-plate-badge"><?= e($v['plate_number']) ?></span>
          <span style="margin-inline-start:8px"><?= e($v['violation_type']) ?></span>
        </div>
        <div style="text-align:end">
          <strong><?= number_format((float)$v['amount'],0) ?> DZD</strong>
          <?php $cls=['unpaid'=>'badge-danger','paid'=>'badge-success','cancelled'=>'badge-info'][$v['status']]??'badge-info'; ?>
          <span class="badge <?= $cls ?>" style="margin-inline-start:6px"><?= e($v['status']) ?></span>
        </div>
      </div>
    <?php endforeach; endif; ?>
  </div>
</div>

<?php require __DIR__.'/../../app/Views/shared/layout_end.php'; ?>
