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

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

$vehicles    = Vehicle::byOwner($user['id']);
$totalV      = count($vehicles);
$validDocs   = Document::countValidByOwner($user['id']);
$expiredDocs = Document::countExpiredByOwner($user['id']);
$expiringDocs= Document::countExpiringByOwner($user['id']);
$docAlerts   = Document::alertsByOwner($user['id']);

// Count unique drivers
$driverIds = [];
foreach($vehicles as $v) {
    foreach(Vehicle::getDrivers($v['id']) as $d) $driverIds[$d['id']] = true;
}
$totalDrivers = count($driverIds);

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

<div class="page-header">
  <h2><?= t('welcome') ?>, <?= e(explode(' ',$user['name'])[0]) ?> 👋</h2>
  <p style="color:var(--text-muted);font-size:14px">
    <?= date('Y-m-d') ?>
  </p>
</div>

<!-- Stats -->
<div class="stats-grid">
  <div class="stat-card">
    <div class="stat-icon blue">🚗</div>
    <div><div class="stat-value"><?= $totalV ?></div><div class="stat-label"><?= t('total_vehicles') ?></div></div>
  </div>
  <div class="stat-card">
    <div class="stat-icon green">👥</div>
    <div><div class="stat-value"><?= $totalDrivers ?></div><div class="stat-label"><?= t('total_drivers') ?></div></div>
  </div>
  <div class="stat-card">
    <div class="stat-icon green">✅</div>
    <div><div class="stat-value"><?= $validDocs ?></div><div class="stat-label"><?= t('valid_docs') ?></div></div>
  </div>
  <div class="stat-card">
    <div class="stat-icon red">⚠️</div>
    <div><div class="stat-value"><?= $expiredDocs ?></div><div class="stat-label"><?= t('expired_docs') ?></div></div>
  </div>
</div>

<div style="display:grid;grid-template-columns:1fr 1fr;gap:20px">
  <!-- Quick actions -->
  <div class="card">
    <div class="card-header"><span class="card-title"><?= t('quick_actions') ?></span></div>
    <div class="card-body" style="display:flex;flex-direction:column;gap:10px">
      <a href="<?= BASE_URL ?>/owner/vehicles.php?action=add" class="btn btn-primary">🚗 <?= t('add_vehicle') ?></a>
      <a href="<?= BASE_URL ?>/owner/drivers.php"   class="btn btn-secondary">👥 <?= t('my_drivers') ?></a>
      <a href="<?= BASE_URL ?>/owner/documents.php" class="btn btn-secondary">📄 <?= t('upload_doc') ?></a>
      <a href="<?= BASE_URL ?>/owner/personal.php" class="btn btn-secondary">🪪 <?= t('my_documents') ?></a>
      <a href="<?= BASE_URL ?>/owner/qr.php"        class="btn btn-secondary">📱 <?= t('generate_qr') ?></a>
    </div>
  </div>

  <!-- Recent vehicles -->
  <div class="card">
    <div class="card-header">
      <span class="card-title"><?= t('my_vehicles') ?></span>
      <a href="<?= BASE_URL ?>/owner/vehicles.php" class="btn btn-sm btn-secondary"><?= t('all') ?></a>
    </div>
    <div style="padding:0">
      <?php if(empty($vehicles)): ?>
        <div class="empty-state" style="padding:28px">
          <div class="empty-state-icon">🚗</div>
          <p><?= t('no_vehicles') ?></p>
        </div>
      <?php else: ?>
        <?php foreach(array_slice($vehicles,0,4) as $v):
            $docs = Document::byVehicle($v['id']);
            $hasExp = in_array('expired', array_column($docs,'status'));
        ?>
        <div class="verify-row">
          <div>
            <div class="font-bold"><?= e($v['brand'].' '.$v['model']) ?></div>
            <span class="vehicle-plate-badge"><?= e($v['plate_number']) ?></span>
          </div>
          <div style="text-align:end">
            <?= $hasExp ? '<span class="badge badge-danger">⚠ '.t('doc_expired').'</span>' : '<span class="badge badge-success">✓ OK</span>' ?>
            <?php if($v['driver_name']): ?>
            <div style="font-size:12px;color:var(--text-muted);margin-top:3px">👤 <?= e($v['driver_name']) ?></div>
            <?php endif; ?>
          </div>
        </div>
        <?php endforeach; ?>
      <?php endif; ?>
    </div>
  </div>
</div>

<?php if($expiredDocs > 0 || $expiringDocs > 0): ?>
<div class="card" style="margin-top:20px;border:1px solid <?= $expiredDocs>0?'var(--danger)':'var(--warning,#f59e0b)' ?>">
  <div class="card-header">
    <span class="card-title">🔔 <?= t('docs_need_attention') ?></span>
    <div>
      <?php if($expiredDocs>0): ?><span class="badge badge-danger"><?= t('expired_docs') ?>: <?= $expiredDocs ?></span><?php endif; ?>
      <?php if($expiringDocs>0): ?><span class="badge badge-warning"><?= t('expiring_soon') ?>: <?= $expiringDocs ?></span><?php endif; ?>
    </div>
  </div>
  <div style="padding:0">
    <?php foreach(array_slice($docAlerts,0,6) as $a):
      $st = $a['status']; ?>
    <div class="verify-row">
      <div>
        <span class="vehicle-plate-badge"><?= e($a['plate_number']) ?></span>
        <span style="margin-inline-start:8px;font-size:13px"><?= e($a['brand'].' '.$a['model']) ?></span>
      </div>
      <div style="text-align:end">
        <?= statusBadge($st,lang()) ?>
        <div style="font-size:11px;color:var(--text-muted);margin-top:3px"><?= t('expiry_date') ?>: <?= e($a['expiry_date']) ?></div>
      </div>
    </div>
    <?php endforeach; ?>
    <div style="padding:12px 16px">
      <a href="<?= BASE_URL ?>/owner/documents.php" class="btn btn-sm btn-primary"><?= t('view') ?> →</a>
    </div>
  </div>
</div>
<?php endif; ?>

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