import { createReadStream } from "fs";
import { ApiError } from "../../middleware/ApiError";
import CloudeFormdata from 'form-data'
import axios from "axios";


export class CloudeImageService {

    private readonly api_domain: string;
    private readonly api_domainv2: string;
    private readonly api_email: string;
    private readonly api_key: string;


    constructor() {
        this.api_email = process.env.CLOUD_X_AUTH_EMAIL!;
        this.api_key = process.env.CLOUD_X_AUTH_KEY!;
        this.api_domain = process.env.CLOUD_X_AUTH_DOMAIN!;
        this.api_domainv2 = process.env.CLOUD_X_AUTH_DOMAINV2!;
    }


    async getCloudeImageService({ per_page = "1000", token = "" }: { token: string, per_page: string }) {

        const url = `${this.api_domainv2}?per_page=${per_page}&continuation_token=${token}&sort_order=desc`

        const response = await fetch(url, {
            method: 'GET',
            headers: {
                "Content-Type": "multipart/form-data",
                'X-Auth-Email': this.api_email,
                'X-Auth-Key': this.api_key
            }
        })
        return await response.json();
    }


    // get cloude image by id


    async getCloudeImageByIdService(image: string) {


        const url = `${this.api_domain}/${image}`

        const response = await fetch(url, {
            method: 'GET',
            headers: {
                "Content-Type": "multipart/form-data",
                'X-Auth-Email': this.api_email,
                'X-Auth-Key': this.api_key
            }
        })
        return await response.json();
    }





    // todo : delete cloude image from clude flair


    async deleteCloudeImageService(imageId: string) {
        try {
            const url = `${this.api_domain}/${imageId}`
            const response = await fetch(url, {
                method: 'DELETE',
                headers: {
                    "Content-Type": "multipart/form-data",
                    'X-Auth-Email': this.api_email,
                    'X-Auth-Key': this.api_key
                }
            })
            return await response.json();
        } catch (error: any) {
            throw new ApiError(error.result.message,)
        }
    }


    async createCloudeImageService(myfile: any) {
        try {
            const formdata = new CloudeFormdata();


            formdata.append('file', createReadStream(myfile.path), {
                filename: myfile.originalname,
                contentType: myfile.mimetype // Explicitly set the MIME type
            });


            const headers = {
                ...formdata.getHeaders(),
                'X-Auth-Email': this.api_email,
                'X-Auth-Key': this.api_key
            };

            const url = `${this.api_domain}`

            const response = await axios.post(url, formdata, { headers });

            return response.data;

        } catch (error: any) {
            throw new ApiError(error?.result?.message,)
        }
    }


}

export default new CloudeImageService();