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

/** POST /api/v1/admin/config/play-versions — 新建草稿（默认克隆当前 active，若无则按 play_types 生成）。 */
final class PlayConfigVersionStoreController extends Controller
{
    public function __invoke(Request $request, PlayConfigStreamService $service): JsonResponse
    {
        /** @var AdminUser $admin */
        $admin = $request->lotteryAdmin();

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

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

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