// ComfyUI 后端集成
const API_BASE_URL = 'https://your-backend-domain.com'; // 替换为你的后端地址

export const ComfyUIIntegration = {
  
  // 换IP生成 (人脸替换)
  async generateFaceSwap({ sourceImageUrl, targetFaceUrl, style = 'natural' }) {
    const response = await fetch(`${API_BASE_URL}/api/comfyui/face-swap`, {
      method: 'POST',
      headers: { 
        'Content-Type': 'application/json',
        // 如果需要认证，添加Authorization头
        // 'Authorization': `Bearer ${your_token}`
      },
      body: JSON.stringify({
        source_image: sourceImageUrl,
        target_face: targetFaceUrl,
        style: style,
        quality: 'high'
      })
    });
    
    if (!response.ok) {
      const error = await response.json();
      throw new Error(error.message || 'Face swap failed');
    }
    
    return await response.json();
  },

  // 换衣服生成
  async generateClothingSwap({ modelImageUrl, clothingImageUrl, pose = 'auto' }) {
    const response = await fetch(`${API_BASE_URL}/api/comfyui/clothing-swap`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        model_image: modelImageUrl,
        clothing_image: clothingImageUrl,
        pose_control: pose,
        preserve_face: true,
        quality: 'high'
      })
    });
    
    if (!response.ok) {
      const error = await response.json();
      throw new Error(error.message || 'Clothing swap failed');
    }
    
    return await response.json();
  },

  // 智能文案生成
  async generateSmartCopy({ productName, style, platform, keywords = [] }) {
    const response = await fetch(`${API_BASE_URL}/api/comfyui/generate-copy`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        product_name: productName,
        style: style,
        platform: platform,
        keywords: keywords,
        length: 'medium'
      })
    });
    
    if (!response.ok) {
      const error = await response.json();
      throw new Error(error.message || 'Copy generation failed');
    }
    
    return await response.json();
  },

  // 背景替换/生成
  async generateBackground({ subjectImageUrl, backgroundPrompt, style = 'realistic' }) {
    const response = await fetch(`${API_BASE_URL}/api/comfyui/background-replace`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        subject_image: subjectImageUrl,
        background_prompt: backgroundPrompt,
        style: style,
        blend_mode: 'natural',
        quality: 'high'
      })
    });
    
    if (!response.ok) {
      const error = await response.json();
      throw new Error(error.message || 'Background generation failed');
    }
    
    return await response.json();
  },

  // 查询任务状态
  async getTaskStatus(taskId) {
    const response = await fetch(`${API_BASE_URL}/api/comfyui/task-status/${taskId}`);
    if (!response.ok) {
      throw new Error('Failed to get task status');
    }
    return await response.json();
  },

  // 获取生成结果
  async getTaskResult(taskId) {
    const response = await fetch(`${API_BASE_URL}/api/comfyui/task-result/${taskId}`);
    if (!response.ok) {
      throw new Error('Failed to get task result');
    }
    return await response.json();
  }

};