<?php

namespace App\Modules\Admin\Repositories;

use Core\Database\DB;
use PDO;

class AdminRepository
{
    public function dashboardStats(): array
    {
        $db = DB::conn();

        return [
            'users' => (int) $db->query("SELECT COUNT(*) FROM users")->fetchColumn(),
            'drivers' => (int) $db->query("SELECT COUNT(*) FROM users WHERE role = 'driver'")->fetchColumn(),
            'rides' => (int) $db->query("SELECT COUNT(*) FROM rides")->fetchColumn(),
        ];
    }

    public function users(): array
    {
        return DB::conn()
            ->query("SELECT * FROM users ORDER BY id DESC")
            ->fetchAll(PDO::FETCH_ASSOC);
    }

    public function rides(): array
    {
        return DB::conn()
            ->query("
                SELECT rides.*, users.name AS driver_name
                FROM rides
                JOIN users ON users.id = rides.driver_id
                ORDER BY rides.id DESC
            ")
            ->fetchAll(PDO::FETCH_ASSOC);
    }
}