import { apiRequest } from "@/shared/api/request";
import { API_ENDPOINTS, API_OPERATIONS, apiEndpointPath } from "@/shared/api/generated/endpoints";
import type { UploadResultDto } from "@/shared/api/types";

export function uploadFile(file: File): Promise<UploadResultDto> {
    const endpoint = API_ENDPOINTS.uploadFile;
    const formData = new FormData();
    formData.append("file", file);

    return apiRequest<UploadResultDto, FormData>(apiEndpointPath(API_OPERATIONS.uploadFile), {
        body: formData,
        method: endpoint.method,
    });
}

export function uploadImage(file: File): Promise<UploadResultDto> {
    const endpoint = API_ENDPOINTS.uploadImage;
    const formData = new FormData();
    formData.append("image", file);

    return apiRequest<UploadResultDto, FormData>(apiEndpointPath(API_OPERATIONS.uploadImage), {
        body: formData,
        method: endpoint.method,
    });
}

export function uploadFilesBatch(files: File[]): Promise<UploadResultDto[]> {
    const formData = new FormData();
    files.forEach((file) => {
        formData.append("files", file);
    });

    return apiRequest<UploadResultDto[], FormData>("/v1/admin/files/batch-upload", {
        body: formData,
        method: "POST",
    });
}

export function uploadImagesBatch(files: File[]): Promise<UploadResultDto[]> {
    const formData = new FormData();
    files.forEach((file) => {
        formData.append("files", file);
    });

    return apiRequest<UploadResultDto[], FormData>("/v1/admin/files/image/batch-upload", {
        body: formData,
        method: "POST",
    });
}
