import { fetchApi } from '@/lib/server-api-util';
import { ConfirmUpload, UploadUrlResponse } from './types';

// 获取上传URL
export const getUploadUrl = async (file: File, metadata: Record<string, any> = {}): Promise<UploadUrlResponse> => {
    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: metadata.isCompressed || 'false',
        },
    };
    return await fetchApi<UploadUrlResponse>('/file/generate-upload-url', {
        method: 'POST',
        body: JSON.stringify(body)
    });
};

// 确认上传
// 确认上传
export const confirmUpload = async (file_id: string): Promise<ConfirmUpload> => {
    return await fetchApi<ConfirmUpload>('/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
        }])
    });
}