import { FiArrowRight } from 'react-icons/fi'

import { formatDateToLongFormat } from '@/lib/utils/date'
import { cn } from '@/lib/utils/tw-merge'

interface ScheduleDateChangeProps {
  currentDate?: string | number | Date | null
  previousDate?: string | number | Date | null
  fallback?: string
  className?: string
}

/**
 * Renders the previous and current schedule side by side as
 * "previous date -> current date". Falls back to the current date only
 * when there is no previous date or when it matches the current one.
 * The previous date and arrow stay together while the current date can
 * wrap below them on small screens.
 */
export default function ScheduleDateChange({
  currentDate,
  previousDate,
  fallback = '-',
  className,
}: ScheduleDateChangeProps) {
  const formattedCurrentDate =
    currentDate == null || currentDate === ''
      ? fallback
      : formatDateToLongFormat(currentDate)

  const formattedPreviousDate =
    previousDate == null || previousDate === ''
      ? null
      : formatDateToLongFormat(previousDate)

  if (
    formattedPreviousDate === null ||
    formattedPreviousDate === '-' ||
    formattedPreviousDate === formattedCurrentDate
  ) {
    return (
      <span className={cn('font-medium', className)}>
        {formattedCurrentDate}
      </span>
    )
  }

  return (
    <span
      className={cn(
        'inline-flex flex-wrap items-center gap-x-2 gap-y-1',
        className,
      )}
      title={`Previous schedule: ${formattedPreviousDate}`}
    >
      <span className="inline-flex items-center gap-2 whitespace-nowrap font-medium text-slate-400">
        {formattedPreviousDate}
        <FiArrowRight className="h-4 w-4 shrink-0" aria-hidden="true" />
      </span>
      <span className="font-medium">{formattedCurrentDate}</span>
    </span>
  )
}
