<?php
declare(strict_types=1);
require_once __DIR__ . '/queue_bootstrap.php';
qh_boot();
qh_admin_handle_request();

$stats = qh_admin_stats();
$clinics = qh_fetch_clinics();
$doctors = qh_fetch_doctors();
$recentClinics = array_slice($clinics, 0, 4);
$recentDoctors = array_slice($doctors, 0, 5);
$profile = qh_fetch_hospital_profile();

qh_page_start(
    'admin',
    qh_t('Admin overview', 'نظرة عامة للإدارة'),
    qh_t('Structured admin overview with separate pages for clinics and doctors.', 'نظرة عامة منظمة للإدارة مع صفحات مستقلة للعيادات والأطباء.')
);
?>
<div class="container-fluid container-xxl px-3 px-lg-4">
  <div class="admin-layout">
    <aside class="admin-sidebar-column">
      <?php qh_render_admin_sidebar('admin.php', $stats); ?>
    </aside>

    <div class="admin-content-stack">
      <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
          <h1 class="h3 mb-0 text-gray-800 fw-bold"><?= qh_h(qh_t('Admin Dashboard', 'لوحة تحكم الإدارة')) ?></h1>
          <p class="text-muted mb-0 mt-1"><?= qh_h(qh_t('System configuration and management.', 'إعدادات وإدارة النظام.')) ?></p>
        </div>
      </div>

      <div class="row g-4 mb-4">
        <div class="col-md-3 col-sm-6">
          <div class="card shadow-sm border-0 h-100">
            <div class="card-body">
              <div class="text-xs font-weight-bold text-primary text-uppercase mb-1 small"><?= qh_h(qh_t('Clinics', 'العيادات')) ?></div>
              <div class="h3 mb-0 font-weight-bold text-gray-800"><?= qh_h((string) $stats['clinics']) ?></div>
            </div>
          </div>
        </div>
        <div class="col-md-3 col-sm-6">
          <div class="card shadow-sm border-0 h-100">
            <div class="card-body">
              <div class="text-xs font-weight-bold text-success text-uppercase mb-1 small"><?= qh_h(qh_t('Doctors', 'الأطباء')) ?></div>
              <div class="h3 mb-0 font-weight-bold text-gray-800"><?= qh_h((string) $stats['doctors']) ?></div>
            </div>
          </div>
        </div>
        <div class="col-md-3 col-sm-6">
          <div class="card shadow-sm border-0 h-100">
            <div class="card-body">
              <div class="text-xs font-weight-bold text-warning text-uppercase mb-1 small"><?= qh_h(qh_t('Vitals Clinics', 'عيادات العلامات')) ?></div>
              <div class="h3 mb-0 font-weight-bold text-gray-800"><?= qh_h((string) $stats['vitals_clinics']) ?></div>
            </div>
          </div>
        </div>
        <div class="col-md-3 col-sm-6">
          <div class="card shadow-sm border-0 h-100">
            <div class="card-body">
              <div class="text-xs font-weight-bold text-info text-uppercase mb-1 small"><?= qh_h(qh_t('Direct Clinics', 'عيادات مباشرة')) ?></div>
              <div class="h3 mb-0 font-weight-bold text-gray-800"><?= qh_h((string) $stats['direct_clinics']) ?></div>
            </div>
          </div>
        </div>
      </div>

      <div class="row g-4 mb-4">
        <div class="col-lg-4">
          <div class="card shadow-sm border-0 h-100">
            <div class="card-header bg-white border-bottom py-3 d-flex justify-content-between align-items-center">
              <h5 class="mb-0 font-weight-bold text-dark"><?= qh_h(qh_t('Hospital Profile', 'ملف المستشفى')) ?></h5>
              <a class="btn btn-sm btn-outline-secondary shadow-sm" href="<?= qh_h(qh_url('admin_hospital.php')) ?>"><?= qh_h(qh_t('Edit', 'تعديل')) ?></a>
            </div>
            <div class="card-body">
              <div class="d-flex flex-column gap-3">
                <div>
                  <div class="fw-semibold text-dark"><?= qh_h(qh_hospital_name()) ?></div>
                  <div class="small text-muted"><?= qh_h((string) ($profile['short_name'] ?? qh_t('No short name', 'لا يوجد اسم مختصر'))) ?></div>
                </div>
                <div>
                  <div class="small text-muted mb-1"><?= qh_h(qh_t('Phone', 'الهاتف')) ?></div>
                  <div class="fw-semibold text-dark"><?= qh_h((string) (($profile['phone'] ?? '') !== '' ? $profile['phone'] : '--')) ?></div>
                </div>
                <div>
                  <div class="small text-muted mb-1"><?= qh_h(qh_t('Working hours', 'ساعات العمل')) ?></div>
                  <div class="fw-semibold text-dark"><?= qh_h(qh_name($profile, 'working_hours', '--')) ?></div>
                </div>
              </div>
            </div>
          </div>
        </div>

        <div class="col-lg-4">
          <div class="card shadow-sm border-0 h-100">
            <div class="card-header bg-white border-bottom py-3 d-flex justify-content-between align-items-center">
              <h5 class="mb-0 font-weight-bold text-dark"><?= qh_h(qh_t('Recent Clinics', 'أحدث العيادات')) ?></h5>
              <a class="btn btn-sm btn-outline-secondary shadow-sm" href="<?= qh_h(qh_url('admin_clinics.php')) ?>"><?= qh_h(qh_t('Manage', 'إدارة')) ?></a>
            </div>
            <div class="card-body p-0">
              <?php if ($recentClinics === []): ?>
                <div class="text-center py-4 text-muted"><p class="mb-0 small"><?= qh_h(qh_t('No clinics yet.', 'لا توجد عيادات.')) ?></p></div>
              <?php else: ?>
                <div class="list-group list-group-flush">
                  <?php foreach ($recentClinics as $clinic): ?>
                    <div class="list-group-item d-flex justify-content-between align-items-center py-3 border-0 border-bottom">
                      <div>
                        <div class="fw-semibold text-dark mb-1"><?= qh_h(qh_name($clinic)) ?></div>
                        <div class="small text-muted"><?= qh_h(qh_t('Code', 'الرمز')) ?>: <?= qh_h((string) $clinic['code']) ?></div>
                      </div>
                      <span class="badge bg-<?= (int) $clinic['requires_vitals'] === 1 ? 'warning text-dark' : 'info text-dark' ?> rounded-pill px-2 py-1">
                        <?= qh_h((int) $clinic['requires_vitals'] === 1 ? qh_t('Vitals', 'علامات') : qh_t('Direct', 'مباشر')) ?>
                      </span>
                    </div>
                  <?php endforeach; ?>
                </div>
              <?php endif; ?>
            </div>
          </div>
        </div>

        <div class="col-lg-4">
          <div class="card shadow-sm border-0 h-100">
            <div class="card-header bg-white border-bottom py-3 d-flex justify-content-between align-items-center">
              <h5 class="mb-0 font-weight-bold text-dark"><?= qh_h(qh_t('Recent Doctors', 'أحدث الأطباء')) ?></h5>
              <a class="btn btn-sm btn-outline-secondary shadow-sm" href="<?= qh_h(qh_url('admin_doctors.php')) ?>"><?= qh_h(qh_t('Manage', 'إدارة')) ?></a>
            </div>
            <div class="card-body p-0">
              <?php if ($recentDoctors === []): ?>
                <div class="text-center py-4 text-muted"><p class="mb-0 small"><?= qh_h(qh_t('No doctors yet.', 'لا يوجد أطباء.')) ?></p></div>
              <?php else: ?>
                <div class="list-group list-group-flush">
                  <?php foreach ($recentDoctors as $doctor): ?>
                    <div class="list-group-item d-flex justify-content-between align-items-center py-3 border-0 border-bottom">
                      <div>
                        <div class="fw-semibold text-dark mb-1"><?= qh_h(qh_name($doctor)) ?></div>
                        <div class="small text-muted"><?= qh_h(qh_name($doctor, 'clinic_name', qh_t('Unassigned', 'غير محدد'))) ?></div>
                      </div>
                      <span class="badge bg-light text-dark border px-2 py-1"><?= qh_h(qh_t('Rm', 'غرفة')) ?> <?= qh_h((string) $doctor['room_number']) ?></span>
                    </div>
                  <?php endforeach; ?>
                </div>
              <?php endif; ?>
            </div>
          </div>
        </div>
      </div>

    </div>
  </div>
</div>
<?php qh_page_end(); ?>