<?php

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

use App\Lottery\ErrorCode;
use App\Support\ApiMessage;
use App\Support\ApiResponse;
use Illuminate\Http\Request;
use App\Models\SettlementBatch;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use App\Services\Settlement\SettlementBatchWorkflowService;

final class AdminSettlementBatchPayoutController extends Controller
{
    public function __construct(private readonly SettlementBatchWorkflowService $service) {}

    public function __invoke(Request $request, SettlementBatch $batch): JsonResponse
    {
        try {
            $updated = $this->service->payout($batch);
        } catch (\RuntimeException $e) {
            return ApiMessage::runtimeErrorResponse($request, $e);
        }

        return ApiResponse::success([
            'id' => (int) $updated->id,
            'status' => $updated->status,
            'paid_at' => $updated->paid_at?->toIso8601String(),
        ]);
    }
}
