import CONSTANTS from "../../config/constants";
import log from "../../config/log";
import complaintDB from "../../models/complaint.model";
import propertyDB from "../../models/property.model";
import staffDB from "../../models/staff.model";
import propertiesTypes from "../../schemas/property.schema";

export const complaintsForClient = async (
  filter: string,
  status: string,
  pageNum: number,
  limit: number,
  clientId: number,
  filterVal: any
) => {
  let complaints: any = [];
  if (filter === "S" && filterVal && filterVal !== "" && filterVal !== " ") {
    //Staff
    if (status === "P") {
      complaints = await complaintDB.getComplaintsByStaffStatusForClient({
        clientId,
        status: CONSTANTS.COMPLAINT_STATUS.PENDING,
        staffs: filterVal,
        pageNum: Number(pageNum),
        limit,
      });
    } else if (status === "R") {
      complaints = await complaintDB.getComplaintsByStaffStatusForClient({
        clientId,
        status: CONSTANTS.COMPLAINT_STATUS.RESOLVED,
        staffs: filterVal,
        pageNum: Number(pageNum),
        limit,
      });
    } else if (status === "A") {
      complaints = await complaintDB.getComplaintsByStaffStatusForClient({
        clientId,
        status: CONSTANTS.COMPLAINT_STATUS.ASSIGNED,
        staffs: filterVal,
        pageNum: Number(pageNum),
        limit,
      });
    } else {
      complaints = await complaintDB.getByStaffFilterAndPageForClient({
        clientId,
        staffs: filterVal,
        pageNum: Number(pageNum),
        limit,
      });
    }
  } else if (
    filter === "C" &&
    filterVal &&
    filterVal !== "" &&
    filterVal !== " "
  ) {
    //Category
    if (status === "P") {
      complaints = await complaintDB.getComplaintsByCategoryStatusForClient({
        clientId,
        status: CONSTANTS.COMPLAINT_STATUS.PENDING,
        titles: filterVal,
        pageNum: Number(pageNum),
        limit,
      });
    } else if (status === "R") {
      complaints = await complaintDB.getComplaintsByCategoryStatusForClient({
        clientId,
        status: CONSTANTS.COMPLAINT_STATUS.RESOLVED,
        titles: filterVal,
        pageNum: Number(pageNum),
        limit,
      });
    } else if (status === "A") {
      complaints = await complaintDB.getComplaintsByCategoryStatusForClient({
        clientId,
        status: CONSTANTS.COMPLAINT_STATUS.ASSIGNED,
        titles: filterVal,
        pageNum: Number(pageNum),
        limit,
      });
    } else {
      complaints = await complaintDB.getByCategoryFilterAndPageForClient({
        clientId,
        titles: filterVal,
        pageNum: Number(pageNum),
        limit,
      });
    }
  } else if (
    filter === "PR" &&
    filterVal &&
    filterVal !== "" &&
    filterVal !== " "
  ) {
    //prop complaints
    if (status === "P") {
      complaints = await complaintDB.getComplaintsByPropStatusForClient({
        clientId,
        status: CONSTANTS.COMPLAINT_STATUS.PENDING,
        propIds: filterVal,
        pageNum: Number(pageNum),
        limit,
      });
    } else if (status === "R") {
      complaints = await complaintDB.getComplaintsByPropStatusForClient({
        clientId,
        status: CONSTANTS.COMPLAINT_STATUS.RESOLVED,
        propIds: filterVal,
        pageNum: Number(pageNum),
        limit,
      });
    } else if (status === "A") {
      complaints = await complaintDB.getComplaintsByPropStatusForClient({
        clientId,
        status: CONSTANTS.COMPLAINT_STATUS.ASSIGNED,
        propIds: filterVal,
        pageNum: Number(pageNum),
        limit,
      });
    } else {
      complaints = await complaintDB.getByPropertyFilterAndPageForClient({
        clientId,
        propIds: filterVal,
        pageNum: Number(pageNum),
        limit,
      });
    }
  } else if (status === "P") {
    complaints = await complaintDB.getPendingComplaintsByPage({
      clientId,
      pageNum: Number(pageNum),
      limit,
    });
  } else if (status === "R") {
    complaints = await complaintDB.getResolvedComplaintsByPage({
      clientId,
      pageNum: Number(pageNum),
      limit,
    });
  } else if (status === "A") {
    complaints = await complaintDB.getAssignedComplaintsByPage({
      clientId,
      pageNum: Number(pageNum),
      limit,
    });
  } else {
    complaints = await complaintDB.getByClientIdAndPage({
      clientId,
      pageNum: Number(pageNum),
      limit,
    });
  }

  return complaints;
};

