"use client";

import type { TableColumn } from "@/components/ui/Table";
import { emergencyContactColumns } from "./registrationEmergencyColumns";
import { type EmergencyContactSource } from "@/features/opportunities/registrationEmergencyContact";

/**
 * The volunteering registrations table's columns.
 *
 * Three groups are conditional, and each condition is a real rule:
 *
 *  - the attendance checkbox is always present but **disabled** unless manual
 *    check-in is open, so the organizer can see the state without being able to
 *    change it after the deadline;
 *  - the four guardian columns appear only when a loaded row is a
 *    minor-at-registration (shared with the other two registration lists);
 *  - logged hours and the undo affordance only make sense where check-in happens
 *    at all — workshops and consultations skip both.
 */
export function buildVolunteerRegistrationsColumns({
  t,
  registrations,
  selectAllAttendance,
  onSelectAllAttendanceChange,
  manual_tracking,
  requiresCheckIn,
}: {
  t: (key: string) => string;
  registrations: EmergencyContactSource[];
  selectAllAttendance: boolean;
  onSelectAllAttendanceChange: () => void;
  manual_tracking: boolean;
  requiresCheckIn: boolean;
}): TableColumn[] {
  return [
    {
      label: (
        <div className="flex items-center justify-center gap-2">
          <label className="cursor-pointer flex items-center gap-2">
            <div className="border p-1 border-secondary-100">
              <div
                className={`h-4 w-4 relative ${
                  selectAllAttendance ? "bg-[#373737BF]/75" : "bg-white"
                }`}
              >
                <input
                  type="checkbox"
                  checked={selectAllAttendance}
                  onChange={onSelectAllAttendanceChange}
                  aria-label={t("COMMON.SELECT_ALL")}
                  className="absolute inset-0 w-full h-full opacity-0 cursor-pointer"
                  disabled={!manual_tracking}
                />
              </div>
            </div>
          </label>
        </div>
      ),
      key: "attendance",
      type: "attendance",
    },
    { label: t("COMMON.VOLUNTEER.NAME"), key: "full_name", type: "custom" },
    { label: t("COMMON.EMAIL"), key: "user_email" },
    { label: t("COMMON.CONTACT_NUMBER"), key: "phone_number" },
    ...emergencyContactColumns(t, registrations),
    { label: t("COMMON.ROLE"), key: "role", type: "role" },
    // Logged hours and the undo affordance only make sense where check-in
    // happens at all — workshops and consultations skip both.
    ...(requiresCheckIn
      ? [
          {
            label: t("COMMON.ATTENDANCE_HOURS"),
            key: "attendance_hours",
            type: "attendance_hours",
          },
        ]
      : []),
    { label: t("COMMON.ACTION"), key: "actions", type: "actions" },
  ]
}
