//import { log } from "console";
import occupancyDB from "../../models/occupancy.model";
import propertyDB from "../../models/property.model";
import propertiesTypes from "../../schemas/property.schema";
import log from "../../config/log";
import CONSTANTS from "../../config/constants";

export const tenantsOfSelectedProperty = async (
  clientId: number,
  propId: number,
  pageNum: number,
  filter: string,
  searchVal: string,
  platform: string,
  limit: number
) => {
  let tenantCount = 0;
  let occupancies = [];

  const property = await propertyDB.getById({
    id: propId,
  });

  if (searchVal && searchVal !== "undefined" && filter !== "E") {
    occupancies = await occupancyDB.getSearchResultByClientIdAndPropId({
      clientId,
      propId,
      pageNum: Number(pageNum),
      limit,
      searchVal,
    });

    tenantCount = await occupancyDB.getSearchResultCountByClientIdAndPropId({
      clientId,
      propId,
      searchVal,
    });
  } else if (filter === "U") {
    occupancies = await occupancyDB.getUnpaidTenantsByClientIdAndPropId({
      clientId,
      propId,
      pageNum: Number(pageNum),
      limit,
    });

    tenantCount = await occupancyDB.getUnpaidTenantsCountByClientIdAndPropId({
      clientId,
      propId,
    });
  } else if (filter === "M") {
    occupancies = await occupancyDB.getMovingOutTenantsByClientIdAndPropId({
      clientId,
      propId,
      pageNum: Number(pageNum),
      limit,
    });

    tenantCount = await occupancyDB.getMovingOutTenantsCountByClientIdAndPropId(
      {
        clientId,
        propId,
      }
    );
  } else if (filter === "R") {
    //Reserved Tenants
    occupancies = await occupancyDB.getReservedTenantsByClientIdAndPropId({
      clientId,
      propId,
      pageNum: Number(pageNum),
      limit,
    });

    tenantCount = await occupancyDB.getReservedTenantsCountByClientIdAndPropId({
      clientId,
      propId,
    });
  } else if (filter === "NR") {
    //All Tenants excluding Reserved Ones
    occupancies =
      await occupancyDB.getWithoutReservedTenantsByClientIdAndPropId({
        clientId,
        propId,
        pageNum: Number(pageNum),
        limit,
      });

    tenantCount =
      await occupancyDB.getWithoutReservedTenantsCountByClientIdAndPropId({
        clientId,
        propId,
      });
  } else if (filter === "V") {
    //verified tenants
    occupancies = await occupancyDB.getVerifiedTenantsByClientIdAndPropId({
      clientId,
      propId,
      pageNum: Number(pageNum),
      limit,
    });

    tenantCount = await occupancyDB.getVerifiedTenantsCountByClientIdAndPropId({
      clientId,
      propId,
    });
  } else if (filter === "UV") {
    //Unverified tenants
    occupancies = await occupancyDB.getUnverifiedTenantsByClientIdAndPropId({
      clientId,
      propId,
      pageNum: Number(pageNum),
      limit,
    });

    tenantCount =
      await occupancyDB.getUnverifiedTenantsCountByClientIdAndPropId({
        clientId,
        propId,
      });
  } else if (filter === "NT") {
    //New Tenants
    occupancies = await occupancyDB.getNewTenantsByClientIdAndPropId({
      clientId,
      propId,
      pageNum: Number(pageNum),
      limit,
    });

    tenantCount = await occupancyDB.getNewTenantsCountByClientIdAndPropId({
      clientId,
      propId,
    });
  } else if (filter === "E") {
    if (searchVal && searchVal !== "undefined") {
      occupancies = await occupancyDB.getEvictedTenantsByClientIdAndPropIdWithSearch({
        clientId,
        propId,
        pageNum: Number(pageNum),
        limit,
        searchVal,
      });

      tenantCount = await occupancyDB.getEvictedTenantsCountByClientIdAndPropIdWithSearch({
        clientId,
        propId,
        searchVal,
      });
    } else {
      occupancies = await occupancyDB.getEvictedTenantsByClientIdAndPropId({
        clientId,
        propId,
        pageNum: Number(pageNum),
        limit,
      });

      tenantCount = await occupancyDB.getEvictedTenantsCountByClientIdAndPropId({
        clientId,
        propId,
      });
    }
  } else if (platform === "I" || filter === "I") { //App installed
    occupancies = await occupancyDB.getAppInstalledTenantsByClientIdAndPropId({
      clientId,
      propId,
      pageNum: Number(pageNum),
      limit,
    });

    tenantCount =
      await occupancyDB.getAppInstalledTenantsCountByClientIdAndPropId({
        clientId,
        propId,
      });
  } else if (platform === "NI" || filter === "NI") { //App not installed
    occupancies =
      await occupancyDB.getAppNotInstalledTenantsByClientIdAndPropId({
        clientId,
        propId,
        pageNum: Number(pageNum),
        limit,
      });

    tenantCount =
      await occupancyDB.getAppNotInstalledTenantsCountByClientIdAndPropId({
        clientId,
        propId,
      });
  } else if (filter === "CS") {
    //Created At sort by
    occupancies = await occupancyDB.getByClientIdAndPropIdSortByCreatedAt({
      clientId,
      propId,
      pageNum: Number(pageNum),
      limit,
    });
    tenantCount = await occupancyDB.getCountByClientIdAndPropId({
      clientId,
      propId,
    });
  } else if (filter === "RS") {
    if (property.type === CONSTANTS.PROPERTY_TYPE.FLAT) {
      occupancies = await occupancyDB.getByClientIdAndPropIdSortByRoomFlat({
        clientId,
        propId,
        pageNum: Number(pageNum),
        limit,
      });
    } else {
      occupancies = await occupancyDB.getByClientIdAndPropIdSortByRoomPg({
        clientId,
        propId,
        pageNum: Number(pageNum),
        limit,
      });
    }
    tenantCount = await occupancyDB.getCountByClientIdAndPropId({
      clientId,
      propId,
    });
  } else if (filter === "PV") {
    //Police Verification 
    occupancies = await occupancyDB.getPVCompletedTenantsForProp({
      clientId,
      pageNum: Number(pageNum),
      limit,
      propId: propId,
    });

    tenantCount = await occupancyDB.getPVCompletedTenantsCountForProp({
      clientId,
      propId: propId,
    });
  } else if (filter === "PVP") {
    //Police Verification Pending
    occupancies = await occupancyDB.getPVPendingTenantsForProp({
      clientId,
      pageNum: Number(pageNum),
      propId: propId,
      limit,
    });

    tenantCount = await occupancyDB.getPVPendingTenantsCountForProp({
      clientId,
      propId: propId,
    });
  } else if (filter === "RA") {
    //Rent Agreement done
    occupancies = await occupancyDB.getRASignedTenantsForProp({
      clientId,
      pageNum: Number(pageNum),
      limit,
      propId: propId,
    });

    tenantCount = await occupancyDB.getRASignedTenantsCountForProp({
      clientId,
      propId: propId,
    });
  } else if (filter === "RAP") {
    //Rent Argeement Pending
    occupancies = await occupancyDB.getRAPendingTenantsForProp({
      clientId,
      pageNum: Number(pageNum),
      limit,
      propId: propId,
    });

    tenantCount = await occupancyDB.getRAPendingTenantsCountForProp({
      clientId,
      propId: propId,
    });
  } else if (filter === "RB") {
    //Rental Bond done
    occupancies = await occupancyDB.getRentalBondAvailedTenantsForProp({
      clientId,
      pageNum: Number(pageNum),
      limit,
      propId: propId,
    });

    tenantCount = await occupancyDB.getRentalBondAvailedTenantsCountForProp({
      clientId,
      propId: propId,
    });
  } else if (filter === "RBN") {
    //Rental bond Not done
    occupancies = await occupancyDB.getRentalBondNotAvailedTenantsForProp({
      clientId,
      pageNum: Number(pageNum),
      limit,
      propId: propId,
    });

    tenantCount = await occupancyDB.getRentalBondNotAvailedTenantsCountForProp({
      clientId,
      propId: propId,
    });
  } else if (filter === "RBNA") {
    //Rental Bond Not avaliable done
    occupancies = await occupancyDB.getRentalBondNotAllowedTenantsForProp({
      clientId,
      pageNum: Number(pageNum),
      limit,
      propId: propId,
    });

    tenantCount = await occupancyDB.getRentalBondNotAllowedTenantsCountForProp({
      clientId,
      propId: propId,
    });
  } else if (filter === "SS") {
    //Short Stay
    occupancies = await occupancyDB.getByClientIdAndPropIdAndStayType({
      clientId,
      propId,
      pageNum: Number(pageNum),
      limit,
      stayType: CONSTANTS.STAY_TYPE.SHORT,
    });

    tenantCount = await occupancyDB.getCountByClientIdAndPropIdAndStayType({
      clientId,
      propId,
      stayType: CONSTANTS.STAY_TYPE.SHORT,
    });
  } else if (filter === "FE") {
    //Food enabled
    occupancies = await occupancyDB.getFoodEnabledTenantsByClientIdAndPropId({
      clientId,
      pageNum: Number(pageNum),
      limit,
      propId: propId,
    });

    tenantCount = await occupancyDB.getFoodEnabledTenantsCountByClientIdAndPropId({
      clientId,
      propId: propId,
    });
  } else if (filter === "FNE") {
    //Food not enabled
    occupancies = await occupancyDB.getFoodNotEnabledTenantsByClientIdAndPropId({
      clientId,
      pageNum: Number(pageNum),
      limit,
      propId: propId,
    });

    tenantCount = await occupancyDB.getFoodNotEnabledTenantsCountByClientIdAndPropId({
      clientId,
      propId: propId,
    });
  } else if (filter === "OE") {
    //Online Enabled
    occupancies = await occupancyDB.getByClientIdAndPropIdOnlineEnabled({
      clientId,
      propId,
      pageNum: Number(pageNum),
      limit,
    });
    tenantCount = await occupancyDB.getCountByClientIdAndPropIdOnlineEnabled({
      clientId,
      propId,
    });
  } else if (filter === "ONE") {
    //Online Not Enabled
    occupancies = await occupancyDB.getByClientIdAndPropIdOnlineNotEnabled({
      clientId,
      propId,
      pageNum: Number(pageNum),
      limit,
    });
    tenantCount = await occupancyDB.getCountByClientIdAndPropIdOnlineNotEnabled({
      clientId,
      propId,
    });
  } else {
    occupancies = await occupancyDB.getByClientIdAndPropId({
      clientId,
      propId,
      pageNum: Number(pageNum),
      limit,
    });

    tenantCount = await occupancyDB.getCountByClientIdAndPropId({
      clientId,
      propId,
    });
  }

  return { tenantTotalCount: tenantCount, tenantOccupancies: occupancies };
};

