<?php
// /elms/lecturer/submissions.php
declare(strict_types=1);

require_once __DIR__ . '/../includes/lecturer_auth.php';

$PAGE_TITLE = 'Submissions';
$ACTIVE = 'submissions';

function tbl_exists_sub(mysqli $db, string $table): bool {
    $table = $db->real_escape_string($table);
    $res = $db->query("SHOW TABLES LIKE '{$table}'");
    return $res && $res->num_rows > 0;
}

function col_exists_sub(mysqli $db, string $table, string $col): bool {
    $table = str_replace('`', '``', $table);
    $col = $db->real_escape_string($col);
    $res = $db->query("SHOW COLUMNS FROM `{$table}` LIKE '{$col}'");
    return $res && $res->num_rows > 0;
}

$rows = [];

if (isset($mysqli) && $mysqli instanceof mysqli && tbl_exists_sub($mysqli, 'lms_submissions')) {
    $hasAssess = tbl_exists_sub($mysqli, 'lms_assessments');
    $studentTable = tbl_exists_sub($mysqli, 'studentss') ? 'studentss' : (tbl_exists_sub($mysqli, 'students') ? 'students' : null);

    $joinAssess = $hasAssess ? "LEFT JOIN lms_assessments a ON a.id = s.assessment_id" : "";
    $assTitle = $hasAssess && col_exists_sub($mysqli, 'lms_assessments', 'title') ? "a.title" : "CONCAT('#', s.assessment_id)";

    $joinStudent = $studentTable ? "LEFT JOIN {$studentTable} st ON st.id = s.student_id" : "";
    $stuNameCol = $studentTable && col_exists_sub($mysqli, $studentTable, 'full_name') ? 'full_name' : 'name';
    $stuName = $studentTable ? "COALESCE(st.{$stuNameCol}, CONCAT('#', s.student_id))" : "CONCAT('#', s.student_id)";

    $statusExpr = col_exists_sub($mysqli, 'lms_submissions', 'workflow_status') ? "s.workflow_status" : "'submitted'";
    $dateExpr = col_exists_sub($mysqli, 'lms_submissions', 'submitted_at') ? "s.submitted_at" :
        (col_exists_sub($mysqli, 'lms_submissions', 'created_at') ? "s.created_at" : "s.id");

    $sql = "
        SELECT 
            s.id,
            {$stuName} AS student_name,
            {$assTitle} AS assessment_title,
            s.batch_id,
            {$statusExpr} AS workflow_status,
            {$dateExpr} AS submitted_at
        FROM lms_submissions s
        {$joinAssess}
        {$joinStudent}
        ORDER BY s.id DESC
        LIMIT 100
    ";

    $res = $mysqli->query($sql);
    if ($res) {
        $rows = $res->fetch_all(MYSQLI_ASSOC);
    }
}

require_once __DIR__ . '/../includes/lecturer_layout_top.php';
?>

<div class="lux-card">
    <div class="lux-card-header">
        <h5 class="mb-1 fw-bold">Submissions</h5>
        <div class="text-muted small">Latest student submissions.</div>
    </div>

    <div class="lux-card-body">
        <?php if (!$rows): ?>
            <div class="text-center py-5">
                <i class="bi bi-inbox fs-1 text-muted"></i>
                <h6 class="mt-3 fw-bold">No submissions found</h6>
                <p class="text-muted mb-0">Student submissions will appear here.</p>
            </div>
        <?php else: ?>
            <div class="table-responsive">
                <table class="table align-middle">
                    <thead>
                    <tr>
                        <th>Student</th>
                        <th>Assessment</th>
                        <th>Batch</th>
                        <th>Status</th>
                        <th>Submitted</th>
                        <th class="text-end">Action</th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php foreach ($rows as $r): ?>
                        <tr>
                            <td class="fw-bold"><?= lecturer_h($r['student_name'] ?? '-') ?></td>
                            <td><?= lecturer_h($r['assessment_title'] ?? '-') ?></td>
                            <td><span class="badge-lux"><?= lecturer_h((string)($r['batch_id'] ?? '-')) ?></span></td>
                            <td><span class="badge bg-light text-dark border"><?= lecturer_h($r['workflow_status'] ?? '-') ?></span></td>
                            <td class="text-muted small"><?= lecturer_h((string)($r['submitted_at'] ?? '-')) ?></td>
                            <td class="text-end">
                                <a href="/elms/lecturer/grade.php?id=<?= (int)$r['id'] ?>" class="btn btn-sm btn-lux">
                                    Open
                                </a>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php endif; ?>
    </div>
</div>

<?php require_once __DIR__ . '/../includes/lecturer_layout_bottom.php'; ?>