import { NextResponse } from 'next/server';
import { recordEvent } from '@/lib/auditDb';
import { getSeedanceVideoTask } from '@/lib/videoProviders';

export const runtime = 'nodejs';
export const dynamic = 'force-dynamic';

export async function GET(_req: Request, ctx: { params: Promise<{ taskId: string }> }) {
  const { taskId } = await ctx.params;
  try {
    const response = await getSeedanceVideoTask(taskId);
    recordEvent({ action: 'video.status_checked', targetType: 'video', targetId: taskId, status: 'ok', provider: 'seedance', metadata: { status: response.status } });
    return NextResponse.json(response);
  } catch (error) {
    const message = String(error);
    recordEvent({ action: 'video.status_failed', targetType: 'video', targetId: taskId, status: 'error', provider: 'seedance', message });
    return NextResponse.json({ error: message }, {
      status: message.includes('SEEDANCE_API_KEY missing') ? 503 : 500,
    });
  }
}
