import CONSTANTS from "../../config/constants";
import requestDB from "../../models/request.model";
import occupancyDB from "../../models/occupancy.model";
import expenseDB from "../../models/expense.model";
import complaintDB from "../../models/complaint.model";
import leadDB from "../../models/lead.model";
import propertiesTypes from "../../schemas/property.schema";
import propertyDB from "../../models/property.model";
import log from "../../config/log";

export const ClientPendingTasks = async (
  clientId: number,
  platform: number = CONSTANTS.TENANT_DEVICE_TYPE.ANDROID,
) => {
    let moveInRequests = await requestDB.getPendingReqByClientIdWithType({
      clientId,
      type: CONSTANTS.REQUEST_TYPE.ROOM_SELECTION,
    });
    
    let moveOutRequests = await requestDB.getPendingReqByClientIdWithType({
        clientId,
        type: CONSTANTS.REQUEST_TYPE.MOVE_OUT,
    });

    let guestRequests = await requestDB.getPendingReqByClientIdWithType({
        clientId,
        type: CONSTANTS.REQUEST_TYPE.GUEST,
    });

    let refundRequests = await requestDB.getPendingReqByClientIdWithType({
        clientId,
        type: CONSTANTS.REQUEST_TYPE.REFUND,
    });

    // let moveInVerificationPending = await occupancyDB.getUnverifiedTenantsCountByClientId({
    //   clientId,
    // });

    let unPaidExpenseCount = await expenseDB.getCountUnMarkedByClientId({
      clientId
    });

    let complaintCounts = await complaintDB.getTenantAndPropPendingComplaintCountsForClient({
      clientId
    });

    let unassignedLeadsCount = await leadDB.getUnAssignedLeads({
      clientId
    });

    let landlordPaymentsPending = await expenseDB.getCountUnMarkedByClientIdForLandlord({
      clientId
    });
    let tenantAgreementExpiredCount = 0;
    if(platform !== CONSTANTS.TENANT_DEVICE_TYPE.WEB) {
      tenantAgreementExpiredCount = await occupancyDB.getTenantWhoseAgrementExpiringCount({
        clientId
      });
    } else {
      tenantAgreementExpiredCount = await occupancyDB.getTenantWhoseAgrementExpiringCountCurMonth({
        clientId
      });
    }
    
    let reservedTenants = await occupancyDB.getReservedCountForClient({
      clientId
    });

    let moveOutPending = await occupancyDB.getMoveOutPendingCountForClient({
      clientId,
    });

    let unAssignedTenantComplaints = 0;
    let unAssignedPropComplaints = 0;
    if (complaintCounts) {
      unAssignedTenantComplaints = complaintCounts?.tenantPending || 0;
      unAssignedPropComplaints = complaintCounts?.propPending || 0;
    }
    
    let totalPendingTasks = moveInRequests + moveOutRequests + guestRequests + refundRequests + unPaidExpenseCount + unAssignedTenantComplaints + unAssignedPropComplaints + landlordPaymentsPending + tenantAgreementExpiredCount + unassignedLeadsCount + reservedTenants + moveOutPending;

    let tenantActions = {
      //pendingMoveIns: reservedTenants,
      moveInRequests: moveInRequests,
      moveInVerificationPending: reservedTenants,
      moveOutRequests: moveOutRequests,
      guestRequests: guestRequests,
      moveOutPending: moveOutPending,
    }
    let financeActions = {
      landlordPaymentsPending: landlordPaymentsPending || 0,
      refundRequests: refundRequests,
      unpaidExpences: unPaidExpenseCount,
    }
    let operationActions = {
      unAssignedTenantComplaints: unAssignedTenantComplaints,
      unAssignedPropComplaints: unAssignedPropComplaints,
      agreementExpired: tenantAgreementExpiredCount,
      unAssignedLeads: unassignedLeadsCount || 0,
    }

    return {
      totalPendingTasks,
      tenantActions,
      financeActions,
      operationActions
    };
};

