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

include_once 'db_connect.php'; 

if (!isset($_GET['doctor_id'])) {
    echo json_encode(["status" => "error", "message" => "Missing doctor ID"]);
    exit;
}

$doctor_id = $_GET['doctor_id'];

try {
    $sql = "SELECT 
                d.*, 
                u.full_name, 
                u.phone, 
                u.email,
                u.address,
                s.name_ar as specialty_name_ar, 
                s.name_en as specialty_name_en,
                ss.name_ar as sub_specialty_name_ar,
                ss.name_en as sub_specialty_name_en
            FROM doctors d 
            JOIN users u ON d.user_id = u.id
            LEFT JOIN medical_specialties s ON d.specialty_id = s.id
            LEFT JOIN medical_sub_specialties ss ON d.sub_specialty_id = ss.id
            WHERE d.id = :doctor_id";
            
    $stmt = $conn->prepare($sql);
    $stmt->bindParam(':doctor_id', $doctor_id);
    $stmt->execute();

    $doctor = $stmt->fetch(PDO::FETCH_ASSOC);

    if ($doctor) {
        echo json_encode([
            "status" => "success",
            "data" => $doctor
        ]);
    } else {
        echo json_encode(["status" => "error", "message" => "Doctor not found"]);
    }

} catch (PDOException $e) {
    echo json_encode(["status" => "error", "message" => $e->getMessage()]);
}
?>