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

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

    async processGenVideo(genVideoData) {
        return await apiRequest(`${this.baseURL}/i2v`, {
            method: 'POST',
            body: JSON.stringify(genVideoData)
        });
    }
}

// 创建单例实例
export const aiGenVideoAPI = new AIGenVideoAPI();