export const COLLECTION_STAGES = [
  'initial_notice',
  'official_notice',
  'final_notice',
  'notice_of_late_assessment',
  'late_assessment',
  'intent_to_lien',
  'lien_filed',
  'with_attorney',
  'foreclosure',
  'payment_plan',
  'bankruptcy'
];

export const STAGE_LABELS = {
  'initial_notice': 'Initial Notice',
  'official_notice': 'Official Notice',
  'final_notice': 'Final Notice',
  'notice_of_late_assessment': 'Notice of Late Assessment',
  'late_assessment': 'Late Assessment',
  'intent_to_lien': 'Intent to Lien',
  'lien_filed': 'Lien Filed',
  'with_attorney': 'With Attorney',
  'foreclosure': 'Foreclosure',
  'payment_plan': 'Payment Plan',
  'bankruptcy': 'Bankruptcy Hold'
};

export const STAGE_TO_STATUS_MAP = {
  'initial_notice': 'Initial Notice Sent',
  'official_notice': 'Official Notice Sent',
  'final_notice': 'Final Notice Sent',
  'notice_of_late_assessment': 'Notice of Late Assessment',
  'late_assessment': 'Notice of Late Assessment',
  'intent_to_lien': 'Notice of Intent to Lien',
  'lien_filed': 'Lien Filed',
  'with_attorney': 'With Attorney',
  'foreclosure': 'Foreclosure In Process',
  'payment_plan': 'Payment Plan Active',
  'bankruptcy': 'Bankruptcy Hold'
};

export const NON_STAGE_STATUSES = [
  'Active',
  'Resolved',
  'On Hold',
  'Closed'
];

export function getCurrentStage(collection) {
  if (!collection) return COLLECTION_STAGES[0];
  // If stage is defined and valid, return it.
  if (collection.stage && COLLECTION_STAGES.includes(collection.stage)) {
    return collection.stage;
  }
  // Fallback if stage is legacy 'courtesy_notice' but not in list anymore
  if (collection.stage === 'courtesy_notice') {
      return COLLECTION_STAGES[0];
  }
  return COLLECTION_STAGES[0];
}

export function getNextStage(currentStage) {
  // Handle legacy mapping for transition calculation
  if (currentStage === 'courtesy_notice') return 'official_notice';
  
  const currentIndex = COLLECTION_STAGES.indexOf(currentStage);
  
  // If stage not found or is the last one, return null
  if (currentIndex === -1 || currentIndex >= COLLECTION_STAGES.length - 1) {
    return null;
  }
  
  return COLLECTION_STAGES[currentIndex + 1];
}

export function isValidStage(stage) {
  return COLLECTION_STAGES.includes(stage);
}

export function getRemainingStages(currentStage) {
  const currentIndex = COLLECTION_STAGES.indexOf(currentStage);
  if (currentIndex === -1) return COLLECTION_STAGES;
  return COLLECTION_STAGES.slice(currentIndex + 1);
}

/**
 * Robustly retrieves the date a stage was reached, handling both legacy Object maps and new Array histories.
 */
export function getStageHistoryDate(collection, stage) {
  if (!collection?.stage_history) return null;
  
  const history = collection.stage_history;
  
  // Handle Array format (New Schema)
  if (Array.isArray(history)) {
    // Look for entry where 'stage' matches, or 'new_stage' matches
    const entry = history.find(h => h.stage === stage || h.new_stage === stage);
    return entry ? (entry.date || entry.changed_at) : null;
  }
  
  // Handle Object format (Legacy Schema)
  if (typeof history === 'object') {
    return history[stage] || null;
  }
  
  return null;
}

export function getCompletedStages(currentStage) {
  const currentIndex = COLLECTION_STAGES.indexOf(currentStage);
  if (currentIndex === -1) return [];
  return COLLECTION_STAGES.slice(0, currentIndex + 1);
}

export function getMergedStageStatus(collection) {
  if (!collection) return '';

  const status = collection.status;
  const stage = collection.stage;

  if (status && status !== 'Active' && NON_STAGE_STATUSES.includes(status)) {
    return status;
  }

  return STAGE_LABELS[stage] || stage || status || 'Initial Notice';
}