"use client";

import { FiEdit } from "react-icons/fi";
import { RiDeleteBin5Fill } from "react-icons/ri";
import { useTranslation } from "react-i18next";
import type { EventTimeSlotDisplayRow } from "./useEventTimeSlots";

/**
 * The saved time slots, one table per date.
 *
 * Grouping is the hook's job; this renders the groups and raises the edit and
 * delete intents back to it.
 */
export default function EventTimeSlotTable({
  groupedTimeSlots,
  windowWidth,
  onEdit,
  onDelete,
}: {
  groupedTimeSlots: Record<string, EventTimeSlotDisplayRow[]>;
  windowWidth: number;
  /* Both take the slot's id — the hook looks the slot up itself. */
  onEdit: (slotId: number) => void;
  onDelete: (slotId: number) => void;
}) {
  const { t } = useTranslation();

  return (
    <>
        {Object.entries(groupedTimeSlots).map(([date, slots], dateIndex) => (
          <div key={date} className={`mb-0 ${dateIndex > 0 ? "pt-6" : ""}`}>
            <table className="w-full border-collapse min-w-[500px]">
              <thead>
                <tr className="border-b border-gray-200">
                  <th className="text-start py-3 px-4 w-1/4 text-gray-600">
                    {t("COMMON.DATE")}
                  </th>
                  <th className="text-start py-3 px-4 w-1/4 text-gray-600">
                    {date}
                  </th>
                  <th className="text-center py-3 px-4 w-1/4 text-gray-600">
                    {t("COMMON.NEEDED")}
                  </th>
                  <th className="text-center py-3 px-4 w-1/4 text-gray-600">
                    {t("COMMON.ACTION")}
                  </th>
                </tr>
              </thead>
              <tbody>
                {/* No cast: `slots` is `EventTimeSlotDisplayRow[]`, the view
                    model `useEventTimeSlots` builds. */}
                {slots.map((slot, index) => (
                  <tr key={slot.id} className="border-b border-gray-200">
                    <td className="py-3 px-4 text-gray-600">{index + 1}-</td>
                    <td className="py-3 px-4 miniscreen8:text-[14px] xss2:text-[13px]">
                      {slot.time}
                    </td>
                    <td className="py-3 px-4 text-center">
                      <span className="w-[59px] bg-[#29246D08]/5 text-primary-4 bg-primary-5/10 border border-primary-5/10 rounded-xl px-3 py-1 inline-block">
                        {slot.needed}
                      </span>
                    </td>
                    <td className="py-3 px-4 text-center">
                      <div className="flex items-center justify-center gap-4 mobilescreen:gap-2">
                        <button
                          className="text-primary-5 cursor-pointer"
                          onClick={() => onEdit(slot.id)}
                        >
                          <FiEdit size={windowWidth <= 767 ? 18 : 20} />
                        </button>
                        <button
                          className="text-primary-5 cursor-pointer"
                          onClick={() => onDelete(slot.id)}
                        >
                          <RiDeleteBin5Fill
                            size={windowWidth <= 767 ? 18 : 20}
                          />
                        </button>
                      </div>
                    </td>
                  </tr>
                ))}
              </tbody>
            </table>
          </div>
        ))}
    </>
  );
}
