<?php
header('Content-Type: application/json');
require_once('../libs/classes/DbPathsArray.php');
require_once('../libs/classes/App_DB.php');

$db = new App_DB(DBU::$dba_user);

$response = [
    'success' => false,
    'message' => '',
    'subjects' => []
];

if ($_SERVER['REQUEST_METHOD'] === 'GET') {
    $darjaSno = $_GET['darja_sno'] ?? '';

    if (empty($darjaSno)) {
        $response['message'] = 'درجہ کا انتخاب ضروری ہے';
        echo json_encode($response);
        exit;
    }

    try {
        $sql = "SELECT s.sno, s.subjectName, s.totalMarks 
                FROM subjects s 
                JOIN darjaat d ON d.derjaCode = s.darjaSno 
                WHERE s.darjaSno = :darjaSno 
                ORDER BY s.sno ASC";

        $subjects = $db->getRecordSet($sql, ['darjaSno' => $darjaSno]);

        $response['success'] = true;
        $response['subjects'] = $subjects;
        if (empty($subjects)) {
            $response['message'] = 'اس درجہ کے لئے کوئی مضمون نہیں ملا';
        }
    } catch (Exception $e) {
        $response['message'] = 'ڈیٹابیس کی خرابی: ' . $e->getMessage();
    }
}

echo json_encode($response);
