<?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 assessment ID.';
    header('Location: assessments.php');
    exit;
}

$assessment = $db->get(
    "SELECT a.*, c.title AS course_title, u.first_name, u.last_name
     FROM assessments a
     LEFT JOIN courses c ON a.course_id = c.id
     LEFT JOIN users u ON a.created_by = u.id
     WHERE a.id = ?",
    [$id]
);

if (!$assessment) {
    $_SESSION['flash']['danger'] = 'Assessment not found.';
    header('Location: assessments.php');
    exit;
}

$submissions = $db->getAll(
    "SELECT s.*, u.username, u.first_name, u.last_name
     FROM assessment_submissions s
     JOIN users u ON s.student_id = u.id
     WHERE s.assessment_id = ?
     ORDER BY s.submitted_at DESC",
    [$id]
);

$questions = $db->getAll(
    "SELECT * FROM assessment_questions WHERE assessment_id = ? ORDER BY question_order",
    [$id]
);

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

<div class="d-flex justify-content-between align-items-center mb-4">
  <div>
    <h1 class="h4 mb-0">Assessment Details</h1>
    <p class="text-muted mb-0">View assessment information and submissions</p>
  </div>
  <div>
    <a href="assessments_edit.php?id=<?php echo $id; ?>" class="btn btn-primary me-2">
      <i class="fas fa-edit me-2"></i>Edit
    </a>
    <a href="assessments.php" class="btn btn-outline-secondary">
      <i class="fas fa-arrow-left me-2"></i>Back to Assessments
    </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($assessment['title'] ?? ''); ?></h5>
        <small class="text-muted">Type: <?php echo htmlspecialchars(ucfirst($assessment['assessment_type'] ?? '')); ?></small>
      </div>
      <div class="card-body">
        <div class="row mb-3">
          <div class="col-sm-3"><strong>Course</strong></div>
          <div class="col-sm-9"><?php echo htmlspecialchars($assessment['course_title'] ?? ''); ?></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 $assessment['status']==='published'?'success':($assessment['status']==='draft'?'warning':'secondary'); ?>">
              <?php echo htmlspecialchars(ucfirst($assessment['status'] ?? '')); ?>
            </span>
          </div>
        </div>
        <div class="row mb-3">
          <div class="col-sm-3"><strong>Passing Score</strong></div>
          <div class="col-sm-9"><?php echo (int)($assessment['passing_score'] ?? 0); ?>%</div>
        </div>
        <div class="row mb-3">
          <div class="col-sm-3"><strong>Time Limit</strong></div>
          <div class="col-sm-9"><?php echo (int)($assessment['time_limit'] ?? 0) > 0 ? (int)($assessment['time_limit'] ?? 0) . ' minutes' : 'Unlimited'; ?></div>
        </div>
        <div class="row mb-3">
          <div class="col-sm-3"><strong>Max Attempts</strong></div>
          <div class="col-sm-9"><?php echo (int)($assessment['max_attempts'] ?? 0); ?></div>
        </div>
        <div class="row mb-3">
          <div class="col-sm-3"><strong>Created By</strong></div>
          <div class="col-sm-9"><?php echo htmlspecialchars(($assessment['first_name'] ?? '') . ' ' . ($assessment['last_name'] ?? '')); ?></div>
        </div>
        <div class="row mb-3">
          <div class="col-sm-3"><strong>Description</strong></div>
          <div class="col-sm-9"><?php echo nl2br(htmlspecialchars($assessment['description'] ?? '')); ?></div>
        </div>
      </div>
    </div>
  </div>

  <div class="col-md-4">
    <div class="card">
      <div class="card-header">
        <h5 class="mb-0">Stats</h5>
      </div>
      <div class="card-body">
        <div class="row text-center">
          <div class="col-6">
            <h4 class="mb-1"><?php echo count($submissions); ?></h4>
            <small class="text-muted">Submissions</small>
          </div>
          <div class="col-6">
            <h4 class="mb-1"><?php echo count($questions); ?></h4>
            <small class="text-muted">Questions</small>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

<div class="row mt-4">
  <div class="col-12">
    <div class="card">
      <div class="card-header">
        <h5 class="mb-0">Recent Submissions</h5>
      </div>
      <div class="card-body">
        <?php if (!$submissions): ?>
          <p class="text-muted">No submissions yet.</p>
        <?php else: ?>
          <div class="table-responsive">
            <table class="table table-striped">
              <thead>
                <tr>
                  <th>Student</th>
                  <th>Score</th>
                  <th>Status</th>
                  <th>Submitted</th>
                </tr>
              </thead>
              <tbody>
                <?php foreach ($submissions as $s): ?>
                  <tr>
                    <td><?php echo htmlspecialchars(($s['first_name'] ?? '') . ' ' . ($s['last_name'] ?? '')); ?></td>
                    <td><?php echo (int)($s['score'] ?? 0); ?>%</td>
                    <td>
                      <span class="badge bg-<?php echo ($s['score'] ?? 0) >= ($assessment['passing_score'] ?? 70) ? 'success' : 'danger'; ?>">
                        <?php echo ($s['score'] ?? 0) >= ($assessment['passing_score'] ?? 70) ? 'Passed' : 'Failed'; ?>
                      </span>
                    </td>
                    <td><?php echo htmlspecialchars($s['submitted_at'] ?? ''); ?></td>
                  </tr>
                <?php endforeach; ?>
              </tbody>
            </table>
          </div>
        <?php endif; ?>
      </div>
    </div>
  </div>
</div>

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