import { fetchApi } from '@/lib/server-api-util';

// 获取上传URL
export const getUploadUrl = async (file: File, metadata: Record<string, any> = {}): Promise<{ upload_url: string; file_id: string }> => {

    const body = {
        filename: file.name,
        content_type: file.type,
        file_size: file.size,
        metadata: {
            ...metadata,
            originalName: file.name,
            fileType: file.type.startsWith('video/') ? 'video' : 'image',
            isCompressed: 'true',
        },
    };

    return await fetchApi<{ upload_url: string; file_id: string }>("/file/generate-upload-url", {
        method: 'POST',
        body: JSON.stringify(body)
    });
};

// 确认上传
export const confirmUpload = async (file_id: string) => {
    return await fetchApi('/file/confirm-upload', {
        method: 'POST',
        body: JSON.stringify({ file_id })
    });
};

// 新增素材
export const addMaterial = async (file: string, compressFile: string) => {
    await fetchApi('/material', {
        method: 'POST',
        body: JSON.stringify([{
            "file_id": file,
            "preview_file_id": compressFile
        }])
    });
}