<?php

namespace App\Http\Controllers\Api\V1\Admin\Reports;

use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\AdminReportQueryRequest;
use App\Services\Admin\AdminReportQueryService;
use App\Support\AdminApiList;
use App\Support\AdminScopePolicy;
use Illuminate\Http\JsonResponse;

/** GET /api/v1/admin/reports/daily-profit */
final class AdminReportDailyProfitController extends Controller
{
    public function __invoke(AdminReportQueryRequest $request, AdminReportQueryService $service): JsonResponse
    {
        $admin = $request->lotteryAdmin();
        abort_if($admin === null, 401);

        $validated = $request->validated();
        $p = AdminApiList::readPaging($request);
        $range = $service->resolveDateRange($validated);
        $scope = AdminScopePolicy::resolveContext($request, $admin, 'site_code', 'agent_node_id');

        $paginator = $service->dailyProfitPaginated(
            $range['date_from'],
            $range['date_to'],
            $p['page'],
            $p['perPage'],
            $scope,
        );

        return AdminApiList::jsonWith($paginator, static fn (array $row): array => $row, [
            'currency_code' => $service->resolvePeriodCurrencyCode($range['date_from'], $range['date_to'], $scope),
        ]);
    }
}
