<?php

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

use App\Models\AdminUser;
use App\Lottery\ErrorCode;
use App\Support\ApiMessage;
use App\Support\ApiResponse;
use Illuminate\Http\Request;
use App\Models\PlayConfigVersion;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use App\Lottery\ConfigVersionStatus;
use App\Services\Config\PlayConfigStreamService;

/** DELETE /api/v1/admin/config/play-versions/{id} */
final class PlayConfigVersionDestroyController extends Controller
{
    public function __invoke(Request $request, int $id, PlayConfigStreamService $service): JsonResponse
    {
        /** @var AdminUser $admin */
        $admin = $request->lotteryAdmin();

        /** @var PlayConfigVersion $version */
        $version = PlayConfigVersion::query()->whereKey($id)->firstOrFail();

        if ($version->status === ConfigVersionStatus::Active->value) {
            return ApiResponse::error(
                ApiMessage::get($request, 'config_version_cannot_delete_active'),
                ErrorCode::ConfigVersionCannotDeleteActive->value,
                null,
                400,
            );
        }

        $service->deleteVersion($version, $admin, $request);

        return ApiResponse::success(['deleted' => true]);
    }
}