export const complaintsForAdmin = async (
  filter: string,
  status: string,
  pageNum: number,
  limit: number,
  clientId: number,
  staffId: number,
  filterVal: any
) => {
  let complaints: any = [];

  const staffLinkedProps = await propertyDB.getPropsByStaffId({
    staffId,
  });

  if (staffLinkedProps) {
    const propertiesIds = staffLinkedProps
      .map((prop: propertiesTypes) => prop.id)
      .join(",");

    if (
      filter === "S" &&
      filterVal &&
      filterVal !== "" &&
      filterVal !== " "
    ) {
      //Staff
      if (status === "P") {
        complaints =
          await complaintDB.getByStaffFilterAndStatusAndPageForAdmin({
            clientId,
            staffs: filterVal,
            pageNum: Number(pageNum),
            limit,
            propertiesIds,
            status: CONSTANTS.COMPLAINT_STATUS.PENDING,
          });
      } else if (status === "R") {
        complaints = await complaintDB.getByStaffFilterAndStatusAndPageForAdmin(
          {
            clientId,
            staffs: filterVal,
            pageNum: Number(pageNum),
            limit,
            propertiesIds,
            status: CONSTANTS.COMPLAINT_STATUS.RESOLVED,
          }
        );
      } else if (status === "A") {
        complaints = await complaintDB.getByStaffFilterAndStatusAndPageForAdmin(
          {
            clientId,
            staffs: filterVal,
            pageNum: Number(pageNum),
            limit,
            propertiesIds,
            status: CONSTANTS.COMPLAINT_STATUS.ASSIGNED,
          }
        );
      } else {
        complaints = await complaintDB.getByStaffFilterAndPageForAdmin(
          {
            clientId,
            staffs: filterVal,
            pageNum: Number(pageNum),
            limit,
            propertiesIds,
          }
        );
      }
    } else if (
      filter === "C" &&
      filterVal &&
      filterVal !== "" &&
      filterVal !== " "
    ) {
      //Category
      if (status === "P") {
        complaints =
          await complaintDB.getByCategoryFilterAndStatusAndPageForAdmin({
            clientId,
            titles: filterVal,
            pageNum: Number(pageNum),
            limit,
            propertiesIds,
            status: CONSTANTS.COMPLAINT_STATUS.PENDING,
          });
      } else if (status === "R") {
        complaints =
          await complaintDB.getByCategoryFilterAndStatusAndPageForAdmin({
            clientId,
            titles: filterVal,
            pageNum: Number(pageNum),
            limit,
            propertiesIds,
            status: CONSTANTS.COMPLAINT_STATUS.RESOLVED,
          });
      } else if (status === "A") {
        complaints =
          await complaintDB.getByCategoryFilterAndStatusAndPageForAdmin({
            clientId,
            titles: filterVal,
            pageNum: Number(pageNum),
            limit,
            propertiesIds,
            status: CONSTANTS.COMPLAINT_STATUS.ASSIGNED,
          });
      } else {
        complaints = await complaintDB.getByCategoryFilterAndPageForAdmin({
          clientId,
          titles: filterVal,
          pageNum: Number(pageNum),
          limit,
          propertiesIds,
        });
      }
    } else if (
      filter === "PR" &&
      filterVal &&
      filterVal !== "" &&
      filterVal !== " "
    ) {
      //prop complaints
      if (status === "P") {
        complaints =
          await complaintDB.getByPropertyFilterAndStatusAndPageForAdmin({
            clientId,
            propIds: filterVal,
            pageNum: Number(pageNum),
            limit,
            propertiesIds,
            status: CONSTANTS.COMPLAINT_STATUS.PENDING,
          });
      } else if (status === "R") {
        complaints =
          await complaintDB.getByPropertyFilterAndStatusAndPageForAdmin({
            clientId,
            propIds: filterVal,
            pageNum: Number(pageNum),
            limit,
            propertiesIds,
            status: CONSTANTS.COMPLAINT_STATUS.RESOLVED,
          });
      } else if (status === "A") {
        complaints =
          await complaintDB.getByPropertyFilterAndStatusAndPageForAdmin({
            clientId,
            propIds: filterVal,
            pageNum: Number(pageNum),
            limit,
            propertiesIds,
            status: CONSTANTS.COMPLAINT_STATUS.ASSIGNED,
          });
      } else {
        complaints = await complaintDB.getByPropertyFilterAndPageForAdmin({
          clientId,
          propIds: filterVal,
          pageNum: Number(pageNum),
          limit,
          propertiesIds,
        });
      }
    } else if (status === "P") {
      complaints = await complaintDB.getAdminPendingComplaintsByPage({
        clientId,
        pageNum: Number(pageNum),
        limit,
        propertiesIds,
      });
    } else if (status === "R") {
      complaints = await complaintDB.getAdminResolvedComplaintsByPage({
        clientId,
        pageNum: Number(pageNum),
        limit,
        propertiesIds,
      });
    } else if (status === "A") {
      complaints = await complaintDB.getAdminAssignedComplaintsByPage({
        clientId,
        pageNum: Number(pageNum),
        limit,
        propertiesIds,
      });
    } else {
      complaints = await complaintDB.getByAdminIdAndPage({
        clientId,
        pageNum: Number(pageNum),
        limit,
        propertiesIds,
      });
    }
  }

  return complaints;
};

