import axios from 'axios';

const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || '';

// AI换背景API接口
const apiRequest = async (endpoint, options = {}) => {
    const baseURL = import.meta.env.VITE_API_BASE_URL || 'http://localhost:8000';
    const url = `${baseURL}${endpoint}`;
    
    // 获取认证token
    const token = localStorage.getItem('authToken');
    
    const defaultOptions = {
      headers: {
        'Content-Type': 'application/json',
        ...(token && { 'Authorization': `Bearer ${token}` }),
      },
      ...options,
    };
  
    try {
      const response = await fetch(url, defaultOptions);
      
      if (!response.ok) {
        const errorText = await response.text();
        throw new Error(`HTTP error! status: ${response.status} - ${errorText}`);
      }
      
      return await response.json();
    } catch (error) {
      console.error('API request failed:', error);
      throw error;
    }
  };

// AI换背景API类
export class AISwapBgAPI {
    constructor() {
        this.baseURL = '/api/v1';
    }

    /**
     * 执行AI换背景任务
     * @param {Object} swapBgData - 换背景数据
     * @param {number} swapBgData.user_id - 用户ID
     * @param {number} swapBgData.image_id - 图片ID
     * @param {string} swapBgData.prompt - 提示词
     * @param {number} [swapBgData.quantity] - 每组生成数量（可选）
     * @returns {Promise<Object>} 任务提交结果
     */


    async processSwapBg(swapBgData) {
        return await apiRequest(`${this.baseURL}/swap-bg`, {
            method: 'POST',
            body: JSON.stringify(swapBgData),
        });
    }
}

// 创建单例实例
export const aiSwapBgAPI = new AISwapBgAPI();

    
    
    
    
