/**
 * Get a single application by ID.
 *
 * GET /api/applications/:id
 *
 * Returns the application entity, initial chat state, and deployment snapshot
 * in a single call — everything the application page needs to render.
 */
import { NextResponse } from 'next/server';
import { resolve } from '../../../../lib/server-container.js';
import { featureIdForApplication } from '../../../../../../../packages/core/src/domain/shared/feature-id.js';
export const dynamic = 'force-dynamic';
export async function GET(_request, { params }) {
    try {
        const { id } = await params;
        const getApp = resolve('GetApplicationUseCase');
        const application = await getApp.execute(id);
        if (!application) {
            return NextResponse.json({ error: 'Application not found' }, { status: 404 });
        }
        let initialChatState;
        try {
            const getChatState = resolve('GetInteractiveChatStateUseCase');
            initialChatState = await getChatState.execute({
                featureId: featureIdForApplication(application.id),
            });
        }
        catch {
            initialChatState = undefined;
        }
        let deployment;
        try {
            const deploymentService = resolve('IDeploymentService');
            const status = deploymentService.getStatus(application.id);
            if (status && status.state !== 'Stopped') {
                deployment = { state: status.state, url: status.url };
            }
        }
        catch {
            deployment = undefined;
        }
        return NextResponse.json({ application, initialChatState, deployment });
    }
    catch (error) {
        // eslint-disable-next-line no-console
        console.error('[GET /api/applications/:id]', error);
        return NextResponse.json({ error: error instanceof Error ? error.message : 'Internal server error' }, { status: 500 });
    }
}