export const complaintsForStaff = async (
  filter: string,
  status: string,
  pageNum: number,
  limit: number,
  staffId: number,
  filterVal: any
) => {
  let complaints: any = [];

  if (
    filter === "C" &&
    filterVal &&
    filterVal !== "" &&
    filterVal !== " "
  ) {
    //Category
    if (status === "P") {
      complaints = await complaintDB.getByCategoryFilterAndStatusAndPageForStaff({
        assignedTo: staffId,
        titles: filterVal,
        pageNum: Number(pageNum),
        limit,
        status: CONSTANTS.COMPLAINT_STATUS.PENDING,
      });
    } else if (status === "R") {
      complaints = await complaintDB.getByCategoryFilterAndStatusAndPageForStaff({
        assignedTo: staffId,
        titles: filterVal,
        pageNum: Number(pageNum),
        limit,
        status: CONSTANTS.COMPLAINT_STATUS.RESOLVED,
      });
    } else if (status === "A") {
      complaints = await complaintDB.getByCategoryFilterAndStatusAndPageForStaff({
        assignedTo: staffId,
        titles: filterVal,
        pageNum: Number(pageNum),
        limit,
        status: CONSTANTS.COMPLAINT_STATUS.ASSIGNED,
      });
    } else {
      complaints = await complaintDB.getByCategoryFilterAndPageForStaff({
        assignedTo: staffId,
        titles: filterVal,
        pageNum: Number(pageNum),
        limit,
      });
    }
  } else if (
    filter === "PR" &&
    filterVal &&
    filterVal !== "" &&
    filterVal !== " "
  ) {
    //prop complaints
    if (status === "P") {
      complaints = await complaintDB.getByPropertyFilterAndStatusAndPageForStaff({
        assignedTo: staffId,
        propIds: filterVal,
        pageNum: Number(pageNum),
        limit,
        status: CONSTANTS.COMPLAINT_STATUS.PENDING,
      });
    }
    else if (status === "R") {
      complaints = await complaintDB.getByPropertyFilterAndStatusAndPageForStaff({
        assignedTo: staffId,
        propIds: filterVal,
        pageNum: Number(pageNum),
        limit,
        status: CONSTANTS.COMPLAINT_STATUS.RESOLVED,
      });
    } else if (status === "A") {
      complaints = await complaintDB.getByPropertyFilterAndStatusAndPageForStaff({
        assignedTo: staffId,
        propIds: filterVal,
        pageNum: Number(pageNum),
        limit,
        status: CONSTANTS.COMPLAINT_STATUS.ASSIGNED,
      });
    } else {
      complaints = await complaintDB.getByPropertyFilterAndPageForStaff({
        assignedTo: staffId,
        propIds: filterVal,
        pageNum: Number(pageNum),
        limit,
      });
    }
  } else if (status === "P") {
    complaints = await complaintDB.getStaffPendingComplaintsByPage({
      assignedTo: staffId,
      pageNum: Number(pageNum),
      limit,
    });
  } else if (status === "R") {
    complaints = await complaintDB.getStaffResolvedComplaintsByPage({
      assignedTo: staffId,
      pageNum: Number(pageNum),
      limit,
    });
  } else if (status === "A") {
    complaints = await complaintDB.getStaffAssignedComplaintsByPage({
      assignedTo: staffId,
      pageNum: Number(pageNum),
      limit,
    });
  } else {
    complaints = await complaintDB.getByStaffIdAndPage({
      assignedTo: staffId,
      pageNum: Number(pageNum),
      limit,
    });
  }

  return complaints;
};

