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 AISwapFaceAPI {
    constructor() {
        this.baseURL = '/api/v1';
    }

    /**
     * 执行AI换脸任务
     * @param {Object} swapFaceData - 换脸数据
     * @param {number} swapFaceData.user_id - 用户ID
     * @param {number} swapFaceData.raw_image_id - 原始图片ID
     * @param {number} swapFaceData.face_image_id - 人脸图片ID
     * @param {number} [swapFaceData.quantity] - 每组生成数量（可选）
     * @returns {Promise<Object>} 任务提交结果
     */

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

// 创建单例实例
export const aiSwapFaceAPI = new AISwapFaceAPI();