<?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\RiskCapStreamService;

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

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

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

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