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

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

$PAGE_TITLE = 'My Modules';
$ACTIVE = 'modules';

$lecturerId = (int)($LECTURER_USER['id'] ?? 0);

function tbl_exists(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_exists2(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;
}

$modules = [];

if (isset($mysqli) && $mysqli instanceof mysqli) {
    $hasModules = tbl_exists($mysqli, 'lms_modules');

    if ($hasModules) {
        $moduleTitle = col_exists2($mysqli, 'lms_modules', 'title') ? 'title' : 'name';
        $moduleCode  = col_exists2($mysqli, 'lms_modules', 'module_code') ? 'module_code' : 'id';
        $hasLecturer = col_exists2($mysqli, 'lms_modules', 'lecturer_id');

        if ($hasLecturer) {
            $stmt = $mysqli->prepare("
                SELECT id, {$moduleTitle} AS title, {$moduleCode} AS module_code
                FROM lms_modules
                WHERE lecturer_id = ?
                ORDER BY id DESC
            ");
            $stmt->bind_param("i", $lecturerId);
        } else {
            $stmt = $mysqli->prepare("
                SELECT id, {$moduleTitle} AS title, {$moduleCode} AS module_code
                FROM lms_modules
                ORDER BY id DESC
                LIMIT 50
            ");
        }

        if ($stmt) {
            $stmt->execute();
            $modules = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
            $stmt->close();
        }
    }
}

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

<div class="lux-card">
    <div class="lux-card-header d-flex justify-content-between align-items-center">
        <div>
            <h5 class="mb-1 fw-bold">My Modules</h5>
            <div class="text-muted small">Modules assigned to your lecturer account.</div>
        </div>
        <span class="badge-lux"><?= count($modules) ?> Modules</span>
    </div>

    <div class="lux-card-body">
        <?php if (!$modules): ?>
            <div class="text-center py-5">
                <i class="bi bi-journal-x fs-1 text-muted"></i>
                <h6 class="mt-3 fw-bold">No modules found</h6>
                <p class="text-muted mb-0">Assigned modules will appear here.</p>
            </div>
        <?php else: ?>
            <div class="table-responsive">
                <table class="table align-middle">
                    <thead>
                    <tr>
                        <th>#</th>
                        <th>Module</th>
                        <th>Code</th>
                        <th class="text-end">Action</th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php foreach ($modules as $i => $m): ?>
                        <tr>
                            <td><?= $i + 1 ?></td>
                            <td class="fw-bold"><?= lecturer_h($m['title'] ?? '-') ?></td>
                            <td>
                                <span class="badge-lux"><?= lecturer_h((string)($m['module_code'] ?? '-')) ?></span>
                            </td>
                            <td class="text-end">
                                <a href="/elms/lecturer/content.php?module_id=<?= (int)$m['id'] ?>" class="btn btn-sm btn-lux">
                                    View Content
                                </a>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php endif; ?>
    </div>
</div>

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