import { Types } from 'mongoose'
export class workspace_aggregation {

    static match_data() {
        try {
            return {
                $match: {}
            }
        } catch (error) {
            throw error
        }
    }
    static match_workspace_data(wokspace_id: string) {
        try {
            return {
                $match: { _id: new Types.ObjectId(wokspace_id) }
            }
        } catch (error) {
            throw error
        }
    }
    static lookup_owner = async () => {
        try {
            return {
                $lookup: {
                    from: "users",
                    let: { user_id: "$created_by" },
                    pipeline: [
                        {
                            $match: {
                                $expr: {
                                    $and: [
                                        { $eq: ["$_id", "$$user_id"] },
                                    ]
                                }
                            }
                        },
                        {
                            $project: {
                                name: 1,
                                profile_pic: 1
                            }
                        }
                    ],
                    as: "owner_data"
                }
            }
        } catch (error) {
            throw error
        }
    }

    static unwind_owner = async () => {
        try {
            return {
                $unwind: {
                    path: "$owner_data",
                    preserveNullAndEmptyArrays: true,
                },
            };
        } catch (error) {
            throw error;
        }
    }

    static find_members = async () => {
        try {
            return {
                $lookup: {
                    from: "alluserworkspaces",
                    let: { workspace_id: "$_id" },
                    pipeline: [
                        {
                            $match: {
                                $expr: {
                                    $and: [
                                        { $eq: ["$workspace_id", "$$workspace_id"] },
                                    ]
                                }
                            }
                        },

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

    static add_fields = async () => {
        try {
            return {
                $addFields: {
                    workspace_members_count: { $size: '$workspace_members' },
                }
            }
        } catch (error) {
            throw error
        }
    }

    static lookup_subscription = async () => {
        try {
            return {
                $lookup: {
                    from: "subscriptions",
                    let: { id: "$_id" },
                    pipeline: [
                        {
                            $match: {
                                $expr: {
                                    $and: [
                                        { $eq: ["$$id", "$workspace_id"] },
                                        { $eq: ["$is_active", true] },
                                    ]
                                }
                            }
                        },
                        {
                            $project: {
                                _id: true,
                                plan_name: true,
                                amount: true,
                            }
                        }
                    ],
                    as: "subscriptions"
                }
            }
        } catch (error) {
            throw error
        }
    }


    static unwind_subscription = async () => {
        try {
            return {
                $unwind: {
                    path: "$subscriptions",
                    preserveNullAndEmptyArrays: true,
                },
            };
        } catch (error) {
            throw error;
        }
    }

    static group_data = async () => {
        try {
            return {
                $group: {
                    _id: "$_id",
                    name: { $first: "$name" },
                    image: { $first: "$image" },
                    status: { $first: "$status" },
                    owner_data: { $first: "$owner_data" },
                    amount: { $first: "$subscriptions.amount" },
                    workspace_members_count: { $first: "$workspace_members_count" },
                    created_at: { $first: "$created_at" },
                    updated_at: { $first: "$updated_at" },
                }
            }
        } catch (error) {
            throw error
        }
    }

    static project_data = async () => {
        try {
            return {
                $project: {
                    _id: true,
                    name: true,
                    image: true,
                    status: true,
                    owner_data: true,
                    subscription_data: "$subscriptions",
                    workspace_members_count: true,
                    created_at: true,
                    updated_at: true,
                }
            }
        } catch (error) {
            throw error
        }
    }

    static facetData = 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;
        }
    }


}