export const tenantsForClient = async (
  clientId: number,
  propId: number,
  pageNum: number,
  filter: string,
  filterVal: string,
  searchVal: string,
  platform: string,
  limit: number,
  startDate: string,
  endDate: string,
) => {
  let tenantCount = 0;
  let occupancies = [];
  if (searchVal && searchVal !== "undefined" && filter !== "E" && filter !== "R") {
    occupancies = await occupancyDB.getTenantSearchByClientId({
      clientId,
      pageNum: Number(pageNum),
      limit,
      searchVal,
    });

    tenantCount = await occupancyDB.getTenantSearchCountByClientId({
      clientId,
      searchVal,
    });
  } else if (filter === "U") {
    //Defaulter
    occupancies = await occupancyDB.getUnpaidTenantsByClientId({
      clientId,
      pageNum: Number(pageNum),
      limit,
    });

    tenantCount = await occupancyDB.getUnpaidTenantsCountByClientId({
      clientId,
    });
  } else if (filter === "M") {
    //Moving Out
    occupancies = await occupancyDB.getMovingOutTenantsByClientId({
      clientId,
      pageNum: Number(pageNum),
      limit,
    });

    tenantCount = await occupancyDB.getMovingOutTenantsCountByClientId({
      clientId,
    });
  } else if (filter === "R") {
    //Reserved Tenants
    if (searchVal && searchVal !== "undefined") {
      occupancies = await occupancyDB.getReservedTenantSearchByClientId({
        clientId,
        pageNum: Number(pageNum),
        limit,
        searchVal
      });

      tenantCount = await occupancyDB.getReservedTenantSearchCountByClientId({
        clientId,
      });
    } else {
      occupancies = await occupancyDB.getReservedTenantsByClientId({
        clientId,
        pageNum: Number(pageNum),
        limit,
      });

      tenantCount = await occupancyDB.getReservedTenantsCountByClientId({
        clientId,
      });
    }
  } else if (filter === "NR") {
    //All Tenants excluding Reserved ones
    occupancies = await occupancyDB.getTenantsWithoutReservedByClientId({
      clientId,
      pageNum: Number(pageNum),
      limit,
    });

    tenantCount = await occupancyDB.getTenantsWithoutReservedCountByClientId({
      clientId,
    });
  } else if (filter === "V") {
    //Verified
    occupancies = await occupancyDB.getVerifiedTenantsByClientId({
      clientId,
      pageNum: Number(pageNum),
      limit,
    });

    tenantCount = await occupancyDB.getVerifiedTenantsCountByClientId({
      clientId,
    });
  } else if (filter === "UV") {
    //Unverified
    occupancies = await occupancyDB.getUnverifiedTenantsByClientId({
      clientId,
      pageNum: Number(pageNum),
      limit,
    });

    tenantCount = await occupancyDB.getUnverifiedTenantsCountByClientId({
      clientId,
    });
  } else if (filter === "NT") {
    //New Tenants
    occupancies = await occupancyDB.getNewTenantsByClientId({
      clientId,
      pageNum: Number(pageNum),
      limit,
    });

    tenantCount = await occupancyDB.getNewTenantsCountByClientId({
      clientId,
    });
  } else if (filter === "E") {
    //Evicted
    if (searchVal && searchVal !== "undefined") {
      occupancies = await occupancyDB.getEvictedTenantsSearchByClientId({
        clientId,
        pageNum: Number(pageNum),
        limit,
        searchVal,
      });

      tenantCount = await occupancyDB.getEvictedTenantsSearchCountByClientId({
        clientId,
        searchVal,
      });
    } else {
      if (startDate && startDate.trim() !== "" && endDate && endDate.trim() !== "") {
        occupancies = await occupancyDB.getEvictedTenantsByClientIdAndDateRange({
          clientId,
          pageNum: Number(pageNum),
          limit,
          startDate,
          endDate,
        });

        tenantCount = await occupancyDB.getEvictedTenantsCountByClientIdAndDateRange({
          clientId,
          startDate,
          endDate,
        });
      } else {
        occupancies = await occupancyDB.getEvictedTenantsByClientId({
          clientId,
          pageNum: Number(pageNum),
          limit,
        });
  
        tenantCount = await occupancyDB.getEvictedTenantsCountByClientId({
          clientId,
        });
      }
    }
  } else if (platform === "I" || filter === "I") { //App installed
    occupancies = await occupancyDB.getAppInstalledTenantsByClientId({
      clientId,
      pageNum: Number(pageNum),
      limit,
    });

    tenantCount = await occupancyDB.getAppInstalledTenantsCountByClientId({
      clientId,
    });
  } else if (platform === "NI" || filter === "NI") { //App not installed
    occupancies = await occupancyDB.getAppNotInstalledTenantsByClientId({
      clientId,
      pageNum: Number(pageNum),
      limit,
    });

    tenantCount = await occupancyDB.getAppNotInstalledTenantsCountByClientId({
      clientId,
    });
  } else if (filter === "CS") {
    //createdAt sort
    occupancies = await occupancyDB.getByClientIdSortByCreatedAt({
      clientId,
      pageNum: Number(pageNum),
      limit,
    });
    tenantCount = await occupancyDB.getCountByClientId({
      clientId,
    });
  } else if (filter === "RS") {
    //room sort
    occupancies = await occupancyDB.getByClientIdSortByRoom({
      clientId,
      pageNum: Number(pageNum),
      limit,
    });
    tenantCount = await occupancyDB.getCountByClientId({
      clientId,
    });
  } else if (filter === "PV") {
    //Police Verification 
    occupancies = await occupancyDB.getPVCompletedTenants({
      clientId,
      pageNum: Number(pageNum),
      limit,
    });

    tenantCount = await occupancyDB.getPVCompletedTenantsCount({
      clientId,
    });
  } else if (filter === "PVP") {
    //Police Verification Pending
    occupancies = await occupancyDB.getPVPendingTenants({
      clientId,
      pageNum: Number(pageNum),
      limit,
    });

    tenantCount = await occupancyDB.getPVPendingTenantsCount({
      clientId,
    });
  } else if (filter === "RA") {
    //Rent Agreement done
    occupancies = await occupancyDB.getRASignedTenants({
      clientId,
      pageNum: Number(pageNum),
      limit,
    });

    tenantCount = await occupancyDB.getRASignedTenantsCount({
      clientId,
    });
  } else if (filter === "RAP") {
    //Rent Argeement Pending
    occupancies = await occupancyDB.getRAPendingTenants({
      clientId,
      pageNum: Number(pageNum),
      limit,
    });

    tenantCount = await occupancyDB.getRAPendingTenantsCount({
      clientId,
      kycStatus: CONSTANTS.KYC_STATUS.POLICE_VERIFICATION_DONE,
    });
  } else if (filter === "SS") {
    //Short Stay
    occupancies = await occupancyDB.getByClientIdAndStayType({
      clientId,
      stayType: CONSTANTS.STAY_TYPE.SHORT,
      pageNum: Number(pageNum),
      limit,
    });
    tenantCount = await occupancyDB.getCountByClientIdAndStayType({
      clientId,
      stayType: CONSTANTS.STAY_TYPE.SHORT,
    });
  } else if (filter === "RB") {
    //Rental Bond Availaved
    occupancies = await occupancyDB.getRentalBondAvailedTenants({
      clientId,
      pageNum: Number(pageNum),
      limit
    });

    tenantCount = await occupancyDB.getRentalBondAvailedTenantsCount({
      clientId
    });
  } else if (filter === "RBN") {
    //Rental Bond Not Availaved 
    occupancies = await occupancyDB.getRentalBondNotAvailedTenants({
      clientId,
      pageNum: Number(pageNum),
      limit
    });

    tenantCount = await occupancyDB.getRentalBondNotAvailedTenantsCount({
      clientId
    });
  } else if (filter === "RBNA") {
    //Rental Bond Not Allowed
    occupancies = await occupancyDB.getRentalBondNotAllowedTenants({
      clientId,
      pageNum: Number(pageNum),
      limit
    });

    tenantCount = await occupancyDB.getRentalBondNotAllowedTenantsCount({
      clientId
    });
  } else if (filter === "ZS") {
    //Zero Security
    occupancies = await occupancyDB.getZeroDepositTenantsByClientId({
      clientId,
      pageNum: Number(pageNum),
      limit,
    });

    tenantCount = await occupancyDB.getZeroDepositTenantsCountByClientId({
      clientId,
    });
  } else if (filter === "1RS") {
    //One Month Rent = Security
    occupancies = await occupancyDB.getNonZeroDepositTenantsByClientId({
      clientId,
      pageNum: Number(pageNum),
      limit,
      rentMultiplier: 1,
    });

    tenantCount = await occupancyDB.getNonZeroDepositTenantsCountByClientId({
      clientId,
      rentMultiplier: 1,
    });
  } else if (filter === "1.5RS") {
    //1.5 Month Rent = Security
    occupancies = await occupancyDB.getNonZeroDepositTenantsByClientId({
      clientId,
      pageNum: Number(pageNum),
      limit,
      rentMultiplier: 1.5,
    });

    tenantCount = await occupancyDB.getNonZeroDepositTenantsCountByClientId({
      clientId,
      rentMultiplier: 1.5,
    });
  } else if (filter === "2RS") {
    //2 Month Rent = Security
    occupancies = await occupancyDB.getNonZeroDepositTenantsByClientId({
      clientId,
      pageNum: Number(pageNum),
      limit,
      rentMultiplier: 2,
    });

    tenantCount = await occupancyDB.getNonZeroDepositTenantsCountByClientId({
      clientId,
      rentMultiplier: 2,
    });
  } else if (filter === "FE") {
    //Food enabled
    occupancies = await occupancyDB.getFoodEnabledTenantsByClientId({
      clientId,
      pageNum: Number(pageNum),
      limit,
    });

    tenantCount = await occupancyDB.getFoodEnabledTenantsCountByClientId({
      clientId,
    });
  } else if (filter === "FNE") {
    //Food not enabled
    occupancies = await occupancyDB.getFoodNotEnabledTenantsByClientId({
      clientId,
      pageNum: Number(pageNum),
      limit,
    });

    tenantCount = await occupancyDB.getFoodNotEnabledTenantsCountByClientId({
      clientId,
    });
  } else if (filter === "ED") {
    //Evicted Tenants with Dues For Web
    if (startDate && startDate.trim() !== "" && endDate && endDate.trim() !== "") {
      occupancies =
        await occupancyDB.getDefaulterEvictedTenantsByClientIdAndDateRange({
          clientId,
          pageNum: Number(pageNum),
          limit,
          startDate,
          endDate,
        });
  
      tenantCount =
        await occupancyDB.getDefaulterEvictedTenantsCountByClientIdAndDateRange({
          clientId,
          startDate,
          endDate
        });
    } else {
      occupancies =
        await occupancyDB.getDefaulterEvictedTenantsByClientId({
          clientId,
          pageNum: Number(pageNum),
          limit,
        });
  
      tenantCount =
        await occupancyDB.getDefaulterEvictedTenantsCountByClientId({
          clientId,
        });
    }
  } else if (filter === "ECM") {
    //Evicted Current Month for web
    occupancies =
      await occupancyDB.getCurMonthEvictedTenantsByClientId({
        clientId,
        pageNum: Number(pageNum),
        limit,
      });

    tenantCount =
      await occupancyDB.getCurMonthEvictedTenantsCountByClientId({
        clientId,
      });
  } else if (filter === "EPR") {
    //Evicted Pending Refund
    occupancies = await occupancyDB.getEvictedTenantsWithPendingRefundByClientId({
      clientId,
      pageNum: Number(pageNum),
      limit,
      startDate,
      endDate,
    });

    tenantCount = await occupancyDB.getEvictedTenantsWithPendingRefundCountByClientId({
      clientId,
      startDate,
      endDate,
    });
  } else if (filter === "ERD") {
    //Evicted Refund Done
    occupancies = await occupancyDB.getEvictedTenantsWithProcessedRefundByClientId({
      clientId,
      pageNum: Number(pageNum),
      limit,
      startDate,
      endDate,
    });

    tenantCount = await occupancyDB.getEvictedTenantsWithProcessedRefundCountByClientId({
      clientId,
      startDate,
      endDate,
    });
  } else if (filter === "FNFG") {
    //Evicted FNF Generated
    occupancies = await occupancyDB.getFNFGeneratedEvictedTenantsByClientId({
      clientId,
      pageNum: Number(pageNum),
      limit,
      startDate,
      endDate,
    });

    tenantCount = await occupancyDB.getFNFGeneratedEvictedTenantsByClientIdCount({
      clientId,
      startDate,
      endDate,
    });
  } else if (filter === "CB") {
    //Cancelled Booking
    occupancies = await occupancyDB.getTenantsByClientIdAndWasCancelled({
      clientId,
      wasCancelled: 1,
      pageNum: Number(pageNum),
      limit,
      startDate,
      endDate,
    });

    tenantCount = await occupancyDB.getTenantsCountByClientIdAndWasCancelled({
      clientId,
      wasCancelled: 1,
      startDate,
      endDate,
    });
  } else if (filter === "MO") {
    //Normal MovedOut
    occupancies = await occupancyDB.getTenantsByClientIdAndWasCancelled({
      clientId,
      wasCancelled: 0,
      pageNum: Number(pageNum),
      limit,
      startDate,
      endDate,
    });

    tenantCount = await occupancyDB.getTenantsCountByClientIdAndWasCancelled({
      clientId,
      wasCancelled: 0,
      startDate,
      endDate,
    });
  } else if (filter === "WP") {
    //Working(fnf) pending
    occupancies = await occupancyDB.getTenantsByClientIdAndPendingFnF({
      clientId,
      pageNum: Number(pageNum),
      limit,
    });

    tenantCount = await occupancyDB.getTenantsCountByClientIdAndPendingFnF({
      clientId,
    });
  } else if (filter === "TB") {
    //Today's Booking
    occupancies = await occupancyDB.getByClientIdTodayBooking({
      clientId,
      pageNum: Number(pageNum),
      limit,
    });
    tenantCount = await occupancyDB.getCountByClientIdTodayBooking({
      clientId,
    });
  } else if (filter === "OE") {
    //Online Enabled
    occupancies = await occupancyDB.getByClientIdOnlineEnabled({
      clientId,
      pageNum: Number(pageNum),
      limit,
    });
    tenantCount = await occupancyDB.getCountByClientIdOnlineEnabled({
      clientId,
    });
  } else if (filter === "ONE") {
    //Online Not Enabled
    occupancies = await occupancyDB.getByClientIdOnlineNotEnabled({
      clientId,
      pageNum: Number(pageNum),
      limit,
    });
    tenantCount = await occupancyDB.getCountByClientIdOnlineNotEnabled({
      clientId,
    });
  } else if (filter === "GM") {
    //Gender Male
    occupancies = await occupancyDB.getByClientIdAndGender({
      clientId,
      pageNum: Number(pageNum),
      limit,
      gender: CONSTANTS.GENDER.MALE,
    });
    tenantCount = await occupancyDB.getCountByClientIdAndGender({
      clientId,
      gender: CONSTANTS.GENDER.MALE,
    });
  } else if (filter === "GF") {
    //Gender Female
    occupancies = await occupancyDB.getByClientIdAndGender({
      clientId,
      pageNum: Number(pageNum),
      limit,
      gender: CONSTANTS.GENDER.FEMALE,
    });
    tenantCount = await occupancyDB.getCountByClientIdAndGender({
      clientId,
      gender: CONSTANTS.GENDER.FEMALE,
    });
  } else if (filter === "GO") {
    //Gender Other
    occupancies = await occupancyDB.getByClientIdAndGender({
      clientId,
      pageNum: Number(pageNum),
      limit,
      gender: CONSTANTS.GENDER.OTHER,
    });
    tenantCount = await occupancyDB.getCountByClientIdAndGender({
      clientId,
      gender: CONSTANTS.GENDER.OTHER,
    });
  } else if (filter === "BY") {
    //Booked By
    let filterValArr: any = [];
    if (filterVal && typeof filterVal === 'string') {
      filterValArr = filterVal.split(',').map(v => v.trim()).filter(Boolean);
    }
    occupancies = await occupancyDB.getByClientIdAndBookedBy({
      clientId,
      pageNum: Number(pageNum),
      limit,
      filterVal: filterValArr,
    });
    tenantCount = await occupancyDB.getCountByClientIdAndBookedBy({
      clientId,
      filterVal: filterValArr,
    });
  } else if (filter === "L") {
    //location
    occupancies = await occupancyDB.getByClientIdAndLocation({
      clientId,
      locationId: Number(filterVal),
      pageNum: Number(pageNum),
      limit,
    });
    tenantCount = await occupancyDB.getCountByClientIdAndLocation({
      clientId,
      locationId: Number(filterVal),
    });
  } else {
    occupancies = await occupancyDB.getByClientId({
      clientId,
      pageNum: Number(pageNum),
      limit,
    });
    tenantCount = await occupancyDB.getCountByClientId({
      clientId,
    });
  }

  return { tenantTotalCount: tenantCount, tenantOccupancies: occupancies };
};

