/*
 * File: hospitalSelectors.ts
 * Description: Redux selectors for Hospital state
 * Design & Developed by Tech4Biz Solutions
 * Copyright (c) Spurrin Innovations. All rights reserved.
 */

import { createSelector } from '@reduxjs/toolkit';
import { RootState } from '../../../store';

// ============================================================================
// BASE SELECTORS
// ============================================================================

/**
 * Select Hospital State
 * 
 * Purpose: Get the entire hospital state from Redux store
 */
export const selectHospitalState = (state: RootState) => state.hospital;

/**
 * Select Hospitals List
 * 
 * Purpose: Get the list of hospitals
 */
export const selectHospitals = (state: RootState) => state.hospital.hospitals;

/**
 * Select Hospital Loading State
 * 
 * Purpose: Get the loading state for hospital operations
 */
export const selectHospitalLoading = (state: RootState) => state.hospital.loading;

/**
 * Select Hospital Error
 * 
 * Purpose: Get any error from hospital operations
 */
export const selectHospitalError = (state: RootState) => state.hospital.error;

// ============================================================================
// DERIVED SELECTORS
// ============================================================================

/**
 * Select Hospitals Count
 * 
 * Purpose: Get the total number of hospitals
 */
export const selectHospitalsCount = createSelector(
  [selectHospitals],
  (hospitals) => hospitals?.length || 0
);

/**
 * Select Hospital by ID
 * 
 * Purpose: Get a specific hospital by its ID
 */
export const selectHospitalById = createSelector(
  [selectHospitals, (_state: RootState, hospitalId: string) => hospitalId],
  (hospitals, hospitalId) => 
    hospitals?.find(hospital => hospital.hospital_id === hospitalId) || null
);

/**
 * Select Hospital Names
 * 
 * Purpose: Get an array of hospital names for display
 */
export const selectHospitalNames = createSelector(
  [selectHospitals],
  (hospitals) => 
    hospitals?.map(hospital => hospital.hospital_name).filter(Boolean) || []
);

/**
 * Select Hospitals for Dropdown
 * 
 * Purpose: Get hospitals formatted for dropdown/select components
 */
export const selectHospitalsForDropdown = createSelector(
  [selectHospitals],
  (hospitals) => 
    hospitals?.map(hospital => ({
      label: hospital.hospital_name || 'Unknown Hospital',
      value: hospital.hospital_id || '',
    })).filter(item => item.value) || []
);

/**
 * Select Filtered Hospitals
 * 
 * Purpose: Get hospitals filtered by search term
 */
export const selectFilteredHospitals = createSelector(
  [selectHospitals, (_state: RootState, searchTerm: string) => searchTerm],
  (hospitals, searchTerm) => {
    if (!searchTerm.trim()) return hospitals || [];
    
    const term = searchTerm.toLowerCase();
    return hospitals?.filter(hospital => 
      hospital.hospital_name?.toLowerCase().includes(term)
    ) || [];
  }
);

/**
 * Select Hospital Loading Status
 * 
 * Purpose: Get comprehensive loading status for hospital operations
 */
export const selectHospitalStatus = createSelector(
  [selectHospitalLoading, selectHospitalError, selectHospitalsCount],
  (loading, error, count) => ({
    loading,
    error,
    hasData: count > 0,
    isEmpty: count === 0,
  })
);

/*
 * End of File: hospitalSelectors.ts
 * Design & Developed by Tech4Biz Solutions
 * Copyright (c) Spurrin Innovations. All rights reserved.
 */ 