<?php

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

use App\Models\ReportJob;
use App\Support\ApiResponse;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use App\Support\AdminReportJobPolicy;

/** GET /api/v1/admin/report-jobs/{report_job} */
final class ReportJobShowController extends Controller
{
    public function __invoke(Request $request, ReportJob $report_job): JsonResponse
    {
        $admin = $request->lotteryAdmin();
        abort_if($admin === null, 401);
        abort_unless(AdminReportJobPolicy::jobAccessible($admin, $report_job), 403);

        return ApiResponse::success([
            'id' => (int) $report_job->id,
            'job_no' => $report_job->job_no,
            'admin_user_id' => $report_job->admin_user_id !== null ? (int) $report_job->admin_user_id : null,
            'report_type' => $report_job->report_type,
            'export_format' => $report_job->export_format,
            'status' => $report_job->status,
            'finished_at' => $report_job->finished_at?->toIso8601String(),
            'created_at' => $report_job->created_at?->toIso8601String(),
        ]);
    }
}
