<?php

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

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

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

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

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