export const propComplaintsForStaff = async (
  filter: string,
  pageNum: number,
  limit: number,
  staffId: number
) => {
  let complaints: any = [];

  if (filter === "R") {
    complaints = await complaintDB.getStaffResolvedPropComplaintsByPage({
      assignedTo: staffId,
      pageNum: Number(pageNum),
      limit,
    });
  } else if (filter === "A") {
    complaints = await complaintDB.getStaffAssignedPropComplaintsByPage({
      assignedTo: staffId,
      pageNum: Number(pageNum),
      limit,
    });
  } else {
    complaints = await complaintDB.getPropComplaintsByStaffIdAndPage({
      assignedTo: staffId,
      pageNum: Number(pageNum),
      limit,
    });
  }

  return complaints;
};

export const propComplaintsForClient = async (
  filter: string,
  pageNum: number,
  limit: number,
  clientId: number
) => {
  let complaints: any = [];
  if (filter === "P") {
    complaints = await complaintDB.getForPropPendingComplaintsByPage({
      clientId,
      pageNum: Number(pageNum),
      limit,
    });
  } else if (filter === "R") {
    complaints = await complaintDB.getForPropResolvedComplaintsByPage({
      clientId,
      pageNum: Number(pageNum),
      limit,
    });
  } else if (filter === "A") {
    complaints = await complaintDB.getForPropAssignedComplaintsByPage({
      clientId,
      pageNum: Number(pageNum),
      limit,
    });
  } else {
    complaints = await complaintDB.getForPropByClientIdAndPage({
      clientId,
      pageNum: Number(pageNum),
      limit,
    });
  }

  return complaints;
};

export const propComplaintsForAdmin = async (
  filter: string,
  pageNum: number,
  limit: number,
  clientId: number,
  staffId: number
) => {
  let complaints: any = [];

  const staffLinkedProps = await propertyDB.getPropsByStaffId({
    staffId,
  });

  if (staffLinkedProps) {
    const propertiesIds = staffLinkedProps
      .map((prop: propertiesTypes) => prop.id)
      .join(",");

    if (filter === "P") {
      complaints = await complaintDB.getForPropAdminPendingComplaintsByPage({
        clientId,
        pageNum: Number(pageNum),
        limit,
        propertiesIds,
      });
    } else if (filter === "R") {
      complaints = await complaintDB.getForPropAdminResolvedComplaintsByPage({
        clientId,
        pageNum: Number(pageNum),
        limit,
        propertiesIds,
      });
    } else if (filter === "A") {
      complaints = await complaintDB.getForPropAdminAssignedComplaintsByPage({
        clientId,
        pageNum: Number(pageNum),
        limit,
        propertiesIds,
      });
    } else {
      complaints = await complaintDB.getForPropByAdminIdAndPage({
        clientId,
        pageNum: Number(pageNum),
        limit,
        propertiesIds,
      });
    }
  }

  return complaints;
};

