<?php

namespace App\Http\Controllers\Api;

use App\Domain\Financial\Models\Invoice;
use App\Domain\Financial\Models\Payment;
use App\Domain\Participant\Models\Participant;
use App\Domain\Training\Models\Enrollment;
use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class QuickStatsController extends Controller
{
    public function __invoke(Request $request): JsonResponse
    {
        $today = now()->toDateString();

        return response()->json([
            'participants' => [
                'total' => Participant::count(),
                'active' => Participant::where('status', 'active')->count(),
                'new_this_month' => Participant::where('created_at', '>=', now()->startOfMonth())->count(),
            ],
            'enrollments' => [
                'active' => Enrollment::where('status', 'active')->count(),
                'pending' => Enrollment::where('status', 'pending')->count(),
                'new_today' => Enrollment::whereDate('created_at', $today)->count(),
            ],
            'financial' => [
                'revenue_today' => Payment::where('status', 'confirmed')->whereDate('created_at', $today)->sum('amount'),
                'revenue_month' => Payment::where('status', 'confirmed')->where('created_at', '>=', now()->startOfMonth())->sum('amount'),
                'outstanding' => Invoice::whereIn('status', ['sent', 'partially_paid', 'overdue'])->sum('due_amount'),
                'overdue_count' => Invoice::where('status', 'overdue')->count(),
            ],
        ]);
    }
}
