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

$db_path = dirname(__DIR__) . '/dramalive.db';

if (!file_exists($db_path)) {
    http_response_code(500);
    echo json_encode(["success" => false, "message" => "قاعدة البيانات غير موجودة"], JSON_UNESCAPED_UNICODE);
    exit;
}

try {
    $db = new SQLite3($db_path);
    
    $series_count = $db->querySingle('SELECT COUNT(*) FROM series');
    $movies_count = $db->querySingle('SELECT COUNT(*) FROM movies');
    $episodes_count = $db->querySingle('SELECT COUNT(*) FROM episodes');
    
    $db->close();

    $response = [
        "success" => true,
        "status" => "يعمل بشكل طبيعي",
        "statistics" => [
            "total_series" => $series_count,
            "total_movies" => $movies_count,
            "total_episodes" => $episodes_count
        ]
    ];

    echo json_encode($response, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);

} catch (Exception $e) {
    http_response_code(500);
    echo json_encode(["success" => false, "message" => "خطأ في قاعدة البيانات"], JSON_UNESCAPED_UNICODE);
}
?>