"use client";

import { useTranslation } from "react-i18next";
import { useQrScanner } from "./useQrScanner";
import { Modal } from "@/components/ui/Modal";
import { Button } from "@/components/ui/Button";

interface SelfScanModalProps {
  open: boolean;
  onClose: () => void;
  /** Dialog heading — e.g. "Scan the arrival code". */
  title: string;
  /** One line telling the participant which printed sheet to point at. */
  hint?: string;
  /**
   * Submits the decoded payload. Resolve to close the dialog; reject to keep it
   * open and re-arm the camera, so a wrong or expired sheet can be re-scanned
   * without reopening anything.
   */
  onScan: (code: string) => Promise<void>;
  isPending?: boolean;
}

/**
 * BE-61 — the participant's own camera, pointed at the organizer's printed code.
 *
 * This is the mirror image of `profile/ScanQR.tsx`, which is the *organizer*
 * scanning a volunteer's personal QR — the flow the client retired. The camera
 * plumbing is the same proven one (getUserMedia → canvas → `jsqr` per frame);
 * what differs is who holds the phone and what the payload means.
 *
 * A file picker sits alongside the camera because `getUserMedia` needs a secure
 * context and a camera: it is unavailable on desktop browsers without one, and
 * over plain http on a LAN address during testing. Without the fallback the
 * feature is simply untestable outside a phone on https.
 */
export default function SelfScanModal({
  open,
  onClose,
  title,
  hint,
  onScan,
  isPending = false,
}: SelfScanModalProps) {
  const { t } = useTranslation();
  const { videoRef, canvasRef, cameraError, scanning, decodeFile, stopCamera } =
    useQrScanner({ open, onScan, onClose, t });

  return (
    <Modal
      open={open}
      onClose={() => {
        stopCamera();
        onClose();
      }}
      title={title}
      size="sm"
    >
      <div className="flex flex-col items-center gap-4 pb-4">
        {hint ? (
          <p className="text-center text-base font-semibold text-secondary-102">
            {hint}
          </p>
        ) : null}

        <div className="relative w-full overflow-hidden rounded-[16px] bg-black">
          <video
            ref={videoRef}
            className="aspect-square w-full object-cover"
            muted
            playsInline
          />
          {/* A frame to aim with — the decoder reads the whole frame, so this is
              guidance only and deliberately not a crop. */}
          <div className="pointer-events-none absolute inset-8 rounded-[12px] border-4 border-white/80" />
        </div>
        <canvas ref={canvasRef} className="hidden" />

        {cameraError ? (
          <p className="text-center text-sm font-semibold text-red-500">
            {cameraError}
          </p>
        ) : (
          <p className="text-center text-sm text-secondary-102">
            {scanning ? t("COMMON.POINT_CAMERA_AT_QR") : t("COMMON.LOADING")}
          </p>
        )}

        <label className="w-full">
          <span className="sr-only">{t("COMMON.UPLOAD_QR_IMAGE")}</span>
          <input
            type="file"
            accept="image/*"
            className="hidden"
            onChange={(event) => {
              const file = event.target.files?.[0];
              if (file) decodeFile(file);
              event.target.value = "";
            }}
          />
          <span className="block cursor-pointer rounded-[20px] bg-[#F4F4F7] py-3 text-center text-sm font-bold text-primary-5">
            {t("COMMON.UPLOAD_QR_IMAGE")}
          </span>
        </label>

        <Button
          variant="secondary"
          size="medium"
          type="button"
          className="xss:!w-full"
          disabled={isPending}
          onClick={() => {
            stopCamera();
            onClose();
          }}
        >
          {t("COMMON.CANCEL")}
        </Button>
      </div>
    </Modal>
  );
}
