<?php
require_once __DIR__ . '/../../config/config.php';
header('Content-Type: application/json');
ob_start();
try {
    $course_id = isset($_GET['course_id']) ? (int)$_GET['course_id'] : 0;
    if(!$course_id){ 
        echo json_encode(['status'=>'error','message'=>'Missing course_id']); 
        exit; 
    }
    
    global $pdo;
    
    // Try to get course-based settings first
    $stmt = $pdo->prepare("SELECT max_marks, pass_marks, min_marks FROM course_assessment_settings WHERE course_id=? AND exam_type='Internal' ORDER BY setting_id DESC LIMIT 1");
    $stmt->execute([$course_id]);
    $row = $stmt->fetch(PDO::FETCH_ASSOC);
    
    // If no course settings found, use defaults
    if(!$row){ 
        $row = ['max_marks'=>20,'pass_marks'=>10,'min_marks'=>0]; 
    }
    
    echo json_encode(['status'=>'success','data'=>$row]);
} catch (Throwable $e) {
    error_log('read_course_settings error: '.$e->getMessage());
    http_response_code(500);
    echo json_encode(['status'=>'error','message'=>$e->getMessage()]);
}
ob_end_flush();
?>
