"use client";

import { useTranslation } from "react-i18next";
import {
  canMarkAttendanceForDate,
  isDateValidForVolunteer,
  toApiDate,
  type UntypedRow,
} from "./volunteerListHelpers";

/**
 * The per-day attendance checkbox.
 *
 * Four separate reasons disable it, and all four render the same state:
 *
 *  1. the volunteer is already marked present for the selected date;
 *  2. manual check-in is shut (the deadline has passed, or the opportunity never
 *     used it);
 *  3. the selected date is not one of *their* days — a volunteer who joined
 *     mid-run does not have the whole range;
 *  4. that day's session has not reached the point where attendance can be
 *     recorded.
 *
 * It stays visible rather than hidden so the organizer can still read who
 * attended after the window closes.
 */
export default function AttendanceCheckboxCell({
  rowData,
  selectedDate,
  manual_tracking,
  opportunity_start_date,
  opportunity_end_time,
  selectedAttendance,
  onAttendanceCheckboxChange,
}: {
  rowData: UntypedRow;
  selectedDate: string;
  manual_tracking: boolean;
  opportunity_start_date: string | undefined;
  opportunity_end_time: string | undefined;
  selectedAttendance: string[];
  onAttendanceCheckboxChange: (volunteerUuid: string) => void;
}) {
    const { t } = useTranslation();

    /*
     * `volunteer_uuid` is **nullable**: the resource reads it off the
     * volunteer *profile* (`$volunteerProfile?->uuid`), so a registration whose
     * user has no profile row has none — and the bulk-attendance endpoint
     * takes nothing else. Such a row cannot be marked present, so the box is
     * disabled rather than offered; before the row was typed, the missing id
     * was passed to `selectedAttendance.includes()` and to the change handler
     * as `undefined`, which selected nothing and reported nothing.
     */
    const volunteerUuid = rowData.volunteer_uuid ?? null;
    const formatted = toApiDate(selectedDate);
    const isAttendedOnSelectedDate = (
      rowData.date_wise_attended || []
    ).includes(formatted);
    const isSelected =
      volunteerUuid !== null && selectedAttendance.includes(volunteerUuid);
    const isCheckboxDisabled =
      isAttendedOnSelectedDate ||
      volunteerUuid === null ||
      !manual_tracking ||
      !isDateValidForVolunteer(
        rowData,
        selectedDate,
        opportunity_start_date
      ) ||
      !canMarkAttendanceForDate(
        rowData,
        selectedDate,
        opportunity_end_time
      );

    return (
      <div className="flex justify-center">
        <label
  className={
    isCheckboxDisabled ? "cursor-not-allowed" : "cursor-pointer"
  }
        >
  <div className="border border-secondary-100 p-1">
    <div
      className={`h-4 w-4 relative ${
        isAttendedOnSelectedDate || isSelected
          ? "bg-[#373737BF]/75"
          : "bg-white"
      }`}
    >
      <input
        type="checkbox"
        checked={isAttendedOnSelectedDate || isSelected}
        onChange={() =>
          !isCheckboxDisabled &&
          volunteerUuid !== null &&
          onAttendanceCheckboxChange(volunteerUuid)
        }
        disabled={isCheckboxDisabled}
        aria-label={t("COMMON.MARK_ATTENDANCE_FOR", { name: rowData.full_name ?? "" })}
        className={`absolute inset-0 w-full h-full opacity-0 ${
          isCheckboxDisabled ? "cursor-not-allowed" : "cursor-pointer"
        }`}
      />
    </div>
  </div>
        </label>
      </div>
    );
}