export const tenantsForStaff = async (
  clientId: number,
  staffId: number,
  propId: number,
  pageNum: number,
  filter: string,
  filterVal: string,
  searchVal: string,
  limit: number,
  startDate: string,
  endDate: string,
) => {
  let tenantCount = 0;
  let occupancies = [];

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

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

    if (searchVal && searchVal !== "undefined" && filter !== "E" && filter !== "R") {
      occupancies = await occupancyDB.getSearchResultForStaff({
        clientId,
        pageNum: Number(pageNum),
        limit,
        searchVal,
        propertiesIds,
      });

      tenantCount = await occupancyDB.getSearchResultCountForStaff({
        clientId,
        searchVal,
        propertiesIds,
      });
    } else if (filter === "U") {
      occupancies = await occupancyDB.getUnpaidTenantsForStaff({
        clientId,
        pageNum: Number(pageNum),
        limit,
        propertiesIds,
      });

      tenantCount = await occupancyDB.getUnpaidTenantsCountForStaff({
        clientId,
        propertiesIds,
      });
    } else if (filter === "M") {
      occupancies = await occupancyDB.getMovingOutTenantsForStaff({
        clientId,
        pageNum: Number(pageNum),
        limit,
        propertiesIds,
      });

      tenantCount = await occupancyDB.getMovingOutTenantsCountForStaff({
        clientId,
        propertiesIds,
      });
    } else if (filter === "R") {
      // Reserved Tenants
      if (searchVal && searchVal !== "undefined") {
        occupancies = await occupancyDB.getReservedTenantSearchForStaff({
          clientId,
          pageNum: Number(pageNum),
          limit,
          propertiesIds,
          searchVal,
        });
        
        tenantCount = await occupancyDB.getReservedTenantSearchCountForStaff({
          clientId,
          propertiesIds,
          searchVal,
        });
      } else {
        occupancies = await occupancyDB.getReservedTenantsForStaff({
          clientId,
          pageNum: Number(pageNum),
          limit,
          propertiesIds,
        });
        
        tenantCount = await occupancyDB.getReservedTenantsCountForStaff({
          clientId,
          propertiesIds,
        });
      }
    } else if (filter === "NR") {
      //all tenant without reserved ones
      occupancies = await occupancyDB.getTenantsWithoutReservedForStaff({
        clientId,
        pageNum: Number(pageNum),
        limit,
        propertiesIds,
      });

      tenantCount = await occupancyDB.getTenantsWithoutReservedCountForStaff({
        clientId,
        propertiesIds,
      });
    } else if (filter === "V") {
      //Verified
      occupancies = await occupancyDB.getVerifiedTenantsForStaff({
        clientId,
        pageNum: Number(pageNum),
        limit,
        propertiesIds,
      });

      tenantCount = await occupancyDB.getVerifiedTenantsCountForStaff({
        clientId,
        propertiesIds,
      });
    } else if (filter === "UV") {
      //Unverified
      occupancies = await occupancyDB.getUnverifiedTenantsForStaff({
        clientId,
        pageNum: Number(pageNum),
        limit,
        propertiesIds,
      });

      tenantCount = await occupancyDB.getUnverifiedTenantsCountForStaff({
        clientId,
        propertiesIds,
      });
    } else if (filter === "NT") {
      //New Tenants
      occupancies = await occupancyDB.getNewTenantsForStaff({
        clientId,
        pageNum: Number(pageNum),
        limit,
        propertiesIds,
      });

      tenantCount = await occupancyDB.getNewTenantsCountForStaff({
        clientId,
        propertiesIds,
      });
    } else if (filter === "E") {
      if (searchVal && searchVal !== "undefined") {
        occupancies = await occupancyDB.getEvictedTenantsSearchByClientIdForStaff({
          clientId,
          propertiesIds,
          pageNum: Number(pageNum),
          limit,
          searchVal,
        });

        tenantCount = await occupancyDB.getEvictedTenantsSearchCountByClientIdForStaff({
          clientId,
          propertiesIds,
          searchVal,
        });
      } else {
        if (startDate && startDate.trim() !== "" && endDate && endDate.trim() !== "") {
          occupancies = await occupancyDB.getEvictedTenantsForStaffAndDateRange({
            clientId,
            pageNum: Number(pageNum),
            limit,
            propertiesIds,
            startDate,
            endDate,
          });
  
          tenantCount = await occupancyDB.getEvictedTenantsCountForStaffAndDateRange({
            clientId,
            propertiesIds,
            startDate,
            endDate,
          });
        } else {
          occupancies = await occupancyDB.getEvictedTenantsForStaff({
            clientId,
            pageNum: Number(pageNum),
            limit,
            propertiesIds,
          });
    
          tenantCount = await occupancyDB.getEvictedTenantsCountForStaff({
            clientId,
            propertiesIds,
          });
        }
      }
    } else if (filter === "ED") {
      //Evicted Tenants with Dues For Web
      occupancies =
        await occupancyDB.getDefaulterEvictedTenantsByClientIdAndDateRangeForStaff({
          clientId,
          pageNum: Number(pageNum),
          limit,
          startDate,
          endDate,
          propertiesIds,
        });
  
      tenantCount =
        await occupancyDB.getDefaulterEvictedTenantsCountByClientIdAndDateRangeForStaff({
          clientId,
          startDate,
          endDate,
          propertiesIds,
        });
    } else if (filter === "ECM") {
        //Evicted Current Month for web
      occupancies = await occupancyDB.getCurMonthEvictedTenantsByClientIdForStaff({
        clientId,
        pageNum: Number(pageNum),
        limit,
        propertiesIds
      });

      tenantCount = await occupancyDB.getCurMonthEvictedTenantsCountByClientIdForStaff({
        clientId,
        propertiesIds,
      });
    } else if (filter === "EPR") {
      //Evicted Pending Refund
      occupancies = await occupancyDB.getEvictedTenantsWithPendingRefundByClientIdForStaff({
        clientId,
        pageNum: Number(pageNum),
        limit,
        propertiesIds,
        startDate,
        endDate,
      });

      tenantCount = await occupancyDB.getEvictedTenantsWithPendingRefundCountByClientIdForStaff({
        clientId,
        propertiesIds,
        startDate,
        endDate,
      });
    } else if (filter === "ERD") {
      //Evicted Refund Done
      occupancies = await occupancyDB.getEvictedTenantsWithProcessedRefundByClientIdForStaff({
        clientId,
        pageNum: Number(pageNum),
        limit,
        propertiesIds,
        startDate,
        endDate,
      });

      tenantCount = await occupancyDB.getEvictedTenantsWithProcessedRefundCountByClientIdForStaff({
        clientId,
        propertiesIds,
        startDate,
        endDate,
      });
    } else if (filter === "FNFG") {
      //Evicted FNF Generated
      occupancies = await occupancyDB.getFNFGeneratedEvictedTenantsByClientIdForStaff({
        clientId,
        propertiesIds,
        pageNum: Number(pageNum),
        limit,
        startDate,
        endDate,
      });

      tenantCount = await occupancyDB.getFNFGeneratedEvictedTenantsByClientIdCountForStaff({
        clientId,
        propertiesIds,
        startDate,
        endDate,
      });
    } else if (filter === "CB") {
      //Cancelled Booking
      occupancies = await occupancyDB.getTenantsByClientIdAndWasCancelledForStaff({
        clientId,
        propertiesIds,
        wasCancelled: 1,
        pageNum: Number(pageNum),
        limit,
        startDate,
        endDate,
      });

      tenantCount = await occupancyDB.getTenantsCountByClientIdAndWasCancelledForStaff({
        clientId,
        propertiesIds,
        wasCancelled: 1,
        startDate,
        endDate,
      });
    } else if (filter === "MO") {
      //Normal MovedOut
      occupancies = await occupancyDB.getTenantsByClientIdAndWasCancelledForStaff({
        clientId,
        propertiesIds,
        wasCancelled: 0,
        pageNum: Number(pageNum),
        limit,
        startDate,
        endDate,
      });

      tenantCount = await occupancyDB.getTenantsCountByClientIdAndWasCancelledForStaff({
        clientId,
        propertiesIds,
        wasCancelled: 0,
        startDate,
        endDate,
      });
    } else if (filter === "WP") {
      //Working(Fnf) pending
      occupancies = await occupancyDB.getTenantsByClientIdAndPendingFnFForStaff({
        clientId,
        propertiesIds,
        pageNum: Number(pageNum),
        limit,
      });

      tenantCount = await occupancyDB.getTenantsCountByClientIdAndPendingFnFForStaff({
        clientId,
        propertiesIds,
      });
    } else if (filter === "RS") {
      occupancies = await occupancyDB.getForStaffSortByRoom({
        clientId,
        pageNum: Number(pageNum),
        limit,
        propertiesIds,
      });

      tenantCount = await occupancyDB.getCountForStaff({
        clientId,
        propertiesIds,
      });
    } else if (filter === "CS") {
      //Created At sort by
      occupancies = await occupancyDB.getForStaffSortByCreatedAt({
        clientId,
        pageNum: Number(pageNum),
        limit,
        propertiesIds,
      });

      tenantCount = await occupancyDB.getCountForStaff({
        clientId,
        propertiesIds,
      });
    } else if (filter === "PV") {
      //Police Verification 
      occupancies = await occupancyDB.getPVTenantsForStaff({
        clientId,
        propertiesIds: propertiesIds,
        isPoliceVerified: 1,
        pageNum: Number(pageNum),
        limit,
      });

      tenantCount = await occupancyDB.getPVTenantsCountForStaff({
        clientId,
        propertiesIds: propertiesIds,
        isPoliceVerified: 1,
      });
    } else if (filter === "PVP") {
      //Police Verification Pending
      occupancies = await occupancyDB.getPVTenantsForStaff({
        clientId,
        propertiesIds: propertiesIds,
        isPoliceVerified: 0,
        pageNum: Number(pageNum),
        limit,
      });

      tenantCount = await occupancyDB.getPVTenantsCountForStaff({
        clientId,
        propertiesIds: propertiesIds,
        isPoliceVerified: 0,
      });
    } else if (filter === "RB") {
      //Rental Bond Taken
      occupancies = await occupancyDB.getRBAvailedTenantsForStaff({
        clientId,
        propertiesIds: propertiesIds,
        pageNum: Number(pageNum),
        limit,
      });

      tenantCount = await occupancyDB.getRBAvailedTenantsCountForStaff({
        clientId,
        propertiesIds: propertiesIds,
      });
    } else if (filter === "RBN") {
      //Rental Bond not availed Pending
      occupancies = await occupancyDB.getRBNotAvailedTenantsForStaff({
        clientId,
        propertiesIds: propertiesIds,
        pageNum: Number(pageNum),
        limit,
      });

      tenantCount = await occupancyDB.getRBNotAvailedTenantsCountForStaff({
        clientId,
        propertiesIds: propertiesIds,
      });
    } else if (filter === "RBNA") {
      //Rental Bond not availed Pending
      occupancies = await occupancyDB.getRBNotAllowedTenantsForStaff({
        clientId,
        propertiesIds: propertiesIds,
        pageNum: Number(pageNum),
        limit,
      });

      tenantCount = await occupancyDB.getRBNotAllowedTenantsCountForStaff({
        clientId,
        propertiesIds: propertiesIds,
      });
    } else if (filter === "RA") {
      //Rent Agreement done
      occupancies = await occupancyDB.getRATenantsForStaff({
        clientId,
        propertiesIds: propertiesIds,
        isRentAgreementSigned: 1,
        pageNum: Number(pageNum),
        limit,
      });

      tenantCount = await occupancyDB.getRATenantsCountForStaff({
        clientId,
        propertiesIds: propertiesIds,
        isRentAgreementSigned: 1,
      });
    } else if (filter === "RAP") {
      //Rent Argeement Pending
      occupancies = await occupancyDB.getRATenantsForStaff({
        clientId,
        propertiesIds: propertiesIds,
        isRentAgreementSigned: 0,
        pageNum: Number(pageNum),
        limit,
      });

      tenantCount = await occupancyDB.getRATenantsCountForStaff({
        clientId,
        propertiesIds: propertiesIds,
        isRentAgreementSigned: 0,
      });
    } else if (filter === "SS") {
      //Short Stay
      occupancies = await occupancyDB.getForStaffAndStayType({
        clientId,
        stayType: CONSTANTS.STAY_TYPE.SHORT,
        pageNum: Number(pageNum),
        limit,
        propertiesIds,
      });
      tenantCount = await occupancyDB.getCountForStaffAndStayType({
        clientId,
        propertiesIds,
        stayType: CONSTANTS.STAY_TYPE.SHORT,
      });
    } else if (filter === "FE") {
      //Food enabled
      occupancies = await occupancyDB.getFoodEnabledTenantsByClientIdForStaff({
        clientId,
        pageNum: Number(pageNum),
        limit,
        propertiesIds: propertiesIds,
      });

      tenantCount = await occupancyDB.getFoodEnabledTenantsCountByClientIdForStaff({
        clientId,
        propertiesIds,
      });
    } else if (filter === "FNE") {
      //Food not enabled
      occupancies = await occupancyDB.getFoodNotEnabledTenantsByClientIdForStaff({
        clientId,
        pageNum: Number(pageNum),
        limit,
        propertiesIds: propertiesIds,
      });

      tenantCount = await occupancyDB.getFoodNotEnabledTenantsCountByClientIdForStaff({
        clientId,
        propertiesIds
      });
    } else if (filter === "ZS") {
      //Zero Security
      occupancies = await occupancyDB.getZeroDepositTenantsByClientIdForStaff({
        clientId,
        pageNum: Number(pageNum),
        limit,
        propertiesIds
      });

      tenantCount = await occupancyDB.getZeroDepositTenantsCountByClientIdForStaff({
        clientId,
        propertiesIds
      });
    } else if (filter === "1RS") {
      //1 Month Rent = Security
      occupancies = await occupancyDB.getNonZeroDepositTenantsByClientIdForStaff({
        clientId,
        pageNum: Number(pageNum),
        limit,
        propertiesIds,
        rentMultiplier: 1,
      });

      tenantCount = await occupancyDB.getNonZeroDepositTenantsCountByClientIdForStaff({
        clientId,
        propertiesIds,
        rentMultiplier: 1,
      });
    } else if (filter === "1.5RS") {
      //1.5 Month Rent = Security
      occupancies = await occupancyDB.getNonZeroDepositTenantsByClientIdForStaff({
        clientId,
        pageNum: Number(pageNum),
        limit,
        propertiesIds,
        rentMultiplier: 1.5,
      });

      tenantCount = await occupancyDB.getNonZeroDepositTenantsCountByClientIdForStaff({
        clientId,
        propertiesIds,
        rentMultiplier: 1.5,
      });
    } else if (filter === "2RS") {
      //2 Month Rent = Security
      occupancies = await occupancyDB.getNonZeroDepositTenantsByClientIdForStaff({
        clientId,
        pageNum: Number(pageNum),
        limit,
        propertiesIds,
        rentMultiplier: 2,
      });

      tenantCount = await occupancyDB.getNonZeroDepositTenantsCountByClientIdForStaff({
        clientId,
        propertiesIds,
        rentMultiplier: 2,
      });
    } else if (filter === "TB") {
      occupancies = await occupancyDB.getForStaffTodayBooking({
        clientId,
        pageNum: Number(pageNum),
        limit,
        propertiesIds,
      });

      tenantCount = await occupancyDB.getCountForStaffTodayBooking({
        clientId,
        propertiesIds,
      });
    } else if (filter === "OE") {
      //Online Enabled
      occupancies = await occupancyDB.getByClientIdOnlineEnabledForStaff({
        clientId,
        pageNum: Number(pageNum),
        limit,
        propertiesIds,
      });
      tenantCount = await occupancyDB.getCountByClientIdOnlineEnabledForStaff({
        clientId,
        propertiesIds,
      });
    } else if (filter === "ONE") {
      //Online Not Enabled
      occupancies = await occupancyDB.getByClientIdOnlineNotEnabledForStaff({
        clientId,
        pageNum: Number(pageNum),
        limit,
        propertiesIds,
      });
      tenantCount = await occupancyDB.getCountByClientIdOnlineNotEnabledForStaff({
        clientId,
        propertiesIds,
      });
    } else if (filter === "GM") {
      //Gender Male
      occupancies = await occupancyDB.getByClientIdAndGenderForStaff({
        clientId,
        pageNum: Number(pageNum),
        limit,
        propertiesIds,
        gender: CONSTANTS.GENDER.MALE,
      });
      tenantCount = await occupancyDB.getCountByClientIdAndGenderForStaff({
        clientId,
        propertiesIds,
        gender: CONSTANTS.GENDER.MALE,
      });
    } else if (filter === "GF") {
      //Gender Female
      occupancies = await occupancyDB.getByClientIdAndGenderForStaff({
        clientId,
        pageNum: Number(pageNum),
        limit,
        propertiesIds,
        gender: CONSTANTS.GENDER.FEMALE,
      });
      tenantCount = await occupancyDB.getCountByClientIdAndGenderForStaff({
        clientId,
        propertiesIds,
        gender: CONSTANTS.GENDER.FEMALE,
      });
    } else if (filter === "GO") {
      //Gender Other
      occupancies = await occupancyDB.getByClientIdAndGenderForStaff({
        clientId,
        pageNum: Number(pageNum),
        limit,
        propertiesIds,
        gender: CONSTANTS.GENDER.OTHER,
      });
      tenantCount = await occupancyDB.getCountByClientIdAndGenderForStaff({
        clientId,
        propertiesIds,
        gender: CONSTANTS.GENDER.OTHER,
      });
    } else if (filter === "BY") {
      //Booked By
      let filterValArr: any = [];
      if (filterVal && typeof filterVal === 'string') {
        filterValArr = filterVal.split(',').map(v => v.trim()).filter(Boolean);
      }
      occupancies = await occupancyDB.getByClientIdAndBookedByForStaff({
        clientId,
        pageNum: Number(pageNum),
        limit,
        propertiesIds,
        filterVal: filterValArr,
      });
      tenantCount = await occupancyDB.getCountByClientIdAndBookedByForStaff({
        clientId,
        propertiesIds,
        filterVal: filterValArr,
      });
    } else if (filter === "L") {
      occupancies = await occupancyDB.getByClientIdAndLocationForStaff({
        clientId,
        locationId: Number(filterVal),
        pageNum: Number(pageNum),
        limit,
        propertiesIds,
      });

      tenantCount = await occupancyDB.getCountByClientIdAndLocationForStaff({
        clientId,
        locationId: Number(filterVal),
        propertiesIds,
      });
    }  else if (filter === "I") { //App installed
      occupancies = await occupancyDB.getAppInstalledTenantsByClientIdForStaff({
        clientId,
        propertiesIds,
        pageNum: Number(pageNum),
        limit,
      });

      tenantCount = await occupancyDB.getAppInstalledTenantsCountByClientId({
        clientId,
        propertiesIds,
      });
    } else if (filter === "NI") { //App not installed
      occupancies = await occupancyDB.getAppNotInstalledTenantsByClientId({
        clientId,
        propertiesIds,
        pageNum: Number(pageNum),
        limit,
      });

      tenantCount = await occupancyDB.getAppNotInstalledTenantsCountByClientId({
        clientId,
        propertiesIds,
      });
    } else {
      occupancies = await occupancyDB.getForStaff({
        clientId,
        pageNum: Number(pageNum),
        limit,
        propertiesIds,
      });

      tenantCount = await occupancyDB.getCountForStaff({
        clientId,
        propertiesIds,
      });
    }
  }

  return { tenantTotalCount: tenantCount, tenantOccupancies: occupancies };
};

