// Native fetch available in Node 20+
import config from '../config';

export interface VideoInfo {
  title: string;
  coverUrl: string;
  videoUrl: string;
  author: string;
  platform: string;
  duration?: string;
}

export async function parseVideo(url: string): Promise<VideoInfo> {
  const apiUrl = `${config.videoParserUrl}/parse`;

  const response = await fetch(apiUrl, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ url }),
    signal: AbortSignal.timeout(30000),
  });

  if (!response.ok) {
    throw new Error(`Video parser API error: ${response.status} ${response.statusText}`);
  }

  const data = await response.json() as any;

  return {
    title: data.title || '',
    coverUrl: data.coverUrl || data.cover || '',
    videoUrl: data.videoUrl || data.url || data.video || '',
    author: data.author || data.nickname || '',
    platform: data.platform || '',
    duration: data.duration || '',
  };
}
