/**
 * GET /api/operations/:kind/:id/logs
 *
 * Returns the full OperationLogEntry history for one scoped long-running
 * operation. v1 supports:
 *
 *   • CloudDeploy         — kind=CloudDeploy, id=<applicationId>
 *   • GitRemoteCreate     — kind=GitRemoteCreate, id=<applicationId>
 *
 * The route delegates to ListOperationLogEntriesUseCase — it never touches
 * the repository directly, so the clean-architecture read path stays:
 *   route → use case → service port → repository.
 */
import { NextResponse } from 'next/server';
import { resolve } from '../../../../../../lib/server-container.js';
import { errorCode, errorMessage } from '../../../../../../lib/error-code.js';
import { OperationLogKind, } from '../../../../../../../../../packages/core/src/domain/generated/output.js';
export const dynamic = 'force-dynamic';
function parseKind(raw) {
    const allowed = Object.values(OperationLogKind);
    return allowed.includes(raw) ? raw : null;
}
function toDto(entry) {
    return {
        id: entry.id,
        operationKind: entry.operationKind,
        operationId: entry.operationId,
        level: entry.level,
        message: entry.message,
        detail: entry.detail,
        createdAt: entry.createdAt instanceof Date
            ? entry.createdAt.toISOString()
            : new Date(entry.createdAt).toISOString(),
    };
}
export async function GET(_request, { params }) {
    try {
        const { kind: rawKind, id } = await params;
        const kind = parseKind(rawKind);
        if (!kind) {
            return NextResponse.json({
                error: `Unknown operation kind: ${rawKind}. Expected one of: ${Object.values(OperationLogKind).join(', ')}`,
            }, { status: 400 });
        }
        if (!id || id.trim().length === 0) {
            return NextResponse.json({ error: 'Missing operation id' }, { status: 400 });
        }
        const useCase = resolve('ListOperationLogEntriesUseCase');
        const entries = await useCase.execute({ kind, id });
        return NextResponse.json({ entries: entries.map(toDto) });
    }
    catch (error) {
        const code = errorCode(error);
        const message = errorMessage(error);
        return NextResponse.json({ error: message, code }, { status: 500 });
    }
}
