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

export default class order_list {

    static match_data = async () => {
        try {
            return {
                $match: {
                },
            };
        } 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"],
                                },
                            },
                        },
                    ],
                    as: "order_products",
                },
            };
        } catch (err) {
            throw err;
        }
    };

    static unwind_order_products = async () => {
        try {
            return {
                $unwind: {
                    path: "$order_products",
                    preserveNullAndEmptyArrays: true,
                },
            };
        } 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"],
                                },
                            },
                        },
                    ],
                    as: "retrive_users"

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

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

    static lookup_products = async () => {
        try {
            return {
                $lookup: {
                    from: "products",
                    let: { product_id: "$order_products.product_id" },
                    pipeline: [
                        {
                            $match: {
                                $expr: {
                                    $eq: ["$_id", "$$product_id"],
                                },
                            },
                        },
                        {
                            $project: {
                                _id: 1,
                                name: 1,
                                description: 1,
                                images: 1,
                            },
                        },
                        {
                            $lookup: {
                                from: "reviews",
                                let: { product_id: "$_id", },
                                pipeline: [
                                    {
                                        $match: {
                                            $expr: {
                                                $eq: ["$product_id", "$$product_id"],
                                            },
                                        },
                                    },
                                    {
                                        $lookup: {
                                            from: "users",
                                            let: { user_id: "$user_id" },
                                            pipeline: [
                                                {
                                                    $match: {
                                                        $expr: {
                                                            $eq: ["$_id", "$$user_id"],
                                                        },
                                                    },
                                                },
                                                {
                                                    $project: {
                                                        name: 1,
                                                        profile_pic: 1,
                                                    },
                                                },
                                            ],
                                            as: "user_detail",
                                        },
                                    },
                                    {
                                        $unwind: "$user_detail",
                                    },
                                ],
                                as: "reviews",
                            },
                        },
                    ],
                    as: "products",
                },
            };
        } catch (err) {
            throw err;
        }
    };

    static lookup_products_order = async () => {
        try {
            return {
                $lookup: {
                    from: "products",
                    let: { product_id: "$order_products.product_id" },
                    pipeline: [
                        {
                            $match: {
                                $expr: {
                                    $eq: ["$_id", "$$product_id"],
                                },
                            },
                        },
                        {
                            $project: {
                                _id: 1,
                                name: 1,
                                description: 1,
                                images: 1,
                            },
                        },
                    ],
                    as: "product",
                },
            };
        } catch (err) {
            throw err;
        }
    };

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

    static lookup_reviewd_products = async (user_id: any) => {
        try {
            return {
                $lookup: {
                    from: "products",
                    let: { product_id: "$order_products.product_id" },
                    pipeline: [
                        {
                            $match: {
                                $expr: {
                                    $eq: ["$_id", "$$product_id"],
                                },
                            },
                        },
                        {
                            $project: {
                                _id: 1,
                                name: 1,
                                description: 1,
                                images: 1,
                            },
                        },
                        {
                            $lookup: {
                                from: "reviews",
                                let: { product_id: "$_id", user_id: user_id },
                                pipeline: [
                                    {
                                        $match: {
                                            $expr: {
                                                // $eq: ["$product_id", "$$product_id"],
                                                $and: [
                                                    {
                                                        $eq: ["$product_id", "$$product_id"],
                                                    },
                                                    {
                                                        $eq: ["$user_id", "$$user_id"],
                                                    },
                                                ],
                                            },
                                        },
                                    },
                                    {
                                        $lookup: {
                                            from: "users",
                                            let: { user_id: "$user_id" },
                                            pipeline: [
                                                {
                                                    $match: {
                                                        $expr: {
                                                            $eq: ["$_id", "$$user_id"],
                                                        },
                                                    },
                                                },
                                                {
                                                    $project: {
                                                        name: 1,
                                                        profile_pic: 1,
                                                    },
                                                },
                                            ],
                                            as: "user_detail",
                                        },
                                    },
                                    {
                                        $unwind: "$user_detail",
                                    },
                                ],
                                as: "reviews",
                            },
                        },
                        {
                            $unwind: {
                                path: "$reviews",
                                preserveNullAndEmptyArrays: true,
                            },
                        },
                    ],
                    as: "products",
                },
            };
        } catch (err) {
            throw err;
        }
    };

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

    static lookup_store = async () => {
        try {
            return {
                $lookup: {
                    from: "stores",
                    let: { store_id: "$store_id" },
                    pipeline: [
                        {
                            $match: {
                                $expr: {
                                    $eq: ["$_id", "$$store_id"],
                                },
                            },
                        },
                    ],
                    as: "order_stores",
                },
            };
        } catch (err) {
            throw err;
        }
    };

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

    static group_data = async () => {
        try {
            return {
                $group: {
                    _id: "$order_products._id",
                    orderId: { $first: "$order_id" },
                    productOrderId: { $first: "$order_products.product_order_id" },
                    order_id: { $first: "$_id" },
                    store_type: { $first: "$order_stores.type" },
                    store_id: { $first: "$store_id" },
                    user_id: { $first: "$user_id" },
                    seller_id: { $first: "$order_products.seller_id" },
                    product_id: { $first: "$product._id" },
                    product_image: { $first: "$product.images" },
                    product_description: { $first: "$product.description" },
                    quantity: { $first: "$order_products.quantity" },
                    price: { $first: "$order_products.price" },
                    delivery_price: { $first: "$order_products.delivery_price" },
                    coupon_discount: { $first: "$order_products.coupon_discount" },
                    total_price: { $first: "$order_products.total_price" },
                    total_earnings: { $first: "$order_products.total_earnings" },
                    shippo_data: { $first: "$order_products.shippo_data" },
                    order_status: { $first: "$order_products.order_status" },
                    tracking_link: { $first: "$order_products.tracking_link" },
                    stripe_data: { $first: "$stripe_data" },
                    updated_at: { $first: "$order_products.updated_at" },
                    shipped_at: { $first: "$order_products.shipped_at" },
                    delivery_date: { $first: "$order_products.delivery_date" },
                    created_at: { $first: "$order_products.created_at" },
                },
            };
        } catch (err) {
            throw err;
        }
    };


    static redact_data = async (payload_data: any) => {
        // console.log('payload-order-list ', payload_data)
        try {
            // let { order_status } = payload_data;
            let { status_type, store_type, store_id, user_id, search } = payload_data;
            // console.log("user_id......", user_id);

            if (status_type == "ALL") {
                status_type = undefined;
            } else if (status_type == "PLACED") {
                status_type = "PLACED";
            } else if (status_type == "DELIVERED") {
                status_type = "DELIVERED";
            } else if (status_type == "CANCELLED") {
                status_type = "CANCELLED"
            }
            return {
                $redact: {
                    $cond: {
                        if: {
                            $and: [
                                {
                                    $or: [
                                        { $eq: [search, undefined] },
                                        {
                                            $regexMatch: {
                                                input: "$store_name",
                                                regex: search,
                                                options: "i"
                                            }
                                        }
                                    ]
                                },
                                {
                                    $or: [
                                        { $eq: [status_type, undefined] },
                                        { $eq: ["$order_status", status_type] },
                                    ],
                                },
                                {
                                    $or: [
                                        { $eq: [store_type, undefined] },
                                        { $eq: ["$store_type", store_type] },
                                    ],
                                },
                                {
                                    $or: [
                                        { $eq: [store_id, undefined] },
                                        { $eq: ["$store_id", new Types.ObjectId(store_id)] },
                                    ],
                                },
                                {
                                    $or: [
                                        { $eq: [user_id, undefined] },
                                        { $eq: ["$user_id", new Types.ObjectId(user_id)] },
                                    ],
                                },
                            ],
                        },
                        then: "$$KEEP",
                        else: "$$PRUNE",
                    },
                },
            };
        } catch (err) {
            throw err;
        }
    };

    static sort_data = async () => {
        try {

            return {
                $sort: {
                    _id: -1
                }
            }
        }
        catch (err) {
            throw err;
        }
    }
    static set_data = async () => {
        try {
            return {
                $set: {
                    is_liked: {
                        $cond: {
                            if: {
                                $gt: [{ $size: "$comment_likes" }, 0]
                            },
                            then: true,
                            else: false
                        }
                    }
                }
            }
        }
        catch (err) {
            throw err;
        }
    }

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


    static group_order = async () => {
        try {
            return {
                $group: {
                    _id: "$order_id",

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

    static lookup_order = async () => {
        return {
            $lookup: {
                from: "orders",
                let: { order_id: "$_id" },
                pipeline: [
                    {
                        $match: {
                            $expr: {
                                $eq: ["$_id", "$$order_id"],
                            },
                        },
                    },
                    {
                        $project: {
                            // _id: 1,
                            total_prices: 1,
                            store_id: 1,
                            order_status: 1,
                            user_id: 1
                        },
                    },

                ],
                as: "orders",
            },
        }

    };

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

                            ],
                            as: "user_id",
                        },
                    },
                    {
                        $unwind: {
                            path: "$user_id",
                            preserveNullAndEmptyArrays: true
                        },
                    },
                    {
                        $project: {
                            // _id: 1,
                            total_prices: 1,
                            store_id: 1,
                            order_status: 1,
                            user_id: 1,
                            chef_id: 1,
                        },
                    },

                ],
                as: "orders",
            },
        }

    };

    static unwind_order = async () => {
        return {
            $unwind: {
                path: "$orders",
                preserveNullAndEmptyArrays: true,
            },
        };
    };



    static lookup_store2 = async () => {
        return {
            $lookup: {
                from: "stores",
                let: { store_id: "$orders.store_id" },
                pipeline: [
                    {
                        $match: {
                            $expr: {
                                $eq: ["$_id", "$$store_id"],
                            },
                        },
                    },
                    {
                        $project: {
                            // _id: 1,
                            store_name: 1,
                            type: 1,
                            images: 1

                        },
                    },

                ],
                as: "stores",
            },
        }

    };

    static unwind_stores = async () => {
        return {
            $unwind: {
                path: "$stores",
                preserveNullAndEmptyArrays: true,
            },
        };
    };

    static lookup_chefs = async () => {
        return {
            $lookup: {
                from: "users",
                let: { chef_id: "$orders.chef_id" },
                pipeline: [
                    {
                        $match: {
                            $expr: {
                                $eq: ["$_id", "$$chef_id"],
                            },
                        },
                    },
                    {
                        $project: {
                            // _id: 1,
                            name: 1,
                            profile_pic: 1,

                        },
                    },

                ],
                as: "chef_detail",
            },
        }

    };

    static unwind_chef = async () => {
        return {
            $unwind: {
                path: "$chef_detail",
                preserveNullAndEmptyArrays: true,
            },
        };
    };

    static lookup_order_products1 = async () => {
        return {
            $lookup: {
                from: "orderproducts",
                let: { order_id: "$_id" },
                pipeline: [
                    {
                        $match: {
                            $expr: {
                                $eq: ["$order_id", "$$order_id"],
                            },
                        },
                    },
                    {
                        $lookup: {
                            from: "products",
                            let: { product_id: "$product_id" },
                            pipeline: [
                                {
                                    $match: {
                                        $expr: {
                                            $eq: ["$_id", "$$product_id"],
                                        },
                                    },

                                },
                                {
                                    $project: {
                                        _id: 1,
                                        name: 1,
                                        description: 1,
                                        images: 1,
                                        price: 1,
                                        discount_price: 1
                                    },
                                },
                            ],
                            as: "products",
                        },
                    },
                    {
                        $unwind: "$products",
                    },
                ],
                as: "order_products",
            },
        };

    };

    static group_data1 = async () => {
        return {
            $group: {
                _id: "$_id",
                order_id: { $first: "$_id" },
                user_id: { $first: "$orders.user_id" },
                store_type: { $first: "$stores.type" },
                store_id: { $first: "$stores._id" },
                store_name: { $first: "$stores.store_name" },
                chef_detail:{$first:"$chef_detail"},
                images: { $first: "$stores.images" },
                order_status: { $first: "$orders.order_status" },
                total_price: { $first: "$orders.total_prices" },
                order_products: { $first: "$order_products" }

            },
        };
    };


    //......Driver_order_list.................>>>>>>>>>>>>>>>>>>>>>>>>>>>


    static match_driver_order = async (driver_id: string, dto: DTO.order_list_driver) => {
        try {
            // console.log("dto");

            let { status_type } = dto;

            let query: any = { driver_id: new Types.ObjectId(driver_id) }
            if (status_type == "ACCEPTED") {
                query.is_is_accepted = true

            } else if (status_type == "DELIVERED") {
                query.is_delivered = true
            }
            else if (status_type == "CANCELLED") {
                query.is_cancelled = true
            }
            else if (status_type == "PICKUPED") {
                query.is_pickup = true
            }
            // console.log("query.....", query);

            return {
                $match: query
            };
        } catch (err) {
            throw err;
        }
    };


    static lookup_orders = async () => {
        try {
            return {
                $lookup: {
                    from: "orders",
                    let: { order_id: "$order_id" },
                    pipeline: [
                        {
                            $match: {
                                $expr: {
                                    $eq: ["$_id", "$$order_id"],
                                },
                            },
                        },
                    ],
                    as: "retrive_orders",
                },
            };
        } catch (err) {
            throw err;
        }
    };

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


    static lookup_order_users = async () => {
        try {
            return {
                $lookup: {
                    from: "users",
                    let: { user_id: "$retrive_orders.user_id" },
                    pipeline: [
                        {
                            $match: {
                                $expr: {
                                    $eq: ["$_id", "$$user_id"],
                                },
                            },
                        },
                    ],
                    as: "retrive_users"

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

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



    static lookup_order_address = async () => {
        try {
            return {
                $lookup: {
                    from: "addresses",
                    let: { address_id: "$retrive_orders.address_id" },
                    pipeline: [
                        {
                            $match: {
                                $expr: {
                                    $eq: ["$_id", "$$address_id"],
                                },
                            },
                        },
                    ],
                    as: "retrive_address"

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

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

    static lookup_order_store = async () => {
        try {
            return {
                $lookup: {
                    from: "stores",
                    let: { store_id: "$retrive_orders.store_id" },
                    pipeline: [
                        {
                            $match: {
                                $expr: {
                                    $eq: ["$_id", "$$store_id"],
                                },
                            },
                        },
                    ],
                    as: "order_stores",
                },
            };
        } catch (err) {
            throw err;
        }
    };

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


    static group_driver_order = async () => {
        return {
            $group: {
                _id: "$_id",
                orders: { $first: "$retrive_orders" },
                users: { $first: "$retrive_users" },
                address: { $first: "$retrive_address" },
                stores: { $first: "$order_stores" },
                is_accepted: { $first: "$is_accepted" },
                is_delivered: { $first: "$is_delivered" },
                is_cancelled: { $first: "$is_cancelled" },
                is_pickup: { $first: "$is_pickup" }



            },
        };
    };

    static redact_driver_order = async (payload_data: any) => {
        // console.log('payload-order-list ', payload_data)
        try {
            // let { order_status } = payload_data;
            let { status_type } = payload_data;

            // ACCEPTED = 'ACCEPTED',
            // DELIVERED = 'DELIVERED',
            // CANCELLED = 'CANCELLED',
            // PICKUPED = 'PICKUPED',

            // if (status_type == "ACCEPTED") {
            //   status_type = undefined;
            // } else if (status_type == "DELIVERED") {
            //   status_type = "PLACED";
            // } else if (status_type == "CANCELLED") {
            //   status_type = "DELIVERED";
            // } else if (status_type == "PICKUPED") {
            //   status_type = "CANCELLED"
            // }
            return {
                $redact: {
                    $cond: {
                        if: {
                            $and: [
                                // {
                                //   $or: [
                                //     { $eq: [user_id, undefined] },
                                //     { $eq: ["$user_id", new Types.ObjectId(user_id)] },
                                //   ],
                                // },
                            ],
                        },
                        then: "$$KEEP",
                        else: "$$PRUNE",
                    },
                },
            };
        } catch (err) {
            throw err;
        }
    };
}