<?php

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

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

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

        /** @var OddsVersion $version */
        $version = OddsVersion::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]);
    }
}
