import { Types } from "mongoose"

export class admin_ai_agent_aggrigation {

    static match_with_agent_id(agent_id: any) {
        try {
            return {
                $match: {
                    agent_id: new Types.ObjectId(agent_id)
                }
            }
        } catch (error) {
            throw error
        }
    }

    static emotionGroupStage() {
        try {
            return {
                _id: null,
                total: { $sum: 1 },
                positive: {
                    $sum: {
                        $cond: {
                            if: { $in: ["$emotion", ["Customer Satisfied\n", "Customer Interested\n"]] },
                            then: 1,
                            else: 0
                        }
                    }
                },
                negative: {
                    $sum: {
                        $cond: {
                            if: { $in: ["$emotion", ["Customer Frustrated\n", "Customer Angry\n"]] },
                            then: 1,
                            else: 0
                        }
                    }
                },
                neutral: {
                    $sum: {
                        $cond: {
                            if: { $in: ["$emotion", ["Customer Indifferent\n", "Customer uninterested\n"]] },
                            then: 1,
                            else: 0
                        }
                    }
                }
            };
        } catch (error) {
            throw error;
        }
    }
    
    static emotionProjectStage() {
        try {
            return {
                _id: 0,
                positive: { $round: [{ $multiply: [{ $divide: ["$positive", "$total"] }, 100] }, 2] },
                negative: { $round: [{ $multiply: [{ $divide: ["$negative", "$total"] }, 100] }, 2] },
                neutral: { $round: [{ $multiply: [{ $divide: ["$neutral", "$total"] }, 100] }, 2] }
            };
        } catch (error) {
            throw error;
        }
    }

    static lookup_chat = async () => {
        try {
            return {
                $lookup: {
                    from: "calls",
                    let: { agent_id: "$_id" },
                    pipeline: [
                        {
                            $match: {
                                $expr: {
                                    $and: [
                                        { $eq: ["$agent_id", "$$agent_id"] },
                                        { $eq: ["$type", "CHAT"] },
                                    ]
                                }
                            }
                        },

                    ],
                    as: "chat_list"
                }
            }
        } catch (error) {
            throw error
        }
    }

    static lookup_call = async () => {
        try {
            return {
                $lookup: {
                    from: "calls",
                    let: { agent_id: "$_id" },
                    pipeline: [
                        {
                            $match: {
                                $expr: {
                                    $and: [
                                        { $eq: ["$agent_id", "$$agent_id"] },
                                        { $eq: ["$type", "CALL"] },
                                    ]
                                }
                            }
                        },

                    ],
                    as: "call_list"
                }
            }
        } catch (error) {
            throw error
        }
    }

    static lookup_voice_call = async () => {
        try {
            return {
                $lookup: {
                    from: "calls",
                    let: { agent_id: "$_id" },
                    pipeline: [
                        {
                            $match: {
                                $expr: {
                                    $and: [
                                        { $eq: ["$agent_id", "$$agent_id"] },
                                        { $eq: ["$type", "VOICE_CHAT"] },
                                    ]
                                }
                            }
                        },

                    ],
                    as: "voice_chat_count"
                }
            }
        } catch (error) {
            throw error
        }
    }

    static add_fields = async () => {
        try {
            return {
                $addFields: {
                    chat_count: { $size: '$chat_list' },
                    call_list: { $size: '$call_list' },
                    voice_chat_count: { $size: '$voice_chat_count' },
                }
            }
        } catch (error) {
            throw error
        }

    }

    static lookup_twilio_config = async () => {
        try {
            return {
                $lookup: {
                    from: "twilioconfigs",
                    let: { twilio: "$twilio_config" },
                    pipeline: [
                        {
                            $match: {
                                $expr: {
                                    $and: [
                                        { $eq: ["$_id", "$$twilio"] },
                                    ]
                                }
                            }
                        },
                        {
                            $project:{
                                _id: true,
                                phone_number: true
                            }
                        }
                    ],
                    as: "twilio_config"
                }
            }
        } catch (error) {
            throw error
        }
    }

    static unwind_twilio_config() {
        try {
            return {
                $unwind: {
                    path: "$twilio_config",
                    preserveNullAndEmptyArrays: true,
                }
            }
        } catch (error) {
            throw error
        }
    }

    static lookup_script_data = async () => {
        try {
            return {
                $lookup: {
                    from: "scripts",
                    let: { id: "$_id" },
                    pipeline: [
                        {
                            $match: {
                                $expr: {
                                    $and: [
                                        { $eq: ["$agent_id", "$$id"] },
                                    ]
                                }
                            }
                        },
                        {
                            $project:{
                                _id: true,
                                key: true,
                                title: true,
                                agent_id: true,
                                image: true,
                                description: true,
                                colour: true,
                                font_colour: true,
                                script_status: true,
                            }
                        }
                    ],
                    as: "script"
                }
            }
        } catch (error) {
            throw error
        }
    }

    static unwind_script_data() {
        try {
            return {
                $unwind: {
                    path: "$script",
                    preserveNullAndEmptyArrays: true,
                }
            }
        } catch (error) {
            throw error
        }
    }

    static group_data = async () => {
        try {
            return {
                $group: {
                    _id: "$_id",
                    name: { $first: "$name" },
                    type: { $first: "$type" },
                    image: { $first: "$image" },
                    voice: { $first: "$voice" },
                    status: { $first: "$status" },
                    ai_model: { $first: "$ai_model" },
                    call_list: { $first: "$call_list" },
                    chat_count: { $first: "$chat_count" },
                    created_at: { $first: "$created_at" },
                    default_type: { $first: "$default_type" },
                    voice_chat_count: { $first: "$voice_chat_count" },
                    twilio_config: { $first: "$twilio_config" },
                    script: { $first: "$script" },
                }
            }
        } catch (error) {
            throw error
        }
    }

    static facet_data = async (page: any, limit: any) => {
        try {
            page = page != undefined ? page : 0;
            limit = limit != undefined ? limit : 10;
            return {
                $facet: {
                    count: [{ $count: "count" }],
                    data: [
                        { $sort: { _id: -1 } as { _id: -1 } },
                        { $skip: parseInt(page) * parseInt(limit) },
                        { $limit: parseInt(limit) },
                    ],
                },
            };
        } catch (err) {
            throw err;
        }
    }

    static project_count = async () => {
        try {
            return {
                $project: {
                    count: { $arrayElemAt: ["$count.count", 0] },
                    data: 1,
                },
            };
        } catch (err) {
            throw err;
        }
    }

}