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 with small "Origin Date" /
 * "Request Date" labels above each date. The two dates stack with a downward
 * arrow on mobile and sit side by side with a rightward arrow from sm up.
 * Falls back to the current date only when there is no previous date or when
 * it matches the current one.
 */
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(
        'flex flex-col gap-2 sm:flex-row sm:flex-wrap sm:items-start sm:gap-x-3',
        className,
      )}
    >
      <span className="inline-flex flex-col gap-0.5">
        <span className="text-xs font-semibold uppercase text-red-600">
          Origin Date
        </span>
        <span className="whitespace-nowrap font-medium text-slate-900">
          {formattedPreviousDate}
        </span>
      </span>

      <FiArrowRight
        className="h-4 w-4 shrink-0 rotate-90 self-start text-slate-400 sm:mt-4 sm:rotate-0 sm:self-auto"
        aria-hidden="true"
      />

      <span className="inline-flex flex-col gap-0.5">
        <span className="text-xs font-semibold uppercase text-green-600">
          Request Date
        </span>
        <span className="whitespace-nowrap font-semibold text-slate-900">
          {formattedCurrentDate}
        </span>
      </span>
    </span>
  )
}
