import Ticket from "../../models/Ticket";
import Contact from "../../models/Contact";
import SchedulingNotify from "../../models/SchedulingNotify";
import { Op, where, Sequelize } from "sequelize";
import AppError from "../../errors/AppError";
import StatusChatEnd from "../../models/StatusChatEnd"

const ListSchedulingNotifyContactService = async (
  contactNumber: string = "",
  startDate: string = "",
  endDate: string = ""
): Promise<SchedulingNotify[]> => {
  let where_clause = {};
  let where_clause_notify = {};

  let nameNumber = {
    [Op.or]: [
      {
        name: Sequelize.where(
          Sequelize.fn("LOWER", Sequelize.col("name")),
          "LIKE",
          `%${contactNumber.toLowerCase().trim()}%`
        )
      },
      { number: { [Op.like]: `%${contactNumber.toLowerCase().trim()}%` } }
    ]
  };

  if (
    contactNumber.trim().length > 0 &&
    startDate.trim().length > 0 &&
    endDate.trim().length > 0
  ) {
    where_clause = nameNumber;

    where_clause_notify = {
      schedulingTime: {
        [Op.gte]: startDate + " 00:00:00.000000",
        [Op.lte]: endDate + " 23:59:59.999999"
      }
    };
  } else if (
    contactNumber.trim().length == 0 &&
    startDate.trim().length > 0 &&
    endDate.trim().length > 0
  ) {
    where_clause = {};

    where_clause_notify = {
      schedulingDate: {
        [Op.gte]: startDate + " 00:00:00.000000",
        [Op.lte]: endDate + " 23:59:59.999999"
      }
    };
  } else if (
    contactNumber.trim().length == 0 &&
    startDate.trim().length > 0 &&
    endDate.trim().length == 0
  ) {
    where_clause = {};

    where_clause_notify = {
      schedulingDate: {
        [Op.gte]: startDate + " 00:00:00.000000",
        [Op.lte]: startDate + " 23:59:59.999999"
      }
    };
  } else if (
    contactNumber.trim().length == 0 &&
    startDate.trim().length == 0 &&
    endDate.trim().length > 0
  ) {
    where_clause = {};

    where_clause_notify = {
      schedulingDate: {
        [Op.gte]: endDate + " 00:00:00.000000",
        [Op.lte]: endDate + " 23:59:59.999999"
      }
    };
  } else if (
    contactNumber.trim().length > 0 &&
    startDate.trim().length > 0 &&
    endDate.trim().length == 0
  ) {
    where_clause = nameNumber;

    where_clause_notify = {
      schedulingDate: {
        [Op.gte]: startDate + " 00:00:00.000000",
        [Op.lte]: startDate + " 23:59:59.999999"
      }
    };
  } else if (
    contactNumber.trim().length > 0 &&
    startDate.trim().length == 0 &&
    endDate.trim().length > 0
  ) {
    where_clause = nameNumber;

    where_clause_notify = {
      schedulingDate: {
        [Op.gte]: endDate + " 00:00:00.000000",
        [Op.lte]: endDate + " 23:59:59.999999"
      }
    };
  } else if (contactNumber.trim().length > 0) {
    where_clause = nameNumber;
  }

  const ticket = await SchedulingNotify.findAll({
    raw: true,
    where: where_clause_notify,

    attributes: [
      "id",
      "ticketId",
      "statusChatEndId",
      [
        Sequelize.fn(
          "DATE_FORMAT",
          Sequelize.col("schedulingDate"),
          "%d/%m/%Y %H:%i:%s"
        ),
        "schedulingDate"
      ],
      [
        Sequelize.fn(
          "DATE_FORMAT",
          Sequelize.col("schedulingTime"),
          "%d/%m/%Y %H:%i:%s"
        ),
        "schedulingTime"
      ],
      "message"
    ],

    include: [
      {
        model: Ticket,
        required: true,
        attributes: [],
        include: [
          {
            model: Contact,
            where: where_clause,
            attributes: ["name", "number", "profilePicUrl"]
          }
        ]
      },
      {
        model: StatusChatEnd,
        required: true,  
      }
    ],

    order: [["id", "DESC"]]
  });

  if (!ticket) {
    throw new AppError("ERR_NO_TICKET_FOUND", 404);
  }

  return ticket;
};

export default ListSchedulingNotifyContactService;
