"use client";

import type { VolunteerOpportunityRoleRecord } from "@/features/opportunities/rolesAndTeamsRecord";
import { useTranslation } from "react-i18next";
import { FiEdit } from "react-icons/fi";
import { RiDeleteBin5Fill } from "react-icons/ri";
import Table, { type TableColumn } from "@/components/ui/Table";
import { useWindowWidth } from "@/hooks/useWindowWidth";
import { useLanguageStore } from "@/store/languageStore";

interface VolunteerRolesTableProps {
  columns: TableColumn[];
  /* The real record. Rows are handed straight to `<Table data>` and read by
     column key, but the keys the columns name are this resource's — declaring
     them as an open `Record<string, unknown>` meant a column could name a
     field the payload does not have and nothing would say so. */
  roles: VolunteerOpportunityRoleRecord[];
  /** Zero once the roles account for the whole participant count. */
  participantsStillNeeded: number;
  /* The role id is an integer on `VolunteerOpportunityRoleResource`. */
  onEdit: (roleId: number) => void;
  onDelete: (roleId: number) => void;
}

/**
 * The roles defined so far, with the running shortfall above them.
 *
 * The heading is the feedback that makes the "must add up exactly" rule
 * learnable — it counts down to zero and then says so, rather than only
 * complaining at the point the organiser tries to leave.
 */
export default function VolunteerRolesTable({
  columns,
  roles,
  participantsStillNeeded,
  onEdit,
  onDelete,
}: VolunteerRolesTableProps) {
  const { t } = useTranslation();
  const windowWidth = useWindowWidth();
  const selectedLanguage = useLanguageStore((s) => s.language);

  return (
    <>
      <div className="text-center mb-4">
        {participantsStillNeeded > 0 ? (
          <p className="text-primary-5 font-semibold">
            {t("COMMON.PARTICIPANTS_STILL_NEEDED", {
              count: participantsStillNeeded,
            })}
          </p>
        ) : (
          <p className="text-green-500 font-semibold">
            {t("COMMON.ALL_ROLES_FILLED")}
          </p>
        )}
      </div>

      <div className="pb-8">
        <Table
          columns={columns}
          data={roles}
          renderCell={(column, rowData) => {
            if (column.key === "Action") {
              return (
                <div className="flex items-center gap-4 mobilescreen:gap-2">
                  <button
                    className="text-primary-5 cursor-pointer"
                    onClick={() => onEdit(rowData.id)}
                  >
                    <FiEdit size={windowWidth <= 767 ? 18 : 24} />
                  </button>
                  <button
                    className="text-primary-5 cursor-pointer"
                    onClick={() => onDelete(rowData.id)}
                  >
                    <RiDeleteBin5Fill size={windowWidth <= 767 ? 18 : 24} />
                  </button>
                </div>
              );
            }
            return (
              <span
                className={
                  column.key === "participants_needed"
                    ? `${
                        selectedLanguage === "ar"
                          ? "mr-[7px] text-right"
                          : "ml-[13px] text-left"
                      } text-primary-4 h-[27px] w-[50px] rounded-xl border border-primary-5/10 bg-primary-5/10 flex items-center justify-center text-center text-[13px] overflow-hidden text-ellipsis whitespace-nowrap`
                    : "block"
                }
              >
                {/* Runtime column key — narrowed and stringified rather
                    than read off an `any`. */}
                {String(
                  (rowData as unknown as Record<string, unknown>)[column.key] ??
                    "-"
                )}
              </span>
            );
          }}
        />
      </div>
    </>
  );
}
