<?php

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

use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\ReportJobStoreRequest;
use App\Models\AdminUser;
use App\Services\Admin\AdminReportJobService;
use App\Support\ApiResponse;
use Illuminate\Http\JsonResponse;

/** POST /api/v1/admin/report-jobs */
final class ReportJobStoreController extends Controller
{
    public function __invoke(ReportJobStoreRequest $request, AdminReportJobService $service): JsonResponse
    {
        /** @var AdminUser $admin */
        $admin = $request->lotteryAdmin();

        $data = $request->validated();

        $job = $service->enqueue(
            $admin,
            $request,
            (string) $data['report_type'],
            (string) ($data['export_format'] ?? 'csv'),
            isset($data['filter_json']) ? (array) $data['filter_json'] : null,
        );

        return ApiResponse::success([
            'id' => (int) $job->id,
            'job_no' => $job->job_no,
            'report_type' => $job->report_type,
            'export_format' => $job->export_format,
            'status' => $job->status,
        ]);
    }
}