import { NextFunction, Request, Response } from "express";
import { CatchAsyncError } from "../../middleware/Catch";
import { CloudeImageService } from "./cloudeflair.service";
import { ApiResponse } from "../../middleware/ApiResponse";
import { ApiError } from "../../middleware/ApiError";


const CloudeFlair = new CloudeImageService()

export const getCloudeImage = CatchAsyncError(async (req: Request, res: Response, next: NextFunction) => {


    const token = req.query.continuation_token as string;
    const per_page = req.query.per_page as string;

    const FindAllCloudeImage = await CloudeFlair.getCloudeImageService({ token, per_page });

    return res.status(200).json(
        FindAllCloudeImage
    )

})




export const getCloudeImageById = CatchAsyncError(async (req: Request, res: Response, next: NextFunction) => {

    const url = req.params.imgId

    if (!url) {
        throw new ApiError("Please Enter a Image URL", 400)
    }

    const SearchImage = await CloudeFlair.getCloudeImageByIdService(url);

    if (!SearchImage?.success) {
        throw new ApiError(SearchImage?.errors[0]?.message, 400)
    }
    return res.status(200).json(SearchImage);

})




export const deleteMultipleCloudeImage = CatchAsyncError(async (req: Request, res: Response, next: NextFunction) => {


    const data = req.body;

    if (!data?.ids || !Array.isArray(data?.ids)) {
        throw new ApiError("Please Add Valid Credentials !!")
    }

    const bulkDelete = await Promise.all(data?.ids.map((e: string) => CloudeFlair.deleteCloudeImageService(e)))


    return res.status(201).json(
        new ApiResponse("All Selected Images Deleted", 201, [])
    )

})


export const deleteCloudeImage = CatchAsyncError(async (req: Request, res: Response, next: NextFunction) => {

    const dataId = req.params.imgId

    if (!dataId) {
        throw new ApiError("Please Enter ImageId !!", 400)
    }

    const FoundAndDelete = await CloudeFlair.deleteCloudeImageService(dataId)

    if (!FoundAndDelete?.success) {
        throw new ApiError(FoundAndDelete?.errors[0]?.message, 400)
    }

    return res.status(201).json(
        new ApiResponse("Image Deleted Successfully", 201, [])
    )

})





export const createCloudeImage = CatchAsyncError(async (req: Request, res: Response, next: NextFunction) => {


    const data = req.files;

    if (!data || !Array.isArray(data)) {
        throw new ApiError("Please Enter Valid Credentials !!", 400)
    }

    const response = await Promise.allSettled(data.map((images) => CloudeFlair.createCloudeImageService(images)))


    const filter = response.reduce((accu, curr) => {
        if (curr?.status === "fulfilled") {
            accu.trueCount++;
        } else {
            accu.falseCount++;
        }
        return accu;
    }, { trueCount: 0, falseCount: 0 });



    return res.status(201).json(
        new ApiResponse(`Uploaded: ${filter.trueCount} , Failed: ${filter.falseCount}`, 201, [])
    )

})
