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

    static match_data() {
        try {
            return {
                $match:{
                    // is_active: true
                    status: "authenticated"
                }
            }
        } catch (error) {
            throw error
        }
    }

    static match_is_deleted(value: boolean) {
        try {
            return {
                $match:{
                    is_deleted: value
                }
            }
        } catch (error) {
            throw error
        }
    }

    static lookup_vendor = async () => {
        try {
            return {
                $lookup: {
                    from: "users",
                    let: { user_id: "$user_id" },
                    pipeline: [
                        {
                            $match: {
                                $expr: {
                                    $and: [
                                        { $eq: ["$$user_id", "$_id"] },
                                    ]
                                }
                            }
                        },
                        {
                            $project: {
                                _id: true,
                                name: true,
                                profile_pic: true,
                            }
                        }
                    ],
                    as: "vendor"
                },
            }
        } catch (error) {
            throw error
        }
    }

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

    static lookup_workspace_data = async () => {
        try {
            return {
                $lookup: {
                    from: "workspaces",
                    let: { workspace_id: "$workspace_id" },
                    pipeline: [
                        {
                            $match: {
                                $expr: {
                                    $and: [
                                        { $eq: ["$$workspace_id", "$_id"] },
                                    ]
                                }
                            }
                        },
                        {
                            $project: {
                                _id: true,
                                name: true,
                                image: true,
                            }
                        }
                    ],
                    as: "workspace"
                },
            }
        } catch (error) {
            throw error
        }
    }

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


    static group_data = async () => {
        try {
            return {
                $group: {
                    _id: "$_id",
                    subscription_id: { $first: "$subscription_id" },
                    vendor: { $first: "$vendor" },
                    status: { $first: "$status" },
                    plan_name: { $first: "$plan_name" },
                    amount: { $first: "$amount" },
                    workspace: { $first: "$workspace" },
                    created_at: { $first: "$created_at" },
                }
            }
        } 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;
        }
    }

    static match_with_vendor_id(vendor_id: string) {
        try {
            return {
                $match:{
                    vendor_id: new Types.ObjectId(vendor_id)
                }
            }
        } catch (error) {
            throw error
        }
    }

    static workspace_info_lookup = async () => {
        try {
            return {
                $lookup: {
                    from: "workspaces",
                    let: { workspace_id: "$workspace_id" },
                    pipeline: [
                        {
                            $match: {
                                $expr: {
                                    $and: [
                                        { $eq: ["$_id", "$$workspace_id"] },
                                    ]
                                }
                            }
                        },
                        {
                            $lookup: {
                                from: "users",
                                let: { user_id: "$created_by" },
                                pipeline: [
                                    {
                                        $match: {
                                            $expr: {
                                                $and: [
                                                    { $eq: ["$_id", "$$user_id"] },
                                                ]
                                            }
                                        }
                                    },
                                    {
                                        $project:{
                                            name: true,
                                            email: true,
                                            profile_pic: true,
                                        }
                                    }
                                ],
                                as: "owner"
                            }
                        },
                        {
                            $unwind: {
                                path: "$owner",
                                preserveNullAndEmptyArrays: true,
                            },
                        },
                        {
                            $lookup: {
                                from: "alluserworkspaces",
                                let: { workspace_id: "$_id" },
                                pipeline: [
                                    {
                                        $match: {
                                            $expr: {
                                                $and: [
                                                    { $eq: ["$workspace_id", "$$workspace_id"] },
                                                ]
                                            }
                                        }
                                    },
                                ],
                                as: "member_counts"
                            }
                        },
                        {
                            $addFields:{
                                member_counts: { $size: "$member_counts" }
                            }
                        },
                        {
                            $lookup: {
                                from: "subscriptions",
                                let: { workspace_id: "$workspace_id" },
                                pipeline: [
                                    {
                                        $match: {
                                            $expr: {
                                                $and: [
                                                    { $eq: ["$workspace_id", "$$workspace_id"] },
                                                    { $eq: ["$is_active", true] },
                                                ]
                                            }
                                        }
                                    },
                                    {
                                        $project:{
                                            plan_name: true,
                                            is_active: true,
                                            amount: true,
                                            created_at: true,
                                        }
                                    }
                                ],
                                as: "subscriptions"
                            }
                        },
                        {
                            $unwind: {
                                path: "$subscriptions",
                                preserveNullAndEmptyArrays: true,
                            },
                        },
                    ],
                    as: "workspaces"
                }
            };
        } catch (err) {
            throw err;
        }
    }

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

    static workspace_info_project() {
        try {
            return {
                // $project:{
                //     _id: "$workspaces._id",
                //     role: true,
                //     name: "$workspaces.name",
                //     image: "$workspaces.image",
                //     description: "$workspaces.description",
                //     status: "$workspaces.status",
                //     created_at: "$workspaces.created_at",
                //     member_counts: "$workspaces.member_counts",
                //     owner: "$workspaces.owner",
                //     subscriptions: "$workspaces.subscriptions",
                // }
                    $group: {
                        _id: "$workspaces._id",
                        vendor: { $first: "$vendor" },
                        role: { $first: "$role" },
                        name: { $first: "$workspaces.name" },
                        image: { $first: "$workspaces.image" },
                        description: { $first: "$workspaces.description" },
                        status: { $first: "$workspaces.status" },
                        created_at: { $first: "$workspaces.created_at" },
                        member_counts: { $first: "$workspaces.member_counts" },
                        owner: { $first: "$workspaces.owner" },
                        subscriptions: { $first: "$workspaces.subscriptions" },
                    }
                // }
            }
        } catch (error) {
            throw error
        }
    }

    static facet_data_owner_shared = async () => {
        try {
            return {
                $facet: {
                    owner: [{ $match: { role: "OWNER" } }],
                    shared: [{ $match: { role: { $ne: "OWNER" } } }],
                },
            };
        } catch (err) {
            throw err;
        }
    }


}