-- to check duplicate CarRegistration
SELECT 
    `driverID`,
    COUNT(*) AS `count`,created_at
FROM 
    `CarRegistration`
GROUP BY 
    `driverID`
HAVING 
    COUNT(*) > 1;
--  


--  to delete duplicate
WITH CTE AS (
    SELECT 
        MIN(`id`) AS `min_id`
    FROM 
        `CarRegistration`
    GROUP BY 
        `driverID`
)
DELETE FROM 
    `CarRegistration`
WHERE 
    `id` NOT IN (SELECT `min_id` FROM CTE);

--  get for employee
SELECT
    d.`maritalStatus` AS NAME,
    COUNT(*) AS `count`
FROM
    `driver` d
WHERE
    d.`maritalStatus` IN('Maryam', 'rawda', 'Mena') AND DATE(d.created_at) = CURDATE()
GROUP BY
    d.`maritalStatus`
ORDER BY
    COUNT
DESC
    
    
    -- get driver without cars 

    SELECT
    d.id, d.phone
FROM
    `driver` d
WHERE
    d.id NOT IN (SELECT driverID FROM CarRegistration);


    -- car without drivers

    SELECT
   cr.created_at, cr.driverID
FROM
    `CarRegistration` cr
WHERE
    cr.driverID NOT IN (SELECT id FROM driver);



----- driver 
    SELECT phone,email,name_arabic,national_number FROM `driver`  WHERE national_number ='29209290106392'
ORDER BY `driver`.`created_at` DESC



------- driver work 

SELECT
    COUNT(`car_locations`.driver_id),
    driver.id,
    driver.phone,
    driver.name_arabic
FROM
    `car_locations`
LEFT JOIN driver ON driver.id = car_locations.driver_id
WHERE
   `car_locations`. created_at > TIMESTAMP(
        DATE_SUB(NOW(), INTERVAL 10 MINUTE))
    GROUP BY
        driver_id
    ORDER BY
        COUNT(driver_id)
    DESC
        ;


------  get count of year cars
SELECT 
  CASE 
    WHEN `year` > 2017 THEN 'After 2017'
    WHEN `year` BETWEEN 2000 AND 2016 THEN '2000-2016'
    ELSE 'Before 2000'
  END AS `year_group`,
  COUNT(*) AS `count`
FROM `carPlateEdit`
GROUP BY `year_group`
ORDER BY `year_group` ASC;



--  delete location 1 day

DELETE
FROM
    `car_locations`
WHERE
    DATE(`created_at`) < CURDATE();
SELECT
    COUNT(`car_locations`.driver_id),
    driver.id,
    driver.phone,
    driver.name_arabic
FROM
    `car_locations`
LEFT JOIN driver ON driver.id = car_locations.driver_id
WHERE
    `car_locations`.created_at > TIMESTAMP(
        DATE_SUB(NOW(), INTERVAL 10 MINUTE))
    GROUP BY
        driver_id
    ORDER BY
        COUNT(driver_id)
    DESC
        ;


-- driver register by hours for employee
SELECT
    d.`maritalStatus` AS NAME,
    HOUR(d.created_at) AS hour,
    COUNT(*) AS `count`
FROM
    `driver` d
WHERE
    d.`maritalStatus` IN ('Maryam', 'rawda', 'Mena')
    AND DATE(d.created_at) = CURDATE() 
GROUP BY
    d.`maritalStatus`, HOUR(d.created_at)
ORDER BY
    hour, `count` DESC;





---- monthly 
SELECT
    MONTH(d.created_at) AS month,
    COUNT(d.id) AS `count`
FROM
    `driver` d
WHERE
    YEAR(d.created_at) = YEAR(CURDATE())
GROUP BY
    MONTH(d.created_at)
ORDER BY
    month,
    `count`
DESC
    ;
        -----AI request


        SELECT
    f.id,
    f.passengerId,
    f.feedBack,
    f.datecreated,
    r.id AS ride_id,
    r.start_location,
    r.end_location,
    r.date,
    r.price,
    r.status,
    r.paymentMethod,
    r.distance,
    r.carType,
    r.rideTimeFinish,
    r.rideTimeStart,
    r.DriverIsGoingToPassenger,
    COUNT(rp.id) AS countRateFromPassengerToDrivers,
    COUNT(rd.id) AS countRateFromDriverToPassengers,
    MAX(rp.rating) AS rateFromPassengerToDriver,
    MAX(rd.rating) AS rateFromDriversToPassengers,
    MAX(rp.comment) AS commentFromPassengerToDriver,
    MAX(rd.comment) AS commentFromDriverToPassenger
FROM
    `feedBack` f
LEFT JOIN ride r ON
    r.passenger_id = f.passengerId
LEFT JOIN ratingPassenger rp ON
    rp.passenger_id = r.passenger_id
LEFT JOIN ratingDriver rd ON
    rd.driver_id = r.driver_id
WHERE
    r.passenger_id = '113172279072358305645'
GROUP BY
    f.id, f.passengerId, f.feedBack, f.datecreated,
    r.id, r.start_location, r.end_location, r.date, r.price,
    r.status, r.paymentMethod, r.distance, r.carType, 
    r.rideTimeFinish, r.rideTimeStart, r.DriverIsGoingToPassenger
ORDER BY
    r.date DESC
LIMIT 1;