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

try {
    // استعلام شامل يدمج الجداول الأربعة بناءً على هيكلية مشروعك
    $sql = "SELECT 
                u.full_name, u.phone, u.address,
                d.id as doctor_id, d.profile_image, d.latitude, d.longitude, 
                d.experience_years, d.consultation_fee, d.working_hours_ar,
                s.name_ar as specialty_name_ar, 
                ss.name_ar as sub_specialty_name_ar
            FROM users u
            INNER JOIN doctors d ON u.id = d.user_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.latitude IS NOT NULL AND d.longitude IS NOT NULL";

    $stmt = $conn->prepare($sql);
    $stmt->execute();
    $doctors = $stmt->fetchAll(PDO::FETCH_ASSOC);

    echo json_encode([
        "status" => "success",
        "data" => $doctors
    ], JSON_UNESCAPED_UNICODE);

} catch (PDOException $e) {
    echo json_encode([
        "status" => "error", 
        "message" => "خطأ في قاعدة البيانات: " . $e->getMessage()
    ]);
}
?>