<?php

namespace App\Http\Controllers\Admin;

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

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

        $paymentCounts = Payment::query()
            ->selectRaw('status, COUNT(*) as aggregate')
            ->groupBy('status')
            ->pluck('aggregate', 'status');

        return view('admin.reports.index', [
            'summary' => [
                'orders' => Order::query()->count(),
                'customers' => User::query()->where('status', 'active')->count(),
                'paid_revenue' => Payment::query()->where('status', 'paid')->sum('amount'),
                'avg_order_value' => (float) Order::query()->avg('total_amount'),
            ],
            'statusCounts' => $statusCounts,
            'paymentCounts' => $paymentCounts,
            'recentNotifications' => NotificationLog::query()->latest()->take(10)->get(),
        ]);
    }
}
