import { toTitleCase, replaceUnderscoreWithSpace, formatSwapStatus, formatDateIST, formatDateTimeIST, formatTimeIST } from '../../common/utils/common.util'
import {
  formatApprovalStatus,
  formatPaymentStatus,
  formatInvoiceStatus,
  formatTravelStatus,
  formatRegistrationStatus,
  formatRegistrationStatusApproval,
  formatRegistrationStatusWaitlist,
  formatRegistrationStatusApprovalWaitlist,
  computeSeekerStatus,
} from '../../common/utils/status-formatter.util'

export interface FormatContext {
  allocatedProgramId?: number | null
  isFreeSeat?: boolean
  paymentStatus?: string | null
  subStatus?: string | null
  approvalStatus?: string | null
  reservedLink?: boolean | null
  requiresApproval?: boolean | null
  waitlistApplicable?: boolean | null
}

function titleCaseStatus(value: unknown): string {
  if (!value) return ''
  return toTitleCase(replaceUnderscoreWithSpace(String(value)))
}

export function formatValue(value: unknown, format: string | null = null, context: FormatContext = {}): string | number | null {
  if (value === null || value === undefined) return null

  switch (format) {
    case 'date':
      return formatDateIST(value) || String(value)
    case 'datetime':
      return formatDateTimeIST(value) || String(value)
    case 'time':
      return formatTimeIST(value) || String(value)
    case 'boolean':
      if (value === true  || value === 'true'  || value === 1) return 'Yes'
      if (value === false || value === 'false' || value === 0) return 'No'
      return null
    case 'number':
    case 'currency': {
      const n = Number(value)
      return isNaN(n) ? null : n
    }
    case 'json':
      if (Array.isArray(value)) return value.join(', ')
      if (typeof value === 'object') return JSON.stringify(value)
      return String(value)
    case 'title-case':
      return titleCaseStatus(value)
    case 'registration-status':
      return formatRegistrationStatus(String(value))
    case 'registration-status-approval':
      return formatRegistrationStatusApproval(String(value), context.approvalStatus)
    case 'registration-status-waitlist':
      return formatRegistrationStatusWaitlist(String(value), context.subStatus)
    case 'registration-status-approval-waitlist':
      return formatRegistrationStatusApprovalWaitlist(String(value), context.subStatus, context.approvalStatus)
    case 'seeker-status':
      // Unified seeker status — same logic as the dashboard list
      return computeSeekerStatus({
        registrationStatus: String(value),
        approvalStatus: context.approvalStatus,
        subStatus: context.subStatus,
        reservedLink: context.reservedLink,
        isFreeSeat: context.isFreeSeat,
        paymentStatus: context.paymentStatus,
        requiresApproval: context.requiresApproval,
        waitlistApplicable: context.waitlistApplicable,
      })
    case 'approval-status':
      return formatApprovalStatus(String(value))
    case 'payment-status':
      return formatPaymentStatus(String(value), context.allocatedProgramId ?? null, context.isFreeSeat ?? false)
    case 'invoice-status':
      return formatInvoiceStatus(String(value), context.allocatedProgramId ?? null, context.isFreeSeat ?? false, context.paymentStatus ?? null)
    case 'travel-status':
      return formatTravelStatus(String(value), context.allocatedProgramId ?? null)
    case 'swap-status':
      return formatSwapStatus(value as string | undefined)
    default:
      return value == null ? null : String(value)
  }
}
