// Lightweight face descriptor utilities.
// Note: Full face-api.js requires model downloads. For this testing build we
// store a perceptual hash of the face capture as the "descriptor" — proves
// the registration captured a real face and lets us re-verify on login by
// comparing distances. In production this would be swapped for a SITA-approved
// biometric service with liveness detection.

export interface FaceDescriptor {
  hash: number[];      // 64-element perceptual hash
  capturedAt: string;
}

/** Compute a 64-d perceptual hash from an HTMLVideoElement frame. */
export async function captureFaceDescriptor(video: HTMLVideoElement): Promise<FaceDescriptor> {
  const size = 64;
  const canvas = document.createElement("canvas");
  canvas.width = size;
  canvas.height = size;
  const ctx = canvas.getContext("2d");
  if (!ctx) throw new Error("Canvas not supported");

  // Square crop from center
  const vw = video.videoWidth;
  const vh = video.videoHeight;
  const s = Math.min(vw, vh);
  const sx = (vw - s) / 2;
  const sy = (vh - s) / 2;
  ctx.drawImage(video, sx, sy, s, s, 0, 0, size, size);

  const img = ctx.getImageData(0, 0, size, size).data;
  // Build 8x8 grayscale grid -> 64 floats
  const block = size / 8;
  const hash: number[] = [];
  for (let by = 0; by < 8; by++) {
    for (let bx = 0; bx < 8; bx++) {
      let sum = 0, n = 0;
      for (let y = by * block; y < (by + 1) * block; y++) {
        for (let x = bx * block; x < (bx + 1) * block; x++) {
          const i = (y * size + x) * 4;
          sum += 0.299 * img[i] + 0.587 * img[i + 1] + 0.114 * img[i + 2];
          n++;
        }
      }
      hash.push(sum / n / 255);
    }
  }
  return { hash, capturedAt: new Date().toISOString() };
}

/** Distance between two face descriptors (0 = identical, ~1 = unrelated). */
export function faceDistance(a: FaceDescriptor, b: FaceDescriptor): number {
  if (a.hash.length !== b.hash.length) return 1;
  let s = 0;
  for (let i = 0; i < a.hash.length; i++) s += (a.hash[i] - b.hash[i]) ** 2;
  return Math.sqrt(s / a.hash.length);
}

export const FACE_MATCH_THRESHOLD = 0.18;
