<?php

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

use App\Models\Draw;
use App\Lottery\ErrorCode;
use App\Support\ApiResponse;
use App\Support\ApiMessage;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\DrawStoreRequest;
use App\Services\Draw\DrawManualUpdateService;

final class AdminDrawUpdateController extends Controller
{
    public function __construct(
        private readonly DrawManualUpdateService $service,
    ) {}

    public function __invoke(DrawStoreRequest $request, Draw $draw): JsonResponse
    {
        try {
            $updated = $this->service->update($draw, $request->validated());
        } catch (\RuntimeException $e) {
            return ApiMessage::runtimeErrorResponse($request, $e);
        }

        return ApiResponse::success([
            'id' => (int) $updated->id,
            'draw_no' => $updated->draw_no,
            'business_date' => (string) $updated->business_date,
            'sequence_no' => (int) $updated->sequence_no,
            'status' => $updated->status,
            'start_time' => $updated->start_time?->toIso8601String(),
            'close_time' => $updated->close_time?->toIso8601String(),
            'draw_time' => $updated->draw_time?->toIso8601String(),
        ]);
    }
}
