<?php
// api/progress.php

header('Content-Type: application/json');
require_once __DIR__ . '/../config/config.php';
require_once __DIR__ . '/../src/Helpers/Auth.php';
require_once __DIR__ . '/../src/Controllers/PortalController.php';

if (!Auth::check()) {
    echo json_encode(['success' => false, 'message' => 'Unauthorized access.']);
    exit;
}

$user = Auth::user();
$courseId = $_POST['course_id'] ?? '';
$moduleIndex = isset($_POST['module_index']) ? (int)$_POST['module_index'] : -1;

if (empty($courseId) || $moduleIndex < 0) {
    echo json_encode(['success' => false, 'message' => 'Invalid parameters.']);
    exit;
}

$result = PortalController::toggleModuleCompletion($user['id'], $courseId, $moduleIndex);
echo json_encode($result);
