"use client";

import { type ConsultationTimeSlot } from "./consultationTypes";
import { clickable } from "@/lib/a11y";

interface ConsultationSlotGridProps {
  selectedDate: string | null;
  timeSlotsByDate: Record<string, ConsultationTimeSlot[]>;
  selectedSlotId: number | null | undefined;
  onSelect: (slotId: number) => void;
  formatTime: (timeString: string | null) => string;
}

export default function ConsultationSlotGrid({
  selectedDate,
  timeSlotsByDate,
  selectedSlotId,
  onSelect,
  formatTime,
}: ConsultationSlotGridProps) {
  if (!selectedDate) return null;

  return (
    <div className="mb-4 flex justify-center">
        <div   className={`grid ${ timeSlotsByDate[selectedDate]?.length === 1 ? "grid-cols-1 justify-items-center": "grid-cols-1 md:grid-cols-2" } gap-4`} >
          {(timeSlotsByDate[selectedDate] ?? []).map((slot) => (
            <div
              key={slot.id}
              className={`rounded-[15px] py-4 px-8 cursor-pointer text-center h-[84px] mobilescreen:h-[65px] mobilescreen:w-[220px] w-[252px] flex items-center justify-center extrasmall:w-auto ${
                selectedSlotId === slot.id
                  ? "bg-primary-5 text-white extrasmall:text-base"
                  : "bg-[#EFF0F6] text-[#181822CC]/80 extrasmall:text-base"
              }`}
              {...clickable(() => onSelect(slot.id))}
            >
              <p className="text-lg mobilescreen:text-sm">
                {`${formatTime(slot.start_time)} - ${formatTime(slot.end_time)}`}
              </p>
            </div>
          ))}
        </div>
    </div>
  );
}
