const History = require("../../models/history.model");

//import model
const User = require("../../models/user.model");

//mongoose
const mongoose = require("mongoose");

//get coin history of particular user
exports.retrieveUserCoinTransactions = async (req, res) => {
  try {
    if (!req.query.startDate || !req.query.endDate || !req.query.userId) {
      return res.status(200).json({ status: false, message: "Oops ! Invalid details!" });
    }

    const userId = new mongoose.Types.ObjectId(req.query.userId);
    const startDate = req?.query?.startDate || "All";
    const endDate = req?.query?.endDate || "All";

    let dateFilterQuery = {};
    if (startDate !== "All" && endDate !== "All") {
      const formatStartDate = new Date(startDate);
      const formatEndDate = new Date(endDate);
      formatEndDate.setHours(23, 59, 59, 999);

      dateFilterQuery = {
        createdAt: {
          $gte: formatStartDate,
          $lte: formatEndDate,
        },
      };
    }

    const [user, coinHistory] = await Promise.all([
      User.findOne({ _id: userId }).select("_id").lean(),
      History.find({
        userId: userId,
        ...dateFilterQuery,
        coin: { $ne: 0 },
        $and: [
          {
            $or: [{ type: { $ne: 5 } }, { type: 5, price: { $exists: true, $ne: 0 } }],
          },
        ],
      })
        .sort({ createdAt: -1 })
        .lean(),
    ]);

    if (!user) {
      return res.status(200).json({ status: false, message: "User does not found." });
    }

    return res.status(200).json({
      status: true,
      message: "Retrive coin history for that user.",
      data: coinHistory,
    });
  } catch (error) {
    console.log(error);
    return res.status(500).json({ status: false, error: error.message || "Internal Server Error" });
  }
};

//get referral history of particular user
exports.retrieveUserReferralRecords = async (req, res) => {
  try {
    if (!req.query.startDate || !req.query.endDate || !req.query.userId) {
      return res.status(200).json({ status: false, message: "Oops ! Invalid details!" });
    }

    const userId = new mongoose.Types.ObjectId(req.query.userId);
    const startDate = req?.query?.startDate || "All";
    const endDate = req?.query?.endDate || "All";

    let dateFilterQuery = {};
    if (startDate !== "All" && endDate !== "All") {
      const formatStartDate = new Date(startDate);
      const formatEndDate = new Date(endDate);
      formatEndDate.setHours(23, 59, 59, 999);

      dateFilterQuery = {
        createdAt: {
          $gte: formatStartDate,
          $lte: formatEndDate,
        },
      };
    }
    //console.log("dateFilterQuery:   ", dateFilterQuery);

    const [user, referralHistory] = await Promise.all([
      User.findOne({ _id: userId }).select("_id").lean(),
      History.find({ userId: userId, type: 4, ...dateFilterQuery, coin: { $ne: 0 } })
        .populate("userId", "name username")
        .sort({ createdAt: -1 })
        .lean(),
    ]);

    if (!user) {
      return res.status(200).json({ status: false, message: "User does not found." });
    }

    return res.status(200).json({
      status: true,
      message: "Retrive Refferal history for that user.",
      data: referralHistory,
    });
  } catch (error) {
    console.log(error);
    return res.status(500).json({ status: false, error: error.message || "Internal Server Error" });
  }
};
