"use client";

import type { TableColumn } from "@/components/ui/Table";
import { emergencyContactColumns } from "./registrationEmergencyColumns";
import { type RegisteredUser } from "./learnServeRegisterListTypes";

/**
 * The registrations table's columns.
 *
 * Two of the groups are conditional, and both conditions are the point:
 *
 *  - the attendance column disappears entirely for the learning types that need
 *    no check-in (Workshop, Consultation), rather than rendering a disabled
 *    checkbox nobody can use;
 *  - the four emergency-contact columns appear only when **at least one** loaded
 *    row is a minor-at-registration with contacts on file, so an all-adult list
 *    is not four columns wider for nothing.
 */
export function buildRegisterListColumns({
  t,
  requiresCheckIn,
  rows,
  selectAll,
  onSelectAllChange,
}: {
  t: (key: string) => string;
  requiresCheckIn: boolean;
  rows: RegisteredUser[];
  selectAll: boolean;
  onSelectAllChange: () => void;
}): TableColumn[] {
  return [
    // The select-all/attendance column disappears entirely for the types that
    // need no check-in (Workshop, Consultation).
    ...(requiresCheckIn
      ? [
          {
            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 ${
                        selectAll ? "bg-[#373737BF]/75" : "bg-white"
                      }`}
                    >
                      <input
                        type="checkbox"
                        checked={selectAll}
                        onChange={onSelectAllChange}
                        className="absolute inset-0 w-full h-full opacity-0 cursor-pointer"
                        aria-label={t("COMMON.SELECT_ALL")}
                      />
                    </div>
                  </div>
                </label>
              </div>
            ),
            key: "attendance",
            type: "attendance",
            customClassName: "pr-[8px]",
          },
        ]
      : []),
    { label: t("COMMON.VOLUNTEER.NAME"), key: "full_name", type: "custom" },
    { label: t("COMMON.EMAIL"), key: "user_email", type: "email" },
    { label: t("COMMON.CONTACT_NUMBER"), key: "phone_number", type: "contact" },
    // Conditionally add emergency contact columns
    ...emergencyContactColumns(t, rows),
    { label: t("COMMON.ACTION"), key: "actions", type: "actions" },
  ];
}
