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

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

$PAGE_TITLE = 'Content';
$ACTIVE = 'content';

$moduleId = (int)($_GET['module_id'] ?? 0);

function tbl_exists_content(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_content(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;
}

$lessons = [];

if (isset($mysqli) && $mysqli instanceof mysqli) {
    $lessonTable = tbl_exists_content($mysqli, 'lms_lessons') ? 'lms_lessons' : null;

    if ($lessonTable) {
        $titleCol   = col_exists_content($mysqli, $lessonTable, 'title') ? 'title' : 'id';
        $typeCol    = col_exists_content($mysqli, $lessonTable, 'lesson_type') ? 'lesson_type' : 'type';
        $contentCol = col_exists_content($mysqli, $lessonTable, 'content') ? 'content' : 'description';

        if ($moduleId > 0 && col_exists_content($mysqli, $lessonTable, 'module_id')) {
            $stmt = $mysqli->prepare("
                SELECT id, {$titleCol} AS title, {$typeCol} AS lesson_type, {$contentCol} AS content
                FROM {$lessonTable}
                WHERE module_id = ?
                ORDER BY id DESC
            ");
            $stmt->bind_param("i", $moduleId);
        } else {
            $stmt = $mysqli->prepare("
                SELECT id, {$titleCol} AS title, {$typeCol} AS lesson_type, {$contentCol} AS content
                FROM {$lessonTable}
                ORDER BY id DESC
                LIMIT 50
            ");
        }

        if ($stmt) {
            $stmt->execute();
            $lessons = $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">Learning Content</h5>
            <div class="text-muted small">View lesson content and module materials.</div>
        </div>
        <a href="/elms/lecturer/modules.php" class="btn btn-sm btn-lux">
            <i class="bi bi-arrow-left"></i> Modules
        </a>
    </div>

    <div class="lux-card-body">
        <?php if (!$lessons): ?>
            <div class="text-center py-5">
                <i class="bi bi-file-earmark-x fs-1 text-muted"></i>
                <h6 class="mt-3 fw-bold">No content found</h6>
                <p class="text-muted mb-0">Uploaded lesson content will appear here.</p>
            </div>
        <?php else: ?>
            <div class="row g-3">
                <?php foreach ($lessons as $lesson): ?>
                    <div class="col-md-6">
                        <div class="border rounded-4 p-3 bg-white h-100">
                            <div class="d-flex justify-content-between align-items-start gap-2 mb-2">
                                <h6 class="fw-bold mb-0"><?= lecturer_h($lesson['title'] ?? '-') ?></h6>
                                <span class="badge-lux"><?= lecturer_h($lesson['lesson_type'] ?? 'Lesson') ?></span>
                            </div>
                            <p class="text-muted small mb-0">
                                <?= lecturer_h(mb_strimwidth(strip_tags((string)($lesson['content'] ?? '')), 0, 160, '...')) ?>
                            </p>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
    </div>
</div>

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