<?php

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

use App\Models\ReconcileJob;
use App\Support\AdminReconcileScope;
use App\Support\ApiResponse;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

/** GET /api/v1/admin/reconcile-jobs/{reconcile_job} */
final class ReconcileJobShowController extends Controller
{
    public function __invoke(Request $request, ReconcileJob $reconcile_job): JsonResponse
    {
        $admin = $request->lotteryAdmin();
        abort_if($admin === null, 401);

        if ($denied = AdminReconcileScope::denyUnlessJobAccessible($admin, $reconcile_job)) {
            return $denied;
        }

        return ApiResponse::success([
            'id' => (int) $reconcile_job->id,
            'job_no' => $reconcile_job->job_no,
            'admin_user_id' => $reconcile_job->admin_user_id !== null ? (int) $reconcile_job->admin_user_id : null,
            'reconcile_type' => $reconcile_job->reconcile_type,
            'status' => $reconcile_job->status,
            'period_start' => $reconcile_job->period_start?->toIso8601String(),
            'period_end' => $reconcile_job->period_end?->toIso8601String(),
            'summary_json' => $reconcile_job->summary_json,
            'finished_at' => $reconcile_job->finished_at?->toIso8601String(),
            'created_at' => $reconcile_job->created_at?->toIso8601String(),
        ]);
    }
}
