<?php
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');

require_once 'database.php';

// فحص قاعدة البيانات باستخدام MySQLi
$db = new Database();
$conn = $db->getConnection();

$db_status = false;
$db_message = "";

if ($conn) {
    $db_status = true;
    $db_message = "✅ متصل";
} else {
    $db_message = "❌ غير متصل";
}

// فحص وجود ملفات API
$endpoints = [
    'admin/endpoints/get_dashboard_stats.php',
    'admin/endpoints/get_requests.php',
    'supervisor/endpoints/get_review_inspections.php',
    'supervisor/endpoints/get_inspector_ratings.php',
    'inspector/endpoints/get_my_tasks.php',
    'client/endpoints/submit_request.php'
];

$endpoint_status = [];
foreach ($endpoints as $endpoint) {
    $endpoint_status[$endpoint] = file_exists(__DIR__ . '/' . $endpoint);
}

echo json_encode([
    'success' => true,
    'message' => '🌐 API يعمل بشكل صحيح',
    'timestamp' => date('Y-m-d H:i:s'),
    'server' => $_SERVER['SERVER_NAME'],
    'database' => [
        'status' => $db_status,
        'message' => $db_message,
        'name' => 'digital_autotester_db'
    ],
    'endpoints' => $endpoint_status
], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
?>