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

export default class earning_list {

  static match_data = async () => {
    try {
      return {
        $match: { added_by: "ADMIN" }
      };
    } catch (err) {
      throw err;
    }
  };

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

        ],
        as: "orders",
      },
    }
  };

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

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

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

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

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

  static group_data = async () => {
    return {
      $group: {
        _id: "$orders._id",
        // order_id: { $first: "$_id" },
        users: { $first: "$users" },
        stores: { $first: "$stores" },
        total_prices: { $first: "$orders.total_prices" }
      },
    };
  };

  static redact_data = async (payload_data: any) => {
    // console.log('payload-order-list ', payload_data)
    try {
      let { search, type } = payload_data;
      return {
        $redact: {
          $cond: {
            if: {
              $and: [
                {
                  $or: [
                    { $eq: [search, undefined] },
                    {
                      $regexMatch: {
                        input: "$stores.store_name",
                        regex: search,
                        options: "i"
                      }
                    }
                  ]
                },
                {
                  $or: [
                    { $eq: [type, undefined] },
                    { $eq: ["$stores.type", type] }
                  ]
                },
              ],
            },
            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"
            }
          ],
          total_earning: [
            {
              $group: {
                _id: null,
                total: { $sum: "$total_prices" }
              }
            }
          ],

          data: [
            {
              $sort: {
                _id: -1 as 1 | -1
              }
            },
            {
              $skip: Number(pagination) * Number(limit)
            },
            {
              $limit: Number(limit)
            }
          ]
        }
      }
    }
    catch (err) {
      throw err;
    }
  }


  //<<<<<<<<<<<<--------DashboardEarning---------------------->>>>>>>>>


  static match_store = async () => {
    try {
      return {
        $match: { added_by: "ADMIN" }
      };
    } catch (err) {
      throw err;
    }
  };

  static lookup_order_dasboard = async () => {
    return {
      $lookup: {
        from: "orders",
        let: { store_id: "$_id" },
        pipeline: [
          {
            $match: {
              $expr: {
                $and: [
                  { $eq: ["$store_id", "$$store_id"] },
                ]
              },
            },
          },
          {
            $project: {
              total_prices: 1,
            },
          },
          {
            $group: {     
              _id: null,
              total_earning: { $sum: "$total_prices" }
            },
          },
        ],
        as: "orders",
      },
    }
  };

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

  static group_data1 = async () => {
    return {
      $group: {
        _id: "$_id",
        total: { $first: "$orders.total_earning" }
      },
    };
  }


  static facet1 = async () => {
    try {
      return {
        $facet: {
          total_amount: [
            {
              $group: {
                _id: null,
                total_earning: { $sum: '$total' }
              }
            }
          ],
        }
      }
    }
    catch (err) {
      throw err;
    }
  }

}