export const StaffPendingTasks = async (
  clientId: number,
  staffId: number,
  platform: number = CONSTANTS.TENANT_DEVICE_TYPE.ANDROID,
) => {

    
    let moveInRequests = 0;
    
    let moveOutRequests = 0;

    let guestRequests = 0;

    let refundRequests = 0;
    let moveOutPending = 0;

    let complaintCounts = {
      tenantPending: 0,
      propPending: 0,
    };

    let unassignedLeadsCount = 0

    //let moveInVerificationPending = 0;
    let unPaidExpenseCount = 0
    let landlordPaymentsPending = 0;
    let tenantAgreementExpiredCount = 0;
    let reservedTenants = 0;
    const staffLinkedProps = await propertyDB.getPropsByStaffId({
        staffId,
    });
    
    if (staffLinkedProps) {
      const propertiesIds = staffLinkedProps
        .map((prop: propertiesTypes) => prop.id)
        .join(",");
      // moveInVerificationPending = await occupancyDB.getUnverifiedTenantsCountForStaff({
      //     clientId,
      //     propertiesIds,
      // });
      unPaidExpenseCount = await expenseDB.getCountUnMarkedByClientIdAndDateRangeAndPropFilterForStaff({
        clientId,
        propertiesIds
      });

      landlordPaymentsPending = await expenseDB.getCountUnMarkedLandlordExpensePropFilterForStaff({
        clientId,
        propertiesIds
      });

      // tenantAgreementExpiredCount = await occupancyDB.getTenantWhoseAgrementExpiringCountForStaff({
      //   clientId,
      //   propertiesIds
      // });

      //let tenantAgreementExpiredCount = 0;
      if(platform !== CONSTANTS.TENANT_DEVICE_TYPE.WEB) {
        tenantAgreementExpiredCount = await occupancyDB.getTenantWhoseAgrementExpiringCountForStaff({
          clientId,
          propertiesIds
        });
      } else {
        // tenantAgreementExpiredCount = await occupancyDB.getTenantWhoseAgrementExpiringCountCurMonth({
        //   clientId
        // });
        tenantAgreementExpiredCount = await occupancyDB.getTenantWhoseAgrementExpiringCountForStaffCurMonth({
        clientId,
        propertiesIds
      });
      }

      reservedTenants = await occupancyDB.getReservedCountForClientStaffs({
        clientId,
        propertiesIds
      });

      moveInRequests = await requestDB.getPendingReqByClientIdWithTypeForStaff({
        clientId,
        type: CONSTANTS.REQUEST_TYPE.ROOM_SELECTION,
        propertiesIds
      });
      
      moveOutRequests = await requestDB.getPendingReqByClientIdWithTypeForStaff({
        clientId,
        type: CONSTANTS.REQUEST_TYPE.MOVE_OUT,
        propertiesIds
      });

      guestRequests = await requestDB.getPendingReqByClientIdWithTypeForStaff({
        clientId,
        type: CONSTANTS.REQUEST_TYPE.GUEST,
        propertiesIds
      });

      refundRequests = await requestDB.getPendingReqByClientIdWithTypeForStaff({
        clientId,
        type: CONSTANTS.REQUEST_TYPE.REFUND,
        propertiesIds
      });

      complaintCounts = await complaintDB.getTenantAndPropPendingComplaintCountsForStaff({
        clientId,
        propertiesIds,
      });

      unassignedLeadsCount = await leadDB.getUnAssignedLeads({
        clientId,
      });

      moveOutPending = await occupancyDB.getMoveOutPendingCountForStaff({
        clientId,
        propertiesIds,
      });
    }

    let unAssignedTenantComplaints = 0;
    let unAssignedPropComplaints = 0;
    if (complaintCounts) {
      unAssignedTenantComplaints = complaintCounts?.tenantPending || 0;
      unAssignedPropComplaints = complaintCounts?.propPending || 0;
    }
    
    let totalPendingTasks = moveInRequests + moveOutRequests + guestRequests + refundRequests + unPaidExpenseCount + unAssignedTenantComplaints + unAssignedPropComplaints + landlordPaymentsPending + tenantAgreementExpiredCount + unassignedLeadsCount + reservedTenants + moveOutPending;

    let tenantActions = {
      //pendingMoveIns: reservedTenants,
      moveInRequests: moveInRequests,
      moveInVerificationPending: reservedTenants,
      moveOutRequests: moveOutRequests,
      guestRequests: guestRequests,
      moveOutPending: moveOutPending,
    }
    let financeActions = {
      landlordPaymentsPending: landlordPaymentsPending || 0,
      refundRequests: refundRequests,
      unpaidExpences: unPaidExpenseCount,
    }
    let operationActions = {
      unAssignedTenantComplaints: unAssignedTenantComplaints,
      unAssignedPropComplaints: unAssignedPropComplaints,
      agreementExpired: tenantAgreementExpiredCount,
      unAssignedLeads: unassignedLeadsCount || 0,
    }

    return {
      totalPendingTasks,
      tenantActions,
      financeActions,
      operationActions
    };
};

export default ClientPendingTasks;
