<?php
require_once __DIR__ . '/../config/database.php';

requireLogin();
requireAnyRole(['admin', 'super_admin']);

$db = Database::getInstance();

$id = isset($_GET['id']) ? (int) $_GET['id'] : 0;
if ($id <= 0) {
    $_SESSION['flash']['danger'] = 'Invalid certificate ID.';
    header('Location: certificates.php');
    exit;
}

$cert = $db->get(
    "SELECT cert.*, c.title AS course_title, c.description AS course_description,
            u.username, u.email, u.first_name, u.last_name, u.region,
            ins.first_name AS ins_first_name, ins.last_name AS ins_last_name
     FROM certificates cert
     JOIN courses c ON cert.course_id = c.id
     JOIN users u ON cert.student_id = u.id
     LEFT JOIN users ins ON c.instructor_id = ins.id
     WHERE cert.id = ?",
    [$id]
);

if (!$cert) {
    $_SESSION['flash']['danger'] = 'Certificate not found.';
    header('Location: certificates.php');
    exit;
}

$pageTitle = 'View Certificate';
include __DIR__ . '/includes/header.php';
?>

<div class="d-flex justify-content-between align-items-center mb-4">
  <div>
    <h1 class="h4 mb-0">Certificate Details</h1>
    <p class="text-muted mb-0">View certificate information</p>
  </div>
  <div>
    <a href="certificates.php" class="btn btn-outline-secondary">
      <i class="fas fa-arrow-left me-2"></i>Back to Certificates
    </a>
  </div>
</div>

<div class="row">
  <div class="col-md-8">
    <div class="card">
      <div class="card-header">
        <h5 class="mb-0"><?php echo htmlspecialchars($cert['certificate_name'] ?? ''); ?></h5>
        <small class="text-muted">ID: #<?php echo (int)$cert['id']; ?></small>
      </div>
      <div class="card-body">
        <div class="row mb-3">
          <div class="col-sm-3"><strong>Student</strong></div>
          <div class="col-sm-9"><?php echo htmlspecialchars(($cert['first_name'] ?? '') . ' ' . ($cert['last_name'] ?? '')); ?></div>
        </div>
        <div class="row mb-3">
          <div class="col-sm-3"><strong>Username</strong></div>
          <div class="col-sm-9"><?php echo htmlspecialchars($cert['username'] ?? ''); ?></div>
        </div>
        <div class="row mb-3">
          <div class="col-sm-3"><strong>Email</strong></div>
          <div class="col-sm-9"><?php echo htmlspecialchars($cert['email'] ?? ''); ?></div>
        </div>
        <div class="row mb-3">
          <div class="col-sm-3"><strong>Region</strong></div>
          <div class="col-sm-9"><?php echo htmlspecialchars(ucfirst($cert['region'] ?? '')); ?></div>
        </div>
        <div class="row mb-3">
          <div class="col-sm-3"><strong>Course</strong></div>
          <div class="col-sm-9"><?php echo htmlspecialchars($cert['course_title'] ?? ''); ?></div>
        </div>
        <div class="row mb-3">
          <div class="col-sm-3"><strong>Instructor</strong></div>
          <div class="col-sm-9"><?php echo htmlspecialchars(($cert['ins_first_name'] ?? '') . ' ' . ($cert['ins_last_name'] ?? '')); ?></div>
        </div>
        <div class="row mb-3">
          <div class="col-sm-3"><strong>Status</strong></div>
          <div class="col-sm-9">
            <span class="badge bg-<?php echo $cert['is_revoked'] ? 'danger' : 'success'; ?>">
              <?php echo $cert['is_revoked'] ? 'Revoked' : 'Active'; ?>
            </span>
          </div>
        </div>
        <div class="row mb-3">
          <div class="col-sm-3"><strong>Issue Date</strong></div>
          <div class="col-sm-9"><?php echo htmlspecialchars($cert['issue_date'] ?? ''); ?></div>
        </div>
        <?php if ($cert['is_revoked']): ?>
          <div class="row mb-3">
            <div class="col-sm-3"><strong>Revoked At</strong></div>
            <div class="col-sm-9"><?php echo htmlspecialchars($cert['revoked_at'] ?? ''); ?></div>
          </div>
        <?php endif; ?>
        <div class="row mb-3">
          <div class="col-sm-3"><strong>Verification Code</strong></div>
          <div class="col-sm-9"><code><?php echo htmlspecialchars($cert['verification_code'] ?? ''); ?></code></div>
        </div>
      </div>
    </div>
  </div>

  <div class="col-md-4">
    <div class="card">
      <div class="card-header">
        <h5 class="mb-0">Actions</h5>
      </div>
      <div class="card-body">
        <?php if (!$cert['is_revoked']): ?>
          <a href="certificates_revoke.php?id=<?php echo (int)$cert['id']; ?>" class="btn btn-danger w-100 mb-2" onclick="return confirm('Revoke this certificate?');">
            <i class="fas fa-ban me-2"></i>Revoke Certificate
          </a>
        <?php endif; ?>
        <a href="#" class="btn btn-outline-primary w-100 mb-2" onclick="window.print(); return false;">
          <i class="fas fa-print me-2"></i>Print Certificate
        </a>
        <a href="certificates_download.php?id=<?php echo (int)$cert['id']; ?>" class="btn btn-outline-secondary w-100">
          <i class="fas fa-download me-2"></i>Download PDF
        </a>
      </div>
    </div>
  </div>
</div>

<div class="row mt-4">
  <div class="col-12">
    <div class="card">
      <div class="card-header">
        <h5 class="mb-0">Certificate Preview</h5>
      </div>
      <div class="card-body text-center p-5" style="border: 2px dashed #ddd; background: #f9f9f9;">
        <h2 class="mb-3">Certificate of Completion</h2>
        <p class="lead mb-4">This is to certify that</p>
        <h3 class="mb-4" style="border-bottom: 2px solid #333; display: inline-block; padding-bottom: 5px;">
          <?php echo htmlspecialchars(($cert['first_name'] ?? '') . ' ' . ($cert['last_name'] ?? '')); ?>
        </h3>
        <p class="lead mb-4">has successfully completed the course</p>
        <h4 class="mb-4"><?php echo htmlspecialchars($cert['course_title'] ?? ''); ?></h4>
        <p class="mb-4">on <?php echo htmlspecialchars($cert['issue_date'] ?? ''); ?></p>
        <p class="text-muted small">Verification Code: <code><?php echo htmlspecialchars($cert['verification_code'] ?? ''); ?></code></p>
      </div>
    </div>
  </div>
</div>

<?php include __DIR__ . '/includes/footer.php'; ?>
