'use client';

import { useTranslations } from 'next-intl';
import { CalendarDays } from 'lucide-react';
import { Card } from '@/components/ui/shared/Card';
import { ScheduleDayPicker } from '@/components/ui/shared/ScheduleDayPicker';
import { startOfLocalDay } from '@/components/appointments/appointmentTime';
import { DayStripCard } from '@/components/ui/treatment/DayStripCard';
import type { DayStripItem } from '@/components/treatment/dayStrip';
import type { TreatmentCatalogEntry } from '@/types/treatment-catalog';

interface AppointmentsStripProps {
  stripHidden: boolean;
  onToggleStripHidden: () => void;
  selectedDay: Date;
  onSelectDay: (day: Date) => void;
  items: DayStripItem[];
  selectedItemId: string | null;
  onSelectItem: (item: DayStripItem) => void;
  onDeleteUnscheduled?: (item: DayStripItem) => void;
  treatmentCatalog: TreatmentCatalogEntry[];
  loading?: boolean;
}

export function AppointmentsStrip({
  stripHidden,
  onToggleStripHidden,
  selectedDay,
  onSelectDay,
  items,
  selectedItemId,
  onSelectItem,
  onDeleteUnscheduled,
  treatmentCatalog,
  loading = false,
}: AppointmentsStripProps) {
  const t = useTranslations('treatment');

  if (stripHidden) {
    return (
      <Card className="flex items-center justify-between gap-3 flex-wrap" padding="sm">
        <p className="text-sm text-text-secondary">{t('hiddenMessage')}</p>
        <button
          type="button"
          onClick={onToggleStripHidden}
          className="text-sm font-medium text-primary hover:underline focus:outline-none focus-visible:ring-2 focus-visible:ring-primary/40 rounded-[var(--radius-sm)]"
        >
          {t('showAppointments')}
        </button>
      </Card>
    );
  }

  const timed = items.filter((item) => item.kind === 'appointment');
  const unscheduled = items.filter((item) => item.kind === 'unscheduled');

  return (
    <Card className="space-y-4">
      <div className="flex items-center justify-between gap-3 flex-wrap">
        <div className="flex items-center gap-2 min-w-0">
          <CalendarDays className="w-5 h-5 text-text-muted shrink-0 icon-flat" aria-hidden />
          <h2 className="text-sm font-semibold text-text-primary truncate">{t('appointmentsTitle')}</h2>
        </div>
        <button
          type="button"
          onClick={onToggleStripHidden}
          className="text-sm font-medium text-primary hover:underline focus:outline-none focus-visible:ring-2 focus-visible:ring-primary/40 rounded-[var(--radius-sm)]"
        >
          {t('hideAppointments')}
        </button>
      </div>

      <div className="flex flex-col sm:flex-row sm:items-end gap-4 sm:justify-between">
        <ScheduleDayPicker
          value={selectedDay}
          onChange={(d) => onSelectDay(startOfLocalDay(d))}
        />
        {loading && (
          <p className="text-sm text-text-muted pb-2">{t('loadingAppointments')}</p>
        )}
      </div>

      <div className="space-y-3">
        <div className="flex flex-wrap gap-2">
          {timed.length === 0 && unscheduled.length === 0 && (
            <p className="text-sm text-text-muted">{t('emptyDay')}</p>
          )}
          {timed.map((item) => (
            <DayStripCard
              key={`${item.kind}-${item.id}`}
              item={item}
              selected={item.id === selectedItemId}
              treatmentCatalog={treatmentCatalog}
              onSelect={() => onSelectItem(item)}
            />
          ))}
        </div>
        {unscheduled.length > 0 ? (
          <div className="space-y-2">
            <p className="text-[10px] uppercase tracking-wide text-text-muted">{t('unscheduledHeading')}</p>
            <div className="flex flex-wrap gap-2">
              {unscheduled.map((item) => (
                <DayStripCard
                  key={`${item.kind}-${item.id}`}
                  item={item}
                  selected={item.id === selectedItemId}
                  treatmentCatalog={treatmentCatalog}
                  onSelect={() => onSelectItem(item)}
                  canDelete={item.canDelete}
                  onDelete={
                    item.canDelete && onDeleteUnscheduled
                      ? () => onDeleteUnscheduled(item)
                      : undefined
                  }
                  deleteAriaLabel={t('deleteEmptyTreatment')}
                />
              ))}
            </div>
          </div>
        ) : null}
      </div>
    </Card>
  );
}