export const complaintStaffCounts = async (
  clientId: number,
  staffId: number,
  filter: string,
  filterVal: any
) => {
  let complaintCounts = {
    new: 0,
    assigned: 0,
    closed: 0,
  };

  const staff = await staffDB.getById({
    id: staffId,
  });

  const staffLinkedProps = await propertyDB.getPropsByStaffId({
    staffId,
  });

  if (staffLinkedProps) {
    const propertiesIds = staffLinkedProps
      .map((prop: propertiesTypes) => prop.id)
      .join(",");
    if (
      staff.role === CONSTANTS.STAFF_ROLES.ADMIN ||
      staff.role === CONSTANTS.STAFF_ROLES.WARDEN ||
      staff.role === CONSTANTS.STAFF_ROLES.BACK_OFFICE
    ) {
      if (filter === "PR") {
        complaintCounts = await complaintDB.getComplaintCountsByPropForStaff({
          clientId,
          propertiesIds,
          propIds: filterVal,
        });
      } else if (filter === "S") {
        complaintCounts = await complaintDB.getComplaintCountsByStaffForStaff({
          clientId,
          propertiesIds,
          staffs: filterVal,
        });
      } else if (filter === "C") {
        complaintCounts =
          await complaintDB.getComplaintCountsByCategoryForStaff({
            clientId,
            propertiesIds,
            titles: filterVal,
          });
      } else {
        complaintCounts = await complaintDB.getComplaintCountsForStaff({
          clientId,
          propertiesIds,
        });
      }
    } else {
      if (filter === "PR") {
        complaintCounts =
          await complaintDB.getComplaintCountsByPropForRegularStaff({
            clientId,
            assignedTo: staffId,
            propertiesIds,
            propIds: filterVal,
          });
      } else if (filter === "C") {
        complaintCounts =
          await complaintDB.getComplaintCountsByCategoryForRegularStaff({
            clientId,
            assignedTo: staffId,
            propertiesIds,
            titles: filterVal,
          });
      } else {
        complaintCounts = await complaintDB.getComplaintCountsForRegularStaff({
          clientId,
          assignedTo: staffId,
          propertiesIds,
        });
      }
    }
  }

  return complaintCounts;
};

export const propComplaintStaffCounts = async (
  clientId: number,
  staffId: number,
  filter: string,
  filterVal: any
) => {
  let complaintCounts = {
    new: 0,
    assigned: 0,
    closed: 0,
  };

  const staff = await staffDB.getById({
    id: staffId,
  });

  const staffLinkedProps = await propertyDB.getPropsByStaffId({
    staffId,
  });

  if (staffLinkedProps) {
    const propertiesIds = staffLinkedProps
      .map((prop: propertiesTypes) => prop.id)
      .join(",");
    if (
      staff.role === CONSTANTS.STAFF_ROLES.ADMIN ||
      // staff.role === CONSTANTS.STAFF_ROLES.WARDEN || 
      staff.role === CONSTANTS.STAFF_ROLES.BACK_OFFICE ||
      staff.role === CONSTANTS.STAFF_ROLES.FINANCE_ADMIN ||
      staff.role === CONSTANTS.STAFF_ROLES.HELPDESK
    ) {
      if (filter === "PR") {
        complaintCounts = await complaintDB.getComplaintCountsByPropForStaff({
          clientId,
          propertiesIds,
          propIds: filterVal,
        });
      } else if (filter === "S") {
        complaintCounts = await complaintDB.getComplaintCountsByStaffForStaff({
          clientId,
          propertiesIds,
          staffs: filterVal,
        });
      } else if (filter === "C") {
        complaintCounts =
          await complaintDB.getComplaintCountsByCategoryForStaff({
            clientId,
            propertiesIds,
            titles: filterVal,
          });
      } else {
        complaintCounts = await complaintDB.getPropComplaintCountsForStaff({
          clientId,
          propertiesIds,
        });
      }
    } else {
      if (filter === "PR") {
        complaintCounts =
          await complaintDB.getComplaintCountsByPropForRegularStaff({
            clientId,
            propertiesIds,
            propIds: filterVal,
          });
      } else if (filter === "C") {
        complaintCounts =
          await complaintDB.getComplaintCountsByCategoryForRegularStaff({
            clientId,
            propertiesIds,
            titles: filterVal,
          });
      } else {
        complaintCounts = await complaintDB.getPropComplaintCountsForRegularStaff({
          clientId,
          assignedTo: staffId,
          propertiesIds,
        });
      }
    }
  }

  return complaintCounts;
};

