<?php
include "connection.php";
header("Content-Type: application/json");

$response = [
    "status" => false,
    "message" => "",
    "data" => []
];

try {
    // Get category_id from query string
    $category_id = $_GET['category_id'] ?? null;

    if (!empty($category_id)) {
        // If category_id is provided, get courses by category
        $rs = Database::search("SELECT * FROM `course` WHERE `category_id` = '" . $category_id . "'");
    } else {
        // If not provided, get all courses
        $rs = Database::search("SELECT * FROM `course`");
    }

    $data = [];
    while ($row = $rs->fetch_assoc()) {
        $data[] = $row;
    }

    $response["status"] = true;
    $response["data"] = $data;

} catch (Exception $e) {
    $response["message"] = "Error: " . $e->getMessage();
}

echo json_encode($response);
exit;
?>
