/**
 * Feature-scoped chat messages API.
 *
 * POST - Send a user message. Backend handles session lifecycle.
 * GET  - Get chat state: messages + session status + streaming text.
 * DELETE - Clear chat message history for the feature.
 *
 * The frontend never manages sessions — it just sends messages for a feature.
 *
 * `featureId` is a polymorphic scope key: a feature UUID, "repo-<id>", or "global".
 */
import { NextResponse } from 'next/server';
import { resolve } from '../../../../../../lib/server-container.js';
import { getShepHomeDir } from '../../../../../../../../../packages/core/src/infrastructure/services/filesystem/shep-directory.service.js';
export const dynamic = 'force-dynamic';
const MAX_CONTENT_BYTES = 32 * 1024;
export async function POST(request, { params }) {
    try {
        const { featureId } = await params;
        const body = (await request.json());
        const { content, worktreePath, model, agentType } = body;
        if (!content || typeof content !== 'string' || content.trim().length === 0) {
            return NextResponse.json({ error: 'content must be a non-empty string' }, { status: 400 });
        }
        if (Buffer.byteLength(content, 'utf8') > MAX_CONTENT_BYTES) {
            return NextResponse.json({ error: 'content exceeds maximum size of 32 KB' }, { status: 400 });
        }
        // worktreePath is optional — defaults to SHEP_HOME for global/repo sessions
        let resolvedWorktreePath = worktreePath;
        if (!resolvedWorktreePath || typeof resolvedWorktreePath !== 'string') {
            resolvedWorktreePath = getShepHomeDir();
        }
        const useCase = resolve('SendInteractiveMessageUseCase');
        const message = await useCase.execute({
            featureId,
            content,
            worktreePath: resolvedWorktreePath,
            model: typeof model === 'string' ? model : undefined,
            agentType: typeof agentType === 'string' ? agentType : undefined,
        });
        return NextResponse.json({ message }, { status: 201 });
    }
    catch (error) {
        if (error instanceof Error && error.message.includes('concurrent session limit')) {
            return NextResponse.json({ error: error.message }, { status: 429 });
        }
        // eslint-disable-next-line no-console
        console.error('[POST /api/interactive/chat/:featureId/messages]', error);
        return NextResponse.json({ error: error instanceof Error ? error.message : 'Internal server error' }, { status: 500 });
    }
}
export async function DELETE(_request, { params }) {
    try {
        const { featureId } = await params;
        // clearMessages is not yet exposed as a use case — use service directly
        const service = resolve('IInteractiveSessionService');
        await service.clearMessages(featureId);
        return NextResponse.json({ ok: true });
    }
    catch (error) {
        // eslint-disable-next-line no-console
        console.error('[DELETE /api/interactive/chat/:featureId/messages]', error);
        return NextResponse.json({ error: error instanceof Error ? error.message : 'Internal server error' }, { status: 500 });
    }
}
export async function GET(_request, { params }) {
    try {
        const { featureId } = await params;
        const useCase = resolve('GetInteractiveChatStateUseCase');
        const chatState = await useCase.execute({ featureId });
        return NextResponse.json(chatState);
    }
    catch (error) {
        // eslint-disable-next-line no-console
        console.error('[GET /api/interactive/chat/:featureId/messages]', error);
        return NextResponse.json({ error: error instanceof Error ? error.message : 'Internal server error' }, { status: 500 });
    }
}