export const complaintStaffCountsForYearMonth = async (
  clientId: number,
  staffId: number,
  year: any,
  month: any
) => {
  let complaintCounts = {
    new: 0,
    assigned: 0,
    closed: 0,
  };

  complaintCounts = await complaintDB.getComplaintCountsByStaffIdForYearMonth({
    clientId,
    assignedTo: staffId,
    year,
    month,
  });
  // const staffLinkedProps = await propertyDB.getPropsByStaffId({
  //   staffId,
  // });

  // if (staffLinkedProps) {
  //   const propertiesIds = staffLinkedProps
  //     .map((prop: propertiesTypes) => prop.id)
  //     .join(",");
  //   complaintCounts = await complaintDB.getComplaintCountsForStaffForYearMonth({
  //     clientId,
  //     propertiesIds,
  //     year,
  //     month,
  //   });
  // }

  return complaintCounts;
};

export const complaintsForProperty = async (
  propId: number,
  filter: string,
  startDate: string,
  endDate: string,
  filterVal: number,
  titleFilter: any,
) => {
  let complaints: any = [];
  // if (filter === "P") {
  //   complaints = await complaintDB.getByPropIdandDateRangeandStatus({
  //     propId,
  //     status: CONSTANTS.COMPLAINT_STATUS.PENDING,
  //     startDate,
  //     endDate,
  //   });
  // } else if (filter === "R") {
  //   complaints = await complaintDB.getByPropIdandDateRangeandStatus({
  //     propId,
  //     status: CONSTANTS.COMPLAINT_STATUS.RESOLVED,
  //     startDate,
  //     endDate,
  //   });
  // } else if (filter === "A") {
  //   complaints = await complaintDB.getByPropIdandDateRangeandStatus({
  //     propId,
  //     status: CONSTANTS.COMPLAINT_STATUS.ASSIGNED,
  //     startDate,
  //     endDate,
  //   });
  // } else if (Number(filterVal) && Number(filterVal) !== 0) {
  //   complaints = await complaintDB.getByPropIdandDateRangeandAssigned({
  //     propId,
  //     status: CONSTANTS.COMPLAINT_STATUS.ASSIGNED,
  //     startDate,
  //     endDate,
  //     assignedTo: filterVal,
  //   });
  // } else {
  //   complaints = await complaintDB.getByPropIdandDateRange({
  //     propId,
  //     startDate,
  //     endDate,
  //   });
  // }

  let status = filter === "P" ? CONSTANTS.COMPLAINT_STATUS.PENDING :
    filter === "R" ? CONSTANTS.COMPLAINT_STATUS.RESOLVED :
      filter === "A" ? CONSTANTS.COMPLAINT_STATUS.ASSIGNED : null;

    complaints = await complaintDB.getByPropIdandDateRangeAndFilter({
      propId,
      startDate,
      endDate,
      status,
      assignedTo: filterVal,
      title: titleFilter || [],
    });

  return complaints;
};