export const tenantsOfSelectedFlat = async (
  clientId: number,
  flatId: number,
  pageNum: number,
  filter: string,
  searchVal: string,
  platform: string,
  limit: number
) => {
  let tenantCount = 0;
  let occupancies = [];

  if (searchVal && searchVal !== "undefined" && filter !== "E") {
    occupancies = await occupancyDB.getSearchResultByClientIdAndFlatId({
      clientId,
      flatId,
      pageNum: Number(pageNum),
      limit,
      searchVal,
    });

    tenantCount = await occupancyDB.getSearchResultCountByClientIdAndFlatId({
      clientId,
      flatId,
      searchVal,
    });
  } else if (filter === "U") {
    occupancies = await occupancyDB.getUnpaidTenantsByClientIdAndFlatId({
      clientId,
      flatId,
      pageNum: Number(pageNum),
      limit,
    });

    tenantCount = await occupancyDB.getUnpaidTenantsCountByClientIdAndFlatId({
      clientId,
      flatId,
    });
  } else if (filter === "M") {
    occupancies = await occupancyDB.getMovingOutTenantsByClientIdAndFlatId({
      clientId,
      flatId,
      pageNum: Number(pageNum),
      limit,
    });

    tenantCount = await occupancyDB.getMovingOutTenantsCountByClientIdAndFlatId(
      {
        clientId,
        flatId,
      }
    );
  } else if (filter === "R") {
    // Reserved Tenants
    occupancies = await occupancyDB.getReservedTenantsByClientIdAndFlatId({
      clientId,
      flatId,
      pageNum: Number(pageNum),
      limit,
    });

    tenantCount = await occupancyDB.getReservedTenantsCountByClientIdAndFlatId({
      clientId,
      flatId,
    });
  } else if (filter === "NR") {
    //all tenant without reserved ones
    occupancies =
      await occupancyDB.getTenantsWithoutReservedByClientIdAndFlatId({
        clientId,
        flatId,
        pageNum: Number(pageNum),
        limit,
      });

    tenantCount =
      await occupancyDB.getTenantsWithoutReservedCountByClientIdAndFlatId({
        clientId,
        flatId,
      });
  } else if (filter === "V") {
    //Verified
    occupancies = await occupancyDB.getVerifiedTenantsByClientIdAndFlatId({
      clientId,
      flatId,
      pageNum: Number(pageNum),
      limit,
    });

    tenantCount = await occupancyDB.getVerifiedTenantsCountByClientIdAndFlatId({
      clientId,
      flatId,
    });
  } else if (filter === "UV") {
    //Unverified
    occupancies = await occupancyDB.getUnverifiedTenantsByClientIdAndFlatId({
      clientId,
      flatId,
      pageNum: Number(pageNum),
      limit,
    });

    tenantCount =
      await occupancyDB.getUnverifiedTenantsCountByClientIdAndFlatId({
        clientId,
        flatId,
      });
  } else if (filter === "NT") {
    //New Tenants
    occupancies = await occupancyDB.getNewTenantsByClientIdAndFlatId({
      clientId,
      flatId,
      pageNum: Number(pageNum),
      limit,
    });

    tenantCount = await occupancyDB.getNewTenantsCountByClientIdAndFlatId({
      clientId,
      flatId,
    });
  } else if (filter === "E") {
    occupancies = await occupancyDB.getEvictedTenantsByClientIdAndFlatId({
      clientId,
      flatId,
      pageNum: Number(pageNum),
      limit,
    });

    tenantCount = await occupancyDB.getEvictedTenantsCountByClientIdAndFlatId({
      clientId,
      flatId,
    });
  } else if (platform === "I" || filter === "I") { //App installs tenants
    occupancies = await occupancyDB.getAppInstalledTenantsByClientIdAndFlatId({
      clientId,
      flatId,
      pageNum: Number(pageNum),
      limit,
    });

    tenantCount =
      await occupancyDB.getAppInstalledTenantsCountByClientIdAndFlatId({
        clientId,
        flatId,
      });
  } else if (platform === "NI" || filter === "NI") { //App not installed
    occupancies =
      await occupancyDB.getAppNotInstalledTenantsByClientIdAndFlatId({
        clientId,
        flatId,
        pageNum: Number(pageNum),
        limit,
      });

    tenantCount =
      await occupancyDB.getAppNotInstalledTenantsCountByClientIdAndFlatId({
        clientId,
        flatId,
      });
  } else if (filter === "PV") {
    //Police Verification 
    occupancies = await occupancyDB.getPVTenantsByClientIdAndFlatId({
      clientId,
      flatId: flatId,
      pageNum: Number(pageNum),
      limit,
      isPoliceVerified:1
    });

    tenantCount = await occupancyDB.getPVTenantsCountByClientIdAndFlatId({
      clientId,
      flatId: flatId,
      isPoliceVerified:1
    });
  } else if (filter === "PVP") {
    //Police Verification Pending
    occupancies = await occupancyDB.getPVTenantsByClientIdAndFlatId({
      clientId,
      flatId: flatId,
      pageNum: Number(pageNum),
      limit,
      isPoliceVerified:0
    });

    tenantCount = await occupancyDB.getPVTenantsCountByClientIdAndFlatId({
      clientId,
      flatId: flatId,
      isPoliceVerified:0
    });
  } else if (filter === "RA") {
    //Rent Agreement done
    occupancies = await occupancyDB.getRATenantsByClientIdAndFlatId({
      clientId,
      flatId: flatId,
      pageNum: Number(pageNum),
      limit,
      isRentAgreementSigned:1
    });

    tenantCount = await occupancyDB.getRATenantsCountByClientIdAndFlatId({
      clientId,
      flatId: flatId,
      isRentAgreementSigned:1
    });
  } else if (filter === "RAP") {
    //Rent Argeement Pending
    occupancies = await occupancyDB.getRATenantsByClientIdAndFlatId({
      clientId,
      flatId: flatId,
      pageNum: Number(pageNum),
      limit,
      isRentAgreementSigned:0
    });

    tenantCount = await occupancyDB.getRATenantsCountByClientIdAndFlatId({
      clientId,
      flatId: flatId,
      isRentAgreementSigned:0
    });
  } else if (filter === "RB") {
    //Rent Agreement done
    occupancies = await occupancyDB.getRentalBondAvailedTenantsByClientIdAndFlatId({
      clientId,
      flatId: flatId,
      pageNum: Number(pageNum),
      limit,
    });

    tenantCount = await occupancyDB.getRentalBondAvailedTenantsCountByClientIdAndFlatId({
      clientId,
      flatId: flatId,
    });
  } else if (filter === "RBN") {
    //Rent Agreement done
    occupancies = await occupancyDB.getRentalBondNotAvailedTenantsByClientIdAndFlatId({
      clientId,
      flatId: flatId,
      pageNum: Number(pageNum),
      limit,
    });

    tenantCount = await occupancyDB.getRentalBondNotAvailedTenantsCountByClientIdAndFlatId({
      clientId,
      flatId: flatId,
    });
  }else if (filter === "RBNA") {
    //Rent Agreement done
    occupancies = await occupancyDB.getRentalBondNotAllowedTenantsByClientIdAndFlatId({
      clientId,
      flatId: flatId,
      pageNum: Number(pageNum),
      limit,
    });

    tenantCount = await occupancyDB.getRentalBondNotAllowedTenantsCountByClientIdAndFlatId({
      clientId,
      flatId: flatId,
    });
  } else if (filter === "SS") {
    //Short Stay
    occupancies = await occupancyDB.getByClientIdAndFlatIdAndStayType({
      clientId,
      flatId,
      pageNum: Number(pageNum),
      limit,
      stayType: CONSTANTS.STAY_TYPE.SHORT,
    });

    tenantCount = await occupancyDB.getCountByClientIdAndFlatIdAndStayType({
      clientId,
      flatId,
      stayType: CONSTANTS.STAY_TYPE.SHORT,
    });
  } else if (filter === "FE") {
    //Food enabled
    occupancies = await occupancyDB.getFoodEnabledTenantsByClientIdAndFlatId({
      clientId,
      pageNum: Number(pageNum),
      limit,
      flatId: flatId,
    });

    tenantCount = await occupancyDB.getFoodEnabledTenantsCountByClientIdAndFlatId({
      clientId,
      flatId: flatId,
    });
  } else if (filter === "FNE") {
    //Food not enabled
    occupancies = await occupancyDB.getFoodNotEnabledTenantsByClientIdAndFlatId({
      clientId,
      pageNum: Number(pageNum),
      limit,
      flatId: flatId,
    });

    tenantCount = await occupancyDB.getFoodNotEnabledTenantsCountByClientIdAndFlatId({
      clientId,
      flatId: flatId,
    });
  } else if (filter === "OE") {
    //Online Enabled
    occupancies = await occupancyDB.getByClientIdAndFlatIdOnlineEnabled({
      clientId,
      flatId: flatId,
      pageNum: Number(pageNum),
      limit,
    });
    tenantCount = await occupancyDB.getCountByClientIdAndFlatIdOnlineEnabled({
      clientId,
      flatId: flatId,
    });
  } else if (filter === "ONE") {
    //Online Not Enabled
    occupancies = await occupancyDB.getByClientIdAndFlatIdOnlineNotEnabled({
      clientId,
      flatId: flatId,
      pageNum: Number(pageNum),
      limit,
    });
    tenantCount = await occupancyDB.getCountByClientIdAndFlatIdOnlineNotEnabled({
      clientId,
      flatId: flatId,
    });
  } else {
    occupancies = await occupancyDB.getByClientIdAndFlatId({
      clientId,
      flatId,
      pageNum: Number(pageNum),
      limit,
    });

    tenantCount = await occupancyDB.getCountByClientIdAndFlatId({
      clientId,
      flatId,
    });
  }

  return { tenantTotalCount: tenantCount, tenantOccupancies: occupancies };
};

