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

// export default class order_list {

//   static match_data = async (user_id: string) => {
//     try {
//       return {
//         $match: {
//           user_id: new Types.ObjectId(user_id),
//         },
//       };
//     } 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_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_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 group_data = async () => {
//     try {
//       return {
//         $group: {
//           _id: "$order_products._id",
//           orderId: { $first: "$order_id" },
//           productOrderId: { $first: "$order_products.product_order_id" },
//           order_id: { $first: "$order_products.order_id" },
//           user_id: { $first: "$user_id" },
//           seller_id: { $first: "$order_products.seller_id" },
//           product_id: { $first: "$products" },
//           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;
//       if (order_status == "ALL") {
//         order_status = undefined;
//       } else if (order_status == "CONFIRMED") {
//         order_status = ["PLACED", "SHIPPED"];
//       } else if (order_status == "DELIVERED") {
//         order_status = ["DELIVERED"];
//       } else if (order_status == "CANCELLED") {
//         order_status = ["CANCELLED", "PENDING_CANCELLATION"];
//       }
//       return {
//         $redact: {
//           $cond: {
//             if: {
//               $and: [
//                 {
//                   $or: [
//                     { $eq: [order_status, undefined] },
//                     { $in: ["$order_status", order_status] },
//                   ],
//                 },
//               ],
//             },
//             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 () => {
//     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;
//     }
//   }

// }



// ///////////////---------------------------------------------------?


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

export default class order_list {

  static match_data = async (user_id: string) => {
    return {
      $match: {
        user_id: new Types.ObjectId(user_id),
      },
    };
  };




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

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


  //..................

  // static lookup_store1 = async () => {
  //   try {
  //     return {
  //       $lookup: {
  //         from: "stores",
  //         let: { store_id: "$_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 lookup_products = async () => {
    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",
      },
    };
  };
  //,,,,,,,,,,,,,,,,,,,,,
  static lookup_products1 = async () => {
    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",
      },
    };
  };

  static lookup_reviewed_products = async (user_id: any) => {
    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: {
                      $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",
      },
    };
  };

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

  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 () => {
    return {
      $group: {
        _id: "$order_products._id",
        order_id: { $first: "$order_products.order_id" },
        user_id: { $first: "$user_id" },
        seller_id: { $first: "$order_products.seller_id" },
        product_id: { $first: "$products._id" },
        store_type: { $first: "$order_stores.type" },
        store_id: { $first: "$store_id" },
        store_name: { $first: "$order_stores.store_name" },
        product_image: { $first: "$products.images" },
        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" },
      },
    };
  };

  static redact_data = async (payload_data: any) => {
    let { status_type } = payload_data;
    // console.log("payload_data.......",payload_data);

    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"
    }
    // console.log("order_status.......", status_type);

    return {
      $redact: {
        $cond: {
          if: {
            $and: [
              {
                $or: [
                  { $eq: [status_type, undefined] },
                  { $eq: ["$order_status", status_type] },
                ],
              },
            ],
          },
          then: "$$KEEP",
          else: "$$PRUNE",
        },
      },
    };
  };

  static sort_data = async () => {
    return {
      $sort: {
        _id: -1
      }
    }
  }

  static set_data = async () => {
    return {
      $set: {
        is_liked: {
          $cond: {
            if: {
              $gt: [{ $size: "$comment_likes" }, 0]
            },
            then: true,
            else: false
          }
        }
      }
    }
  }

  static facet = async () => {
    return {
      $facet: {
        count: [
          {
            $count: "count"
          }
        ],
        data: [
          {
            $sort: {
              _id: -1 as 1 | -1
            }
          },
        ]
      }
    }
  }
  //..................latest_order_scenario.....................

  static match_order = async (order_id: string) => {
    return {
      $match: {
        order_id: new Types.ObjectId(order_id),
      },
    };
  };



  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,
              address_id: 1,
              driver_id: 1,
              user_id: 1,
              order_status: 1,
              delivered_at: 1,
              order_type: 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,
              seller_id: 1,
              admin_id: 1,
              type: 1,
              images: 1

            },
          },

        ],
        as: "stores",
      },
    }

  };

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



  static lookup_address = async () => {
    return {
      $lookup: {
        from: "addresses",
        let: { address_id: "$orders.address_id" },
        pipeline: [
          {
            $match: {
              $expr: {
                $eq: ["$_id", "$$address_id"],
              },
            },
          },
        ],
        as: "retrive_addresses",
      },
    }

  };

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



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

  };

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



  static lookup_vendor = async () => {
    return {
      $lookup: {
        from: "sellers",
        let: { seller_id: "$stores.seller_id" },
        pipeline: [
          {
            $match: {
              $expr: {
                $eq: ["$_id", "$$seller_id"],
              },
            },
          },
          {
            $project: {
              image: 1,
              email: 1,
              name: 1,
              phone_no: 1,
              country_code: 1
            },
          },
        ],
        as: "retrive_seller",
      },
    }

  };

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


  static lookup_admin = async () => {
    return {
      $lookup: {
        from: "admins",
        let: { seller_id: "$stores.seller_id" },
        pipeline: [
          {
            $match: {
              $expr: {
                $eq: ["$_id", "$$seller_id"],
              },
            },
          },
          {
            $project: {
              profile_pic: 1,
              email: 1,
              name: 1,
              phone_no: 1,
              country_code: 1
            },
          },
        ],
        as: "retrive_admin",
      },
    }

  };

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

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

  };

  static unwind_driver = async () => {
    return {
      $unwind: {
        path: "$retrive_driver",
        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,
                  },
                },
              ],
              as: "products",
            },
          },
          {
            $unwind: "$products",
          },
        ],
        as: "order_products",
      },
    };

  };

  static lookup_order_products_chefs = async () => {
    return {
      $lookup: {
        from: "orderproducts",
        let: { order_id: "$_id", order_type: "$orders.order_type" },
        pipeline: [
          {
            $match: {
              $expr: {
                $and: [
                  { $eq: ["$order_id", "$$order_id"], },
                  { $ne: ["$quantity", 0], },
                  { $eq: ["$$order_type", "CHEF"], }
                ]
              },
            },
          },
          {
            $lookup: {
              from: "menus",
              let: { menu_id: "$menu_id" },
              pipeline: [
                {
                  $match: {
                    $expr: {
                      $eq: ["$_id", "$$menu_id"],
                    },
                  },

                }
              ],
              as: "menu_detail",
            },
          },
          {
            $unwind: "$menu_detail",
          },
        ],
        as: "order_products_chefs",
      },
    };
  };


  static group_data1 = async () => {
    return {
      $group: {
        _id: "$_id",
        order_id: { $first: "$_id" },
        store_type: { $first: "$stores.type" },
        store_id: { $first: "$stores._id" },
        store_name: { $first: "$stores.store_name" },
        images: { $first: "$stores.images" },
        order_status: { $first: "$orders.order_status" },
        delivered_at: { $first: "$orders.delivered_at" },
        total_price: { $first: "$orders.total_prices" },
        order_products: { $first: "$order_products" },
        order_products_chefs:{$first: "$order_products_chefs"},
        address: { $first: "$retrive_addresses" },
        users: { $first: "$retrive_users" },
        vendor: { $first: "$retrive_seller" },
        admin: { $first: "$retrive_admin" },
        driver: { $first: "$retrive_driver" },


      },
    };
  }

}