export const complaintsForClientWeb = async (
  clientId: number,
  filter: string,
  startDate: string,
  endDate: string,
  filterVal: number,
  titleFilter: any,
) => {
  let complaints: any = [];
  // if (filter === "P") {
  //   complaints = await complaintDB.getByClientIdandDateRangeandStatus({
  //     clientId,
  //     status: CONSTANTS.COMPLAINT_STATUS.PENDING,
  //     startDate,
  //     endDate,
  //   });
  // } else if (filter === "R") {
  //   complaints = await complaintDB.getByClientIdandDateRangeandStatus({
  //     clientId,
  //     status: CONSTANTS.COMPLAINT_STATUS.RESOLVED,
  //     startDate,
  //     endDate,
  //   });
  // } else if (filter === "A") {
  //   complaints = await complaintDB.getByClientIdandDateRangeandStatus({
  //     clientId,
  //     status: CONSTANTS.COMPLAINT_STATUS.ASSIGNED,
  //     startDate,
  //     endDate,
  //   });
  // } else if (Number(filterVal) && Number(filterVal) !== 0) {
  //   complaints = await complaintDB.getByClientIdandDateRangeandAssigned({
  //     clientId,
  //     status: CONSTANTS.COMPLAINT_STATUS.ASSIGNED,
  //     startDate,
  //     endDate,
  //     assignedTo: filterVal,
  //   });
  // } else {
  //   complaints = await complaintDB.getByClientIdandDateRangeAndFilters({
  //     clientId,
  //     startDate,
  //     endDate,
  //     title: titleFilter,
  //   });
  // }

  let status = filter === "P" ? CONSTANTS.COMPLAINT_STATUS.PENDING 
    : filter === "R" ? CONSTANTS.COMPLAINT_STATUS.RESOLVED 
    : filter === "A" ? CONSTANTS.COMPLAINT_STATUS.ASSIGNED 
    : null;

  complaints = await complaintDB.getByClientIdandDateRangeAndFilters({
    clientId,
    startDate,
    endDate,
    assignedTo: filterVal,
    status: status,
    title: titleFilter || [],
  });

  return complaints;
};

export const propComplaintsForClientWeb = async (
  clientId: number,
  propId: number,
  startDate: string,
  endDate: string,
  filterVal: number,
  titleFilter: any,
) => {
  let complaints: any = [];
  let complaintCount: any = {
    new: 0,
    assigned: 0,
    closed: 0,
  };

  // if (Number(filterVal) && Number(filterVal) !== 0 ) {
  //   //Staff assigned filter
  //   complaints = await complaintDB.getForPropByClientIdAndDateRangeAndAssigned({
  //     clientId,
  //     startDate,
  //     endDate,
  //     assignedTo: filterVal,
  //   });
  //   complaintCount = await complaintDB.getPropComplaintCountsForClientByDateRangeAndAssigned({
  //     clientId,
  //     startDate,
  //     endDate,
  //     assignedTo: filterVal
  //   });
  // } else 
  if (Number(propId) && Number(propId) !== 0) {
    complaints = await complaintDB.getForPropByPropIdAndDateRangeAndFilters({
      clientId,
      propId,
      startDate,
      endDate,
      assignedTo: Number(filterVal) ? Number(filterVal) : null,
      title: titleFilter || null,
    });
    complaintCount = await complaintDB.getPropComplaintCountsForPropertyByDateRangeAndFilters({
      clientId,
      propId,
      startDate,
      endDate,
      assignedTo: Number(filterVal) ? Number(filterVal) : null,
      title: titleFilter || null,
    });
  } else {
    complaints = await complaintDB.getForPropByClientIdAndDateRangeAndFilters({
      clientId,
      startDate,
      endDate,
      assignedTo: Number(filterVal) ? Number(filterVal) : null,
      title: titleFilter || null,
    });
    complaintCount = await complaintDB.getPropComplaintCountsForClientByDateRangeAndFilters({
      clientId,
      startDate,
      endDate,
      assignedTo: Number(filterVal) ? Number(filterVal) : null,
      title: titleFilter || null,
    });
  }

  return {complaints, complaintCount};
};
