<?php

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

use App\Models\AdminUser;
use App\Support\ApiResponse;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use App\Support\AdminConfigPresenter;
use App\Services\Config\OddsStreamService;

/** POST /api/v1/admin/config/odds-versions */
final class OddsVersionStoreController extends Controller
{
    public function __invoke(Request $request, OddsStreamService $service): JsonResponse
    {
        /** @var AdminUser $admin */
        $admin = $request->lotteryAdmin();

        $data = $request->validate([
            'reason' => ['nullable', 'string', 'max:255'],
            'clone_from_version_id' => ['nullable', 'integer', 'exists:odds_versions,id'],
        ]);

        $draft = $service->createDraft(
            $admin,
            $data['reason'] ?? null,
            $data['clone_from_version_id'] ?? null,
        );

        return ApiResponse::success(
            AdminConfigPresenter::oddsVersionDetail($draft->load('items')),
        );
    }
}
