<?php

namespace App\Modules\Rides\Repositories;

use Core\Database\DB;
use PDO;

class RideRepository
{
    public function all(): array
    {
        $db = DB::conn();

        $stmt = $db->query("
        SELECT
            rides.*, 
            users.name AS driver_name,
            users.avatar AS driver_avatar
        FROM rides
        JOIN users ON users.id = rides.driver_id
        WHERE users.role = 'driver'
        ORDER BY rides.id DESC
    ");

        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }
    public function popular(): array
    {
        $db = DB::conn();

        $stmt = $db->query("
        SELECT
            rides.*, 
            users.name AS driver_name,
            users.avatar AS driver_avatar
        FROM rides
        JOIN users ON users.id = rides.driver_id
        WHERE users.role = 'driver'
        ORDER BY rides.id DESC LIMIT 3
    ");

        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }
    public function findDriverById(int $id): array|false
    {
        $db = DB::conn();

        $stmt = $db->prepare("
        SELECT
            rides.*,
            users.name AS driver_name,
            users.email AS driver_email,
            users.avatar AS driver_avatar
        FROM rides
        JOIN users ON users.id = rides.driver_id
        WHERE users.id = ?
        AND users.role = 'driver'
        LIMIT 1
    ");

        $stmt->execute([$id]);

        return $stmt->fetch(PDO::FETCH_ASSOC);
    }
}