<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
include_once 'db_connect.php';

try {
    $conn = new PDO("mysql:host=$host;dbname=$db_name", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    if(isset($_GET['doctor_id']) && isset($_GET['date'])) {
        $doctor_id = $_GET['doctor_id'];
        $date = $_GET['date'];

        // استعلام لحساب عدد الحجوزات لهذا الطبيب في هذا التاريخ المحدد
        $query = "SELECT COUNT(*) as total FROM bookings WHERE doctor_id = :doctor_id AND appointment_date = :date";
        $stmt = $conn->prepare($query);
        $stmt->bindParam(':doctor_id', $doctor_id);
        $stmt->bindParam(':date', $date);
        $stmt->execute();

        $row = $stmt->fetch(PDO::FETCH_ASSOC);
        $count = $row['total'];

        echo json_encode([
            "status" => "success",
            "count" => (int)$count
        ]);
    } else {
        echo json_encode([
            "status" => "error",
            "message" => "Missing parameters"
        ]);
    }
} catch(PDOException $e) {
    echo json_encode([
        "status" => "error",
        "message" => "Connection failed: " . $e->getMessage()
    ]);
}
?>