// import mongoose, { Types } from "mongoose";
import * as DTO from '../dto/index';
import { Types } from 'mongoose'

export default class order_chef_list {

    static match_data_for_order_detail = async (order_id: string) => {
        try {
            return {
                $match: {
                    _id: new Types.ObjectId(order_id)
                },
            };
        } catch (err) {
            throw err;
        }
    };

    static match_data = async (chef_id: string) => {
        try {
            return {
                $match: {
                    chef_id: new Types.ObjectId(chef_id),
                    order_status: { $ne: "PENDING" },
                },
            };
        } catch (err) {
            throw err;
        }
    };

    static lookup_order_products = async () => {
        try {
            return {
                $lookup: {
                    from: "orderproducts",
                    let: { order_id: "$_id" },
                    pipeline: [
                        {
                            $match: {
                                $expr: {
                                    $eq: ["$order_id", "$$order_id"],
                                },
                            },
                        },
                        {
                            $lookup: {
                                from: "menus",
                                let: { menu_id: "$menu_id", },
                                pipeline: [
                                    {
                                        $match: {
                                            $expr: {
                                                $eq: ["$_id", "$$menu_id"],
                                            },
                                        },

                                    }
                                ],
                                as: "menu_detail",
                            },
                        },
                        {
                            $unwind: "$menu_detail",
                        },
                    ],
                    as: "order_products",
                },
            };
        } catch (err) {
            throw err;
        }
    };

    static lookup_users = async () => {
        try {
            return {
                $lookup: {
                    from: "users",
                    let: { user_id: "$user_id" },
                    pipeline: [
                        {
                            $match: {
                                $expr: {
                                    $eq: ["$_id", "$$user_id"],
                                },
                            },
                        },
                        {
                            $project: {
                                name: 1,
                                profile_pic: 1,
                                email: 1,
                                phone_no: 1,
                                country_code: 1,
                            }
                        }
                    ],
                    as: "retrive_users"

                },
            };
        } catch (err) {
            throw err;
        }
    };

    static unwind_users = async () => {
        try {
            return {
                $unwind: {
                    path: "$retrive_users",
                    preserveNullAndEmptyArrays: true,
                },
            };
        } catch (err) {
            throw err;
        }
    };

    static lookup_chef = async () => {
        try {
            return {
                $lookup: {
                    from: "users",
                    let: { chef_id: "$chef_id" },
                    pipeline: [
                        {
                            $match: {
                                $expr: {
                                    $eq: ["$_id", "$$chef_id"],
                                },
                            },
                        },
                        {
                            $project: {
                                name: 1,
                                profile_pic: 1,
                                email: 1,
                                phone_no: 1,
                                country_code: 1,
                                currency: 1,
                                country: 1,
                                currency_sign: 1,
                            }
                        }
                    ],
                    as: "retrive_chef"

                },
            };
        } catch (err) {
            throw err;
        }
    };

    static unwind_chef = async () => {
        try {
            return {
                $unwind: {
                    path: "$retrive_chef",
                    preserveNullAndEmptyArrays: true,
                },
            };
        } catch (err) {
            throw err;
        }
    };

    static lookup_driver = async () => {
        try {
            return {
                $lookup: {
                    from: "users",
                    let: { driver_id: "$driver_id" },
                    pipeline: [
                        {
                            $match: {
                                $expr: {
                                    $eq: ["$_id", "$$driver_id"],
                                },
                            },
                        },
                        {
                            $project: {
                                name: 1,
                                profile_pic: 1
                            }
                        }
                    ],
                    as: "retrive_driver_detail"

                },
            };
        } catch (err) {
            throw err;
        }
    };

    static unwind_driver = async () => {
        try {
            return {
                $unwind: {
                    path: "$retrive_driver_detail",
                    preserveNullAndEmptyArrays: true,
                },
            };
        } catch (err) {
            throw err;
        }
    };

    static facet_data = async (skip: number, limit: number) => {
        try {
            return {
                $facet: {
                    count: [
                        {
                            $count: "count"
                        }
                    ],
                    data: [
                        {
                            $sort: {
                                _id: -1 as 1 | -1
                            }
                        },
                        {
                            $skip: Number(skip)
                        },
                        {
                            $limit: Number(limit)
                        }
                    ]
                }
            }
        } catch (err) {
            throw err;
        }
    };

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

}