import type { PermissionListRow } from "@/features/opportunities/permissionRecord";
import type { VolunteerPickerRecord } from "@/features/opportunities/rolesAndTeamsRecord";

/**
 * A row from either permissions endpoint.
 *
 * The holders list and the candidate list do not agree on shape — one nests the
 * person under `user`, the other is flat — so every field the screen reads is
 * optional on both sides and `person()` picks whichever is present.
 */
/**
 * One row of `GET /attendance-permissions/list/`.
 *
 * A **union**, because the one adapter below reads two endpoints:
 *
 * - the holders list, `GET /attendance-permissions/list/`, is
 *   `CustomUserResource` spread flat with `is_allowed` and
 *   `attendance_permission_id` merged on (`PermissionListRow`). The person's id is
 *   **`id`** — there is no `user_id` — and there is no `civil_id` or
 *   `passport_number` on it at all.
 * - the candidate picker, `GET /all-volunteers/`, is
 *   `VolunteerPickerResource`: it *does* have `user_id` and a nested `user`,
 *   and its identity hints are **`civil_id_last4` / `passport_last4` /
 *   `phone_last4`** — not the full values under the full names.
 *
 * The local shape this replaces declared one nested `user` block with
 * `full_name` / `profile_pic` / `gender_display` plus flat `civil_id` and
 * `passport_number`. Neither resource matches that: the holders have the
 * display fields flat, and neither sends `civil_id` or `passport_number` under
 * those names.
 */
export type PermissionRow = PermissionListRow | VolunteerPickerRecord;

/**
 * «إذن تحضير» — grant and revoke the BE-69 permission for one opportunity.
 *
 * **Not** the `/scan-permission` screen. That one delegates QR scanning and is
 * retiring with BE-61 Part C; this grants a volunteer the ability to add other
 * volunteers to the opportunity and record their hours, which is a different
 * job on a different table. The client spelled the distinction out: «وليس
 * التحضير باستخدام الـ QR».
 *
 * One modal with two panels rather than a second route, because granting is
 * something an organizer does *while* looking at the volunteer list — the
 * screen the client's mockup puts the button on.
 *
 * Revoking is the same bulk-update call with `is_allowed: false`; the API has
 * no delete route, and the row is kept so the grant history survives.
 *
 * Mounted by the caller only while it is open, so every `useState` below starts
 * fresh on each open — reopening always lands on the holders list rather than
 * on a half-filled picker, without an effect that resets state on a prop flip.
 */

/**
 * One person from either endpoint, flattened for the two panels.
 *
 * - the holders list has **no `user_id`** — its `id` *is* the user's id —
 *   while the picker has both, with `id` being the volunteer *profile* id.
 *   So `user_id` is preferred and `id` used only as the fallback.
 * - the identity hints are `civil_id_last4` / `passport_last4` /
 *   `phone_last4` on the picker (masked by the server) and absent from the
 *   holders list.
 * - a holder is any row without `user_id`. It used to be detected by
 *   `scan_permission_id`, which the attendance list never sends (its id key is
 *   `attendance_permission_id`), so every holder lost its email and phone.
 */
export function permissionPerson(row: PermissionRow) {
  const picker = "user_id" in row ? row : undefined;
  const holder = picker ? undefined : (row as PermissionListRow);

  return {
    id: Number(picker?.user_id ?? row.id),
    name: row.full_name || picker?.user?.full_name || "",
    email: holder?.email || "",
    phone: holder?.phone_number || "",
    identifiers: [
      picker?.civil_id_last4,
      picker?.passport_last4,
      picker?.phone_last4 ?? holder?.phone_number,
    ].filter((value): value is string => Boolean(value)),
    pic: row.profile_pic || picker?.user?.profile_pic || "",
    genderEn: row.gender_display?.value_en || "",
  };
}
