/**
 * POST /api/applications/:id/cloud-deploy/select-provider
 *
 * Body: { provider: CloudDeploymentProvider }
 */
import { NextResponse } from 'next/server';
import { resolve } from '../../../../../../lib/server-container.js';
import { errorCode, errorMessage } from '../../../../../../lib/error-code.js';
import { CloudDeploymentProvider, } from '../../../../../../../../../packages/core/src/domain/generated/output.js';
export const dynamic = 'force-dynamic';
function parseProvider(raw) {
    if (typeof raw !== 'string')
        return null;
    const allowed = Object.values(CloudDeploymentProvider);
    return allowed.includes(raw) ? raw : null;
}
export async function POST(request, { params }) {
    try {
        const { id } = await params;
        const body = (await request.json());
        const provider = parseProvider(body.provider);
        if (!provider) {
            return NextResponse.json({ error: 'Invalid provider' }, { status: 400 });
        }
        const useCase = resolve('SelectCloudProviderUseCase');
        await useCase.execute({ applicationId: id, provider });
        return NextResponse.json({ ok: true });
    }
    catch (error) {
        const code = errorCode(error);
        const message = errorMessage(error);
        if (code === 'APPLICATION_NOT_FOUND') {
            return NextResponse.json({ error: message, code }, { status: 404 });
        }
        return NextResponse.json({ error: message }, { status: 500 });
    }
}
