<?php
session_start();
require_once('../config.php');

if (!isset($_SESSION['user_id']) || $_SESSION['role'] != 'lecturer') {
    header("Location: dashboard.php");
    exit();
}

include('../header/header.php');

$user_id = $_SESSION['user_id'];
$error = '';
$success = '';
$preselected_module_id = isset($_GET['module_id']) ? $_GET['module_id'] : '';
$new_unit_id = null;
$current_course_id = null;

$stmt = $pdo->prepare("
SELECT m.*, c.course_name, c.id as course_id
FROM modules m
JOIN courses c ON m.course_id = c.id
WHERE c.lecturer_id = ?
");
$stmt->execute([$_SESSION['user_id']]);
$modules = $stmt->fetchAll();

if ($_POST) {

    $module_id = $_POST['module_id'];
    $unit_name = trim($_POST['unit_name']);

    $stmt = $pdo->prepare("SELECT id FROM modules WHERE id=?");
    $stmt->execute([$module_id]);

    if ($stmt->rowCount() == 0) {
        $error = "Invalid module selected!";
    } else {

        $stmt = $pdo->prepare("INSERT INTO units (module_id, unit_name) VALUES (?, ?)");
        $stmt->execute([$module_id, $unit_name]);
        $new_unit_id = $pdo->lastInsertId();
        
        $stmt = $pdo->prepare("SELECT course_id FROM modules WHERE id=?");
        $stmt->execute([$module_id]);
        $course = $stmt->fetch();
        $current_course_id = $course['course_id'];
        
        $success = "Unit added successfully!";
    }
}
?>

<div class="container py-5">
    <div class="row justify-content-center">
        <div class="col-md-6">
            <div class="card shadow-sm border-0">
                <div class="card-body p-5">
                    <h2 class="fw-bold mb-4 text-center">Add New Unit</h2>
                    
                    <?php if($error): ?>
                        <div class="alert alert-danger"><?php echo $error; ?></div>
                    <?php endif; ?>

                    <?php if($success): ?>
                        <div class="alert alert-success"><?php echo $success; ?></div>
                        
                        <div class="d-grid gap-2 mb-3">
                            <a href="add_unit.php?module_id=<?= $preselected_module_id; ?>" class="btn btn-secondary py-2 fw-bold">Add Another Unit</a>
                            <a href="upload_notes.php?course_id=<?= $current_course_id; ?>&module_id=<?= $preselected_module_id; ?>&unit_id=<?= $new_unit_id; ?>" class="btn btn-success py-2 fw-bold">Upload Notes for this Unit</a>
                            <a href="dashboard.php" class="btn btn-outline-primary py-2 fw-bold">Back to Dashboard</a>
                        </div>
                    <?php endif; ?>

                    <?php if(!$success): ?>
                    <form method="POST">
                        <div class="mb-3">
                            <label class="form-label">Select Module</label>
                            <select name="module_id" class="form-select" required>
                                <option value="">Select Module</option>
                                <?php foreach($modules as $m): ?>
                                <option value="<?= $m['id']; ?>" <?= ($m['id'] == $preselected_module_id) ? 'selected' : ''; ?>>
                                    <?= htmlspecialchars($m['course_name']); ?> - <?= htmlspecialchars($m['module_name']); ?>
                                </option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">Unit Name</label>
                            <input type="text" name="unit_name" class="form-control" placeholder="Enter unit name" required>
                        </div>
                        <button type="submit" class="btn btn-primary w-100 py-2 fw-bold">Add Unit</button>
                    </form>
                    <div class="text-center mt-3">
                        <a href="dashboard.php" class="text-decoration-none">Back to Dashboard</a>
                    </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</div>

<?php include('../footer/footer.php'); ?>