import { NextResponse } from 'next/server';
import { readVideoFile } from '@/lib/storage';

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

export async function GET(_req: Request, ctx: { params: Promise<{ filename: string }> }) {
  const { filename } = await ctx.params;
  const video = await readVideoFile(filename);
  if (!video) return NextResponse.json({ error: 'video not found' }, { status: 404 });

  return new Response(new Uint8Array(video.buf), {
    headers: {
      'Content-Type': video.type,
      'Content-Length': String(video.buf.length),
      'Cache-Control': 'private, max-age=3600',
      'Accept-Ranges': 'bytes',
    },
  });
}
