<?php
require_once __DIR__ . '/../connect.php';

$sql = "
SELECT
    -- العدادات العامة
    (SELECT COUNT(*) FROM passengers) AS countPassengers,
    (SELECT COUNT(*) FROM driver) AS countDriver,
    (SELECT COUNT(*) FROM ride) AS countRide,

    -- إحصائيات الشهر الحالي
    (SELECT COUNT(*) FROM passengers WHERE created_at BETWEEN DATE_FORMAT(CURDATE(), '%Y-%m-01') AND LAST_DAY(CURDATE())) AS countPassengersThisMonth,
    (SELECT COUNT(*) FROM driver WHERE created_at BETWEEN DATE_FORMAT(CURDATE(), '%Y-%m-01') AND LAST_DAY(CURDATE())) AS countDriverThisMonth,
    (SELECT COUNT(*) FROM ride WHERE created_at BETWEEN DATE_FORMAT(CURDATE(), '%Y-%m-01') AND LAST_DAY(CURDATE())) AS countRideThisMonth,
    (SELECT COUNT(*) FROM CarRegistration WHERE created_at BETWEEN DATE_FORMAT(CURDATE(), '%Y-%m-01') AND LAST_DAY(CURDATE())) AS countCarRegistrationThisMonth,

    -- شكاوى
    (SELECT COUNT(*) FROM complaint WHERE date_filed BETWEEN DATE_FORMAT(CURDATE(), '%Y-%m-01') AND LAST_DAY(CURDATE())) AS countComplaintThisMonth,
    (SELECT COUNT(*) FROM complaint WHERE date_filed BETWEEN DATE_SUB(CURDATE(), INTERVAL WEEKDAY(CURDATE()) DAY) AND DATE_ADD(DATE_SUB(CURDATE(), INTERVAL WEEKDAY(CURDATE()) DAY), INTERVAL 6 DAY)) AS countComplaintThisWeek,
    (SELECT COUNT(*) FROM complaint WHERE DATE(date_filed) = CURDATE()) AS countComplaintToday,

    -- المحافظ والتحويلات
   
    -- إحصائيات وقت ومسافة الرحلات
    (SELECT TIME_FORMAT(SEC_TO_TIME(AVG(TIMESTAMPDIFF(SECOND, rideTimeStart, rideTimeFinish))), '%Hh %im') FROM ride WHERE rideTimeStart IS NOT NULL AND rideTimeFinish IS NOT NULL) AS driver_avg_duration,
    (SELECT MAX(SEC_TO_TIME(TIMESTAMPDIFF(SECOND, rideTimeStart, rideTimeFinish))) FROM ride WHERE rideTimeStart IS NOT NULL AND rideTimeFinish IS NOT NULL) AS longest_duration,
    (SELECT ROUND(SUM(distance),2) FROM ride) AS total_distance,
    (SELECT ROUND(AVG(distance),2) FROM ride) AS average_distance,
    (SELECT ROUND(MAX(distance),2) FROM ride) AS longest_distance,

    -- أرباح السائق والشركة
    (SELECT SUM(price_for_driver) FROM ride WHERE status = 'Finished') AS total_driver_earnings,
    (SELECT ROUND(AVG(price_for_passenger),2) FROM ride) AS avg_passenger_price,

    -- توزيع الرحلات حسب الوقت
    (SELECT COUNT(*) FROM ride WHERE HOUR(created_at) BETWEEN 6 AND 11) AS morning_ride_count,
    (SELECT COUNT(*) FROM ride WHERE HOUR(created_at) BETWEEN 12 AND 17) AS evening_ride_count,
    (SELECT COUNT(*) FROM ride WHERE HOUR(created_at) BETWEEN 18 AND 23 OR HOUR(created_at) BETWEEN 0 AND 5) AS night_ride_count,

    -- أنواع الرحلات
    (SELECT COUNT(*) FROM ride WHERE carType = 'Comfort') AS comfort,
    (SELECT COUNT(*) FROM ride WHERE carType = 'Speed') AS speed,
    (SELECT COUNT(*) FROM ride WHERE carType = 'Lady') AS lady,

    -- حالة الرحلات
    (SELECT COUNT(*) FROM ride WHERE status = 'wait') AS ongoing_rides,
    (SELECT COUNT(*) FROM ride WHERE status = 'Finished') AS completed_rides,
    (SELECT COUNT(*) FROM ride WHERE status = 'cancel') AS cancelled_rides,

    -- عدد السائقين الفريدين
    (SELECT COUNT(*) FROM (SELECT driver_id FROM ride GROUP BY driver_id) AS sub) AS num_Driver,

    -- التحويلات البنكية
    (SELECT COUNT(*) FROM payments WHERE payment_method = 'TransferFrom') AS transfer_from_count

FROM passengers
LIMIT 1;
";

$stmt = $con->prepare($sql);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);

if ($result) {
    jsonSuccess($result);
} else {
    jsonError("No dashboard data found");
}
?>