import { AppRequest, AppResponse } from '@/server/web';
import { API_CONFIG } from '@/lib/config';

export async function GET(request: AppRequest) {
  try {
    const { searchParams } = new URL(request.url);
    const id = searchParams.get('id');
    const episode = searchParams.get('episode');

    if (!id) {
      return AppResponse.json(
        { error: 'id parameter is required' },
        { status: 400 }
      );
    }

    const apiUrl = new URL(`${API_CONFIG.shortdrama.baseUrl}/vod/parse/single`);
    apiUrl.searchParams.append('id', id);
    if (episode) apiUrl.searchParams.append('episode', episode);
    apiUrl.searchParams.append('proxy', 'true');

    const response = await fetch(apiUrl.toString(), {
      method: 'GET',
      headers: API_CONFIG.shortdrama.headers,
    });

    if (!response.ok) {
      throw new Error(`API request failed: ${response.status}`);
    }

    const data = await response.json();

    return AppResponse.json(data);
  } catch (error) {
    console.error('Short drama single parse API error:', error);

    // 返回默认数据作为备用
    return AppResponse.json({
      code: 500,
      message: 'Failed to parse episode',
      data: null,
    });
  }
}
