/**
 * POST /api/interactive/sessions
 *
 * Start a new interactive agent session for a feature.
 * Returns 201 with { sessionId, status } on success.
 * Returns 429 when the concurrent session cap is reached.
 */
import { NextResponse } from 'next/server';
import { resolve } from '../../../../lib/server-container.js';
import { ConcurrentSessionLimitError } from '../../../../../../../packages/core/src/domain/errors/concurrent-session-limit.error.js';
export const dynamic = 'force-dynamic';
export async function POST(request) {
    try {
        const body = (await request.json());
        const { featureId, worktreePath, agentType, model } = body;
        if (!featureId || typeof featureId !== 'string') {
            return NextResponse.json({ error: 'featureId is required' }, { status: 400 });
        }
        if (!worktreePath || typeof worktreePath !== 'string') {
            return NextResponse.json({ error: 'worktreePath is required' }, { status: 400 });
        }
        const useCase = resolve('StartInteractiveSessionUseCase');
        const session = await useCase.execute({ featureId, worktreePath, agentType, model });
        return NextResponse.json({ sessionId: session.id, status: session.status }, { status: 201 });
    }
    catch (error) {
        if (error instanceof ConcurrentSessionLimitError) {
            return NextResponse.json({
                error: error.message,
                code: 'CONCURRENT_SESSION_LIMIT',
            }, { status: 429 });
        }
        // eslint-disable-next-line no-console
        console.error('[POST /api/interactive/sessions]', error);
        return NextResponse.json({ error: error instanceof Error ? error.message : 'Internal server error' }, { status: 500 });
    }
}
