<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Document;
use App\Models\Order;
use App\Models\Payment;
use App\Models\User;

class AdminDashboardController extends Controller
{
    public function index()
    {
        $statusCounts = Order::query()
            ->selectRaw('status, COUNT(*) as aggregate')
            ->groupBy('status')
            ->pluck('aggregate', 'status');

        return view('admin.dashboard', [
            'metrics' => [
                'orders' => Order::query()->count(),
                'users' => User::query()->count(),
                'documents' => Document::query()->count(),
                'revenue' => Payment::query()->where('status', 'paid')->sum('amount'),
                'pending_orders' => Order::query()->where('status', 'pending')->count(),
                'delivered_orders' => Order::query()->where('status', 'delivered')->count(),
            ],
            'recentOrders' => Order::query()->with(['user', 'payments'])->latest()->take(8)->get(),
            'recentUsers' => User::query()->with('role')->latest()->take(6)->get(),
            'statusCounts' => $statusCounts,
        ]);
    }
}
