import { NextRequest, NextResponse } from 'next/server';
import { z } from 'zod';
import { resolveDirectVideoUrl } from '@/lib/resolvers';

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

const BodySchema = z.object({ url: z.string().url() });

export async function POST(req: NextRequest) {
  try {
    const json = await req.json();
    const { url } = BodySchema.parse(json);
    const resolved = await resolveDirectVideoUrl(url);
    return NextResponse.json(resolved);
  } catch (err: any) {
    return NextResponse.json({ error: err?.message || 'Unexpected error' }, { status: 500 });
  }
}

export async function GET(req: NextRequest) {
  try {
    const { searchParams } = new URL(req.url);
    const url = searchParams.get('url');
    if (!url) return NextResponse.json({ error: 'Missing url' }, { status: 400 });
    const resolved = await resolveDirectVideoUrl(url);
    return NextResponse.json(resolved);
  } catch (err: any) {
    return NextResponse.json({ error: err?.message || 'Unexpected error' }, { status: 500 });
  }
}