export const tenantsOfSelectedPropertyForWeb = async (
  clientId: number,
  propId: number,
  pageNum: number,
  filter: string,
  filterVal: string,
  searchVal: string,
  platform: string,
  limit: number,
  startDate: string,
  endDate: string,
) => {
  let tenantCount = 0;
  let occupancies = [];

  const property = await propertyDB.getById({
    id: propId,
  });

  if (searchVal && searchVal !== "undefined" && filter !== "E") {
    occupancies = await occupancyDB.getSearchResultByClientIdAndPropId({
      clientId,
      propId,
      pageNum: Number(pageNum),
      limit,
      searchVal,
    });

    tenantCount = await occupancyDB.getSearchResultCountByClientIdAndPropId({
      clientId,
      propId,
      searchVal,
    });
  } else if (filter === "U") {
    occupancies = await occupancyDB.getUnpaidTenantsByClientIdAndPropId({
      clientId,
      propId,
      pageNum: Number(pageNum),
      limit,
    });

    tenantCount = await occupancyDB.getUnpaidTenantsCountByClientIdAndPropId({
      clientId,
      propId,
    });
  } else if (filter === "UV") {
    //Unverified tenants
    occupancies = await occupancyDB.getUnverifiedTenantsByClientIdAndPropId({
      clientId,
      propId,
      pageNum: Number(pageNum),
      limit,
    });

    tenantCount =
      await occupancyDB.getUnverifiedTenantsCountByClientIdAndPropId({
        clientId,
        propId,
      });
  } else if (filter === "M") {
    occupancies = await occupancyDB.getMovingOutTenantsByClientIdAndPropId({
      clientId,
      propId,
      pageNum: Number(pageNum),
      limit,
    });

    tenantCount = await occupancyDB.getMovingOutTenantsCountByClientIdAndPropId(
      {
        clientId,
        propId,
      }
    );
  } else if (filter === "R") {
    //Reserved Tenants
    occupancies = await occupancyDB.getReservedTenantsByClientIdAndPropId({
      clientId,
      propId,
      pageNum: Number(pageNum),
      limit,
    });

    tenantCount = await occupancyDB.getReservedTenantsCountByClientIdAndPropId({
      clientId,
      propId,
    });
  } else if (filter === "NR") {
    //All Tenants excluding Reserved Ones
    occupancies =
      await occupancyDB.getWithoutReservedTenantsByClientIdAndPropId({
        clientId,
        propId,
        pageNum: Number(pageNum),
        limit,
      });

    tenantCount =
      await occupancyDB.getWithoutReservedTenantsCountByClientIdAndPropId({
        clientId,
        propId,
      });
  } else if (filter === "V") {
    //verified tenants
    occupancies = await occupancyDB.getVerifiedTenantsByClientIdAndPropId({
      clientId,
      propId,
      pageNum: Number(pageNum),
      limit,
    });

    tenantCount = await occupancyDB.getVerifiedTenantsCountByClientIdAndPropId({
      clientId,
      propId,
    });
  } else if (filter === "UV") {
    //Unverified tenants
    occupancies = await occupancyDB.getUnverifiedTenantsByClientIdAndPropId({
      clientId,
      propId,
      pageNum: Number(pageNum),
      limit,
    });

    tenantCount =
      await occupancyDB.getUnverifiedTenantsCountByClientIdAndPropId({
        clientId,
        propId,
      });
  } else if (filter === "NT") {
    //New Tenants
    occupancies = await occupancyDB.getNewTenantsByClientIdAndPropId({
      clientId,
      propId,
      pageNum: Number(pageNum),
      limit,
    });

    tenantCount = await occupancyDB.getNewTenantsCountByClientIdAndPropId({
      clientId,
      propId,
    });
  } else if (filter === "E") {
    if (searchVal && searchVal !== "undefined") {
      occupancies = await occupancyDB.getEvictedTenantsSearchByPropId({
        clientId,
        propId,
        pageNum: Number(pageNum),
        limit,
        searchVal,
      });

      tenantCount = await occupancyDB.getEvictedTenantsSearchCountByPropId({
        clientId,
        propId,
        searchVal,
      });
    } else {
      if (startDate && startDate.trim() !== "" && endDate && endDate.trim() !== "") {
        occupancies = await occupancyDB.getEvictedTenantsByClientIdAndPropIdAndDateRange({
          clientId,
          propId,
          pageNum: Number(pageNum),
          limit,
          startDate,
          endDate,
        });
        tenantCount = await occupancyDB.getEvictedTenantsCountByClientIdAndPropIdAndDateRange({
          clientId,
          propId,
          startDate,
          endDate,
        });
      } else {
        occupancies = await occupancyDB.getEvictedTenantsByClientIdAndPropId({
          clientId,
          propId,
          pageNum: Number(pageNum),
          limit,
        });
        tenantCount = await occupancyDB.getEvictedTenantsCountByClientIdAndPropId({
          clientId,
          propId,
        });
      }
    }
  } else if (filter === "ED") {
    //Evicted Tenants with Dues
    occupancies =
      await occupancyDB.getDefaulterEvictedTenantsByClientIdAndPropId({
        clientId,
        propId,
        pageNum: Number(pageNum),
        limit,
      });

    tenantCount =
      await occupancyDB.getDefaulterEvictedTenantsCountByClientIdAndPropId({
        clientId,
        propId,
      });
  } else if (filter === "ECM") {
    //Current Month Evicted Tenants
    occupancies =
      await occupancyDB.getCurMonthEvictedTenantsByClientIdAndPropId({
        clientId,
        propId,
        pageNum: Number(pageNum),
        limit,
      });

    tenantCount =
      await occupancyDB.getCurMonthEvictedTenantsCountByClientIdAndPropId({
        clientId,
        propId,
      });
  } else if (filter === "EPR") {
    //Evicted Pending Refund
    occupancies =
      await occupancyDB.getEvictedTenantsWithPendingRefundByClientIdAndPropId({
        clientId,
        propId,
        pageNum: Number(pageNum),
        limit,
        startDate,
        endDate,
      });

    tenantCount =
      await occupancyDB.getEvictedTenantsWithPendingRefundCountByClientIdAndPropId({
        clientId,
        propId,
        startDate,
        endDate,
      });
  } else if (filter === "ERD") {
    //Evicted Refund Done
    occupancies =
      await occupancyDB.getEvictedTenantsWithProcessedRefundByClientIdAndPropId({
        clientId,
        propId,
        pageNum: Number(pageNum),
        limit,
        startDate,
        endDate,
      });

    tenantCount =
      await occupancyDB.getEvictedTenantsWithProcessedRefundCountByClientIdAndPropId({
        clientId,
        propId,
        startDate,
        endDate,
      });
  } else if (filter === "CB") {
    //Cancelled Booking
    occupancies = await occupancyDB.getTenantsByClientIdAndPropIdAndWasCancelled({
      clientId,
      propId,
      wasCancelled: 1,
      pageNum: Number(pageNum),
      limit,
      startDate,
      endDate,
    });

    tenantCount = await occupancyDB.getTenantsCountByClientIdAndPropIdAndWasCancelled({
      clientId,
      propId,
      wasCancelled: 1,
      startDate,
      endDate,
    });
  } else if (filter === "MO") {
    //Normal MovedOut
    occupancies = await occupancyDB.getTenantsByClientIdAndPropIdAndWasCancelled({
      clientId,
      propId,
      wasCancelled: 0,
      pageNum: Number(pageNum),
      limit,
      startDate,
      endDate,
    });

    tenantCount = await occupancyDB.getTenantsCountByClientIdAndPropIdAndWasCancelled({
      clientId,
      propId,
      wasCancelled: 0,
      startDate,
      endDate,
      });
  } else if (filter === "WP") {
    //Normal MovedOut
    occupancies = await occupancyDB.getTenantsByClientIdAndPropIdAndPendingFnF({
      clientId,
      propId,
      pageNum: Number(pageNum),
      limit,
    });

    tenantCount = await occupancyDB.getTenantsCountByClientIdAndPropIdAndPendingFnF({
      clientId,
      propId,
      });
  } else if (platform === "I" || filter === "I") {
    occupancies = await occupancyDB.getAppInstalledTenantsByClientIdAndPropId({
      clientId,
      propId,
      pageNum: Number(pageNum),
      limit,
    });

    tenantCount =
      await occupancyDB.getAppInstalledTenantsCountByClientIdAndPropId({
        clientId,
        propId,
      });
  } else if (platform === "NI" || filter === "NI") {
    occupancies =
      await occupancyDB.getAppNotInstalledTenantsByClientIdAndPropId({
        clientId,
        propId,
        pageNum: Number(pageNum),
        limit,
      });

    tenantCount =
      await occupancyDB.getAppNotInstalledTenantsCountByClientIdAndPropId({
        clientId,
        propId,
      });
  } else if (filter === "RS") {
    if (property.type === CONSTANTS.PROPERTY_TYPE.FLAT) {
      occupancies = await occupancyDB.getByClientIdAndPropIdSortByRoomFlat({
        clientId,
        propId,
        pageNum: Number(pageNum),
        limit,
      });
    } else {
      occupancies = await occupancyDB.getByClientIdAndPropIdSortByRoomPg({
        clientId,
        propId,
        pageNum: Number(pageNum),
        limit,
      });
    }
    tenantCount = await occupancyDB.getCountByClientIdAndPropId({
      clientId,
      propId,
    });
  } else if (filter === "PV") {
    //Police Verification 
    occupancies = await occupancyDB.getPVCompletedTenantsForProp({
      clientId,
      pageNum: Number(pageNum),
      limit,
      propId: propId,
    });

    tenantCount = await occupancyDB.getPVCompletedTenantsCountForProp({
      clientId,
      propId: propId,
    });
  } else if (filter === "PVP") {
    //Police Verification Pending
    occupancies = await occupancyDB.getPVPendingTenantsForProp({
      clientId,
      pageNum: Number(pageNum),
      propId: propId,
      limit,
    });

    tenantCount = await occupancyDB.getPVPendingTenantsCountForProp({
      clientId,
      propId: propId,
    });
  } else if (filter === "RA") {
    //Rent Agreement done
    occupancies = await occupancyDB.getRASignedTenantsForProp({
      clientId,
      pageNum: Number(pageNum),
      limit,
      propId: propId,
    });

    tenantCount = await occupancyDB.getRASignedTenantsCountForProp({
      clientId,
      propId: propId,
    });
  } else if (filter === "RAP") {
    //Rent Argeement Pending
    occupancies = await occupancyDB.getRAPendingTenantsForProp({
      clientId,
      pageNum: Number(pageNum),
      limit,
      propId: propId,
    });

    tenantCount = await occupancyDB.getRAPendingTenantsCountForProp({
      clientId,
      propId: propId,
    });
  } else if (filter === "RB") {
    //Rental Bond done
    occupancies = await occupancyDB.getRentalBondAvailedTenantsForProp({
      clientId,
      pageNum: Number(pageNum),
      limit,
      propId: propId,
    });

    tenantCount = await occupancyDB.getRentalBondAvailedTenantsCountForProp({
      clientId,
      propId: propId,
    });
  } else if (filter === "RBN") {
    //Rental bond Not done
    occupancies = await occupancyDB.getRentalBondNotAvailedTenantsForProp({
      clientId,
      pageNum: Number(pageNum),
      limit,
      propId: propId,
    });

    tenantCount = await occupancyDB.getRentalBondNotAvailedTenantsCountForProp({
      clientId,
      propId: propId,
    });
  } else if (filter === "RBNA") {
    //Rental Bond Not avaliable done
    occupancies = await occupancyDB.getRentalBondNotAllowedTenantsForProp({
      clientId,
      pageNum: Number(pageNum),
      limit,
      propId: propId,
    });

    tenantCount = await occupancyDB.getRentalBondNotAllowedTenantsCountForProp({
      clientId,
      propId: propId,
    });
  } else if (filter === "ZS") {
    //Zero Security
    occupancies = await occupancyDB.getZeroDepositTenantsByClientIdAndPropId({
      clientId,
      pageNum: Number(pageNum),
      limit,
      propId: propId,
    });

    tenantCount = await occupancyDB.getZeroDepositTenantsCountByClientIdAndPropId({
      clientId,
      propId: propId,
    });
  } else if (filter === "1RS") {
    //1 Month Rent = Security
    occupancies = await occupancyDB.getNonZeroDepositTenantsByClientIdAndPropId({
      clientId,
      pageNum: Number(pageNum),
      limit,
      propId: propId,
      rentMultiplier: 1,
    });

    tenantCount = await occupancyDB.getNonZeroDepositTenantsCountByClientIdAndPropId({
      clientId,
      propId: propId,
      rentMultiplier: 1,
    });
  } else if (filter === "1.5RS") {
    //1.5 Month Rent = Security
    occupancies = await occupancyDB.getNonZeroDepositTenantsByClientIdAndPropId({
      clientId,
      pageNum: Number(pageNum),
      limit,
      propId: propId,
      rentMultiplier: 1.5,
    });

    tenantCount = await occupancyDB.getNonZeroDepositTenantsCountByClientIdAndPropId({
      clientId,
      propId: propId,
      rentMultiplier: 1.5,
    });
  } else if (filter === "2RS") {
    //2 Month Rent = Security
    occupancies = occupancyDB.getNonZeroDepositTenantsByClientIdAndPropId({
      clientId,
      pageNum: Number(pageNum),
      limit,
      propId: propId,
      rentMultiplier: 2,
    });

    tenantCount = await occupancyDB.getNonZeroDepositTenantsCountByClientIdAndPropId({
      clientId,
      propId: propId,
      rentMultiplier: 2,
    });
  } else if (filter === "SS") {
    //Short Stay
    occupancies = await occupancyDB.getByClientIdAndPropIdAndStayType({
      clientId,
      propId,
      pageNum: Number(pageNum),
      limit,
      stayType: CONSTANTS.STAY_TYPE.SHORT,
    });

    tenantCount = await occupancyDB.getCountByClientIdAndPropIdAndStayType({
      clientId,
      propId,
      stayType: CONSTANTS.STAY_TYPE.SHORT,
    });
  } else if (filter === "TB") {
    occupancies = await occupancyDB.getByClientIdAndPropIdTodayBooking({
      clientId,
      propId,
      pageNum: Number(pageNum),
      limit,
    });

    tenantCount = await occupancyDB.getCountByClientIdAndPropIdTodayBooking({
      clientId,
      propId,
    });
  }  else if (filter === "OE") {
    //Online Enabled
    occupancies = await occupancyDB.getByClientIdAndPropIdOnlineEnabled({
      clientId,
      propId,
      pageNum: Number(pageNum),
      limit,
    });
    tenantCount = await occupancyDB.getCountByClientIdAndPropIdOnlineEnabled({
      clientId,
      propId,
    });
  } else if (filter === "ONE") {
    //Online Not Enabled
    occupancies = await occupancyDB.getByClientIdAndPropIdOnlineNotEnabled({
      clientId,
      propId,
      pageNum: Number(pageNum),
      limit,
    });
    tenantCount = await occupancyDB.getCountByClientIdAndPropIdOnlineNotEnabled({
      clientId,
      propId,
    });
  } else if (filter === "GM") {
    //Gender Male
    occupancies = await occupancyDB.getByClientIdAndPropIdAndGender({
      clientId,
      propId,
      pageNum: Number(pageNum),
      limit,
      gender: CONSTANTS.GENDER.MALE,
    });
    tenantCount = await occupancyDB.getCountByClientIdAndPropIdAndGender({
      clientId,
      propId,
      gender: CONSTANTS.GENDER.MALE,
    });
  } else if (filter === "GF") {
    //Gender Female
    occupancies = await occupancyDB.getByClientIdAndPropIdAndGender({
      clientId,
      propId,
      pageNum: Number(pageNum),
      limit,
      gender: CONSTANTS.GENDER.FEMALE,
    });
    tenantCount = await occupancyDB.getCountByClientIdAndPropIdAndGender({
      clientId,
      propId,
      gender: CONSTANTS.GENDER.FEMALE,
    });
  } else if (filter === "GO") {
    //Gender Other
    occupancies = await occupancyDB.getByClientIdAndPropIdAndGender({
      clientId,
      propId,
      pageNum: Number(pageNum),
      limit,
      gender: CONSTANTS.GENDER.OTHER,
    });
    tenantCount = await occupancyDB.getCountByClientIdAndPropIdAndGender({
      clientId,
      propId,
      gender: CONSTANTS.GENDER.OTHER,
    });
  } else if (filter === "BY") {
    //Booked By
    let filterValArr: any = [];
    if (filterVal && typeof filterVal === 'string') {
      filterValArr = filterVal.split(',').map(v => v.trim()).filter(Boolean);
    }
    occupancies = await occupancyDB.getByClientIdAndPropIdAndBookedBy({
      clientId,
      propId,
      pageNum: Number(pageNum),
      limit,
      filterVal: filterValArr,
    });
    tenantCount = await occupancyDB.getCountByClientIdAndPropIdAndBookedBy({
      clientId,
      propId,
      filterVal: filterValArr,
    });
  } else {

    if (property.type === CONSTANTS.PROPERTY_TYPE.FLAT) {
      occupancies = await occupancyDB.getByPropIdForWebSortByRoomFlat({
        propId,
      });
    } else {
      occupancies = await occupancyDB.getByPropIdForWebSortByRoomPg({
        propId,
      });
    }

    tenantCount = await occupancyDB.getCountByClientIdAndPropId({
      clientId,
      propId,
    });
  }

  return { tenantTotalCount: tenantCount, tenantOccupancies: occupancies };
};
