<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\ShopUser;
use App\Models\UnlockOrder;
use App\Models\ShopPayment;
use Inertia\Inertia;

class UnlockServiceDashboardController extends Controller
{
    public function index()
    {
        $todayOrders = UnlockOrder::whereDate('date', today())->count();
        $totalIncome = UnlockOrder::where('status', 'done')->sum('service_price');
        $pendingOrders = UnlockOrder::where('status', 'pending')->count();
        $completedOrders = UnlockOrder::where('status', 'done')->count();
        $activeShops = ShopUser::count();
        $totalCollected = ShopPayment::where('status', 'completed')->sum('amount');

        $serviceBreakdown = UnlockOrder::selectRaw('service_type, COUNT(*) as count')
            ->groupBy('service_type')
            ->get();

        return Inertia::render('Admin/Pages/UnlockService/Dashboard', [
            'stats' => [
                'today_orders' => $todayOrders,
                'total_income' => $totalIncome,
                'pending_orders' => $pendingOrders,
                'completed_orders' => $completedOrders,
                'active_shops' => $activeShops,
                'total_collected' => $totalCollected,
            ],
            'service_breakdown' => $serviceBreakdown,
        ]);
    }
}
