// screen-qr-scanner.jsx — Scanner QR natif (Capacitor MLKit) + fallback web
// Surcharge QrScannerScreen défini dans screen-home.jsx.
// Données décodées stockées dans window.SM_VICTIM avant nav.go('victim_card').

function QrScannerScreen({ nav }) {
  useLucide();
  const t = useTranslation();
  const fileRef   = useRef(null);
  const canvasRef = useRef(null);

  const [scanning, setScanning] = useState(false);
  const [error, setError]       = useState(null);
  const [hint, setHint]         = useState(null);

  const isNative = !!(window.Capacitor?.isNativePlatform?.());

  // ── Traitement d'un résultat QR brut ──────────────────────────────────────
  // Le QR Sauv'Moi encode désormais une URL vers l'image PNG publique
  // (/api/public/medical-card/<id>.png?gen=…&exp=…) plutôt que du JSON brut
  // — nécessaire pour qu'un appareil photo externe à l'app ouvre directement
  // une image lisible. Scanné DEPUIS l'app, on ne veut pas ouvrir cette image
  // dans un navigateur externe : on extrait l'id/gen/exp de l'URL, on
  // recharge les données en JSON (.json, même route) et on continue vers
  // VictimCardScreen en interne, pour garder l'affichage riche + boutons
  // d'action existants. L'ancien format JSON brut reste supporté pour ne pas
  // casser un QR déjà généré/imprimé avant cette migration.
  async function handleRaw(raw) {
    setError(null);

    let legacyParsed = null;
    try { legacyParsed = JSON.parse(raw); } catch {}
    if (legacyParsed && legacyParsed.id) {
      if (legacyParsed.expiresAt && Date.now() > legacyParsed.expiresAt) {
        setError(t('qrscan.expired'));
        return;
      }
      window.SM_VICTIM = legacyParsed;
      nav.go('victim_card');
      return;
    }

    const m = /\/api\/public\/medical-card\/([^\/?]+)\.png(\?.*)?$/.exec(raw);
    if (!m) {
      setError(t('qrscan.not_sauvmoi'));
      return;
    }
    const id = m[1];
    const qs = m[2] || '';
    const base = raw.slice(0, raw.indexOf('/api/public/medical-card/'));

    // setScanning géré par l'appelant (autour du await handleRaw) — pas ici,
    // pour éviter un flicker true/false/true/false entre la fin du scan et
    // le fetch JSON qui suit.
    try {
      const res = await fetch(`${base}/api/public/medical-card/${id}.json${qs}`);
      if (!res.ok) {
        setError(res.status === 404 ? t('qrscan.card_not_found') : t('qrscan.card_load_failed'));
        return;
      }
      const data = await res.json();
      window.SM_VICTIM = data;
      nav.go('victim_card');
    } catch (e) {
      setError(t('qrscan.card_load_failed_network'));
    }
  }

  // ── Scanner natif via @capacitor-mlkit/barcode-scanning ──────────────────
  async function startNativeScan() {
    setScanning(true);
    setError(null);
    const BS = window.Capacitor?.Plugins?.BarcodeScanner;
    if (!BS) {
      setError(t('qrscan.plugin_unavailable'));
      setScanning(false);
      return;
    }
    try {
      const { camera } = await BS.checkPermissions();
      if (camera !== 'granted') {
        const result = await BS.requestPermissions();
        if (result.camera !== 'granted') {
          setError(t('qrscan.camera_permission_denied'));
          setScanning(false);
          return;
        }
      }
      setHint(t('qrscan.hint_pointing'));
      const { barcodes } = await BS.scan({ formats: ['QR_CODE'] });
      setHint(null);
      if (barcodes && barcodes.length > 0) {
        await handleRaw(barcodes[0].rawValue);
      } else {
        setError(t('qrscan.no_qr_detected'));
      }
    } catch (e) {
      setError(t('qrscan.scanner_error_prefix') + (e.message || String(e)));
      setHint(null);
    } finally {
      setScanning(false);
    }
  }

  // ── Fallback web : photo → jsQR ──────────────────────────────────────────
  function handleFileChange(e) {
    const file = e.target.files?.[0];
    if (!file) return;
    setScanning(true);
    setError(null);

    const img = new Image();
    img.onload = async () => {
      const canvas = canvasRef.current;
      if (!canvas) { setScanning(false); return; }
      canvas.width  = img.width;
      canvas.height = img.height;
      const ctx = canvas.getContext('2d');
      ctx.drawImage(img, 0, 0);
      const imageData = ctx.getImageData(0, 0, img.width, img.height);

      if (typeof window.jsQR !== 'function') {
        setError(t('qrscan.jsqr_not_loaded'));
        setScanning(false);
        return;
      }
      const code = window.jsQR(imageData.data, imageData.width, imageData.height);
      if (code) {
        await handleRaw(code.data);
      } else {
        setError(t('qrscan.no_qr_in_image'));
      }
      setScanning(false);
    };
    img.onerror = () => { setError(t('qrscan.image_read_failed')); setScanning(false); };
    img.src = URL.createObjectURL(file);
    e.target.value = '';
  }

  return (
    <div style={{ position: 'absolute', inset: 0, background: '#0a1628', display: 'flex', flexDirection: 'column' }}>

      {/* Canvas invisible pour jsQR */}
      <canvas ref={canvasRef} style={{ display: 'none' }} />
      {/* Input fichier caché */}
      <input ref={fileRef} type="file" accept="image/*" capture="environment" style={{ display: 'none' }} onChange={handleFileChange} />

      {/* Header */}
      <div style={{ padding: '16px 18px 14px', display: 'flex', alignItems: 'center', gap: 12, flexShrink: 0 }}>
        <button onClick={() => goBack(nav)} style={{ padding: 8, margin: -8, background: 'none', border: 'none', cursor: 'pointer' }}>
          <Icon name="arrow-left" size={22} color="white" />
        </button>
        <h2 className="sm-serif" style={{ fontSize: 20, color: 'white', flex: 1 }}>
          {t('home.qr_scan_title')}
        </h2>
      </div>

      {/* Zone viseur */}
      <div style={{ flex: 1, display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center', padding: '16px 32px' }}>

        {/* Cadre viseur animé */}
        <div style={{ position: 'relative', width: 230, height: 230, marginBottom: 28 }}>
          <div style={{ position: 'absolute', inset: 0, borderRadius: 20, border: '1.5px solid rgba(255,255,255,0.15)', background: 'rgba(255,255,255,0.04)' }} />
          {[
            { top: 12, left: 12, borderTop: '3px solid white', borderLeft: '3px solid white', borderRadius: '10px 0 0 0' },
            { top: 12, right: 12, borderTop: '3px solid white', borderRight: '3px solid white', borderRadius: '0 10px 0 0' },
            { bottom: 12, left: 12, borderBottom: '3px solid white', borderLeft: '3px solid white', borderRadius: '0 0 0 10px' },
            { bottom: 12, right: 12, borderBottom: '3px solid white', borderRight: '3px solid white', borderRadius: '0 0 10px 0' },
          ].map((s, i) => (
            <div key={i} style={{ position: 'absolute', width: 28, height: 28, ...s }} />
          ))}
          {/* Icône centrale */}
          <div style={{ position: 'absolute', inset: 0, display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
            {scanning ? (
              <div style={{ width: 44, height: 44, border: '3px solid rgba(255,255,255,0.3)', borderTopColor: 'white', borderRadius: '50%', animation: 'sm-spin 0.8s linear infinite' }} />
            ) : (
              <Icon name="scan-qr-code" size={52} color="rgba(255,255,255,0.25)" />
            )}
          </div>
        </div>

        {/* Message aide */}
        {!error && !hint && (
          <p style={{ color: 'rgba(255,255,255,0.7)', fontSize: 14, textAlign: 'center', lineHeight: 1.55, marginBottom: 6 }}>
            {isNative ? t('qrscan.help_native') : t('qrscan.help_web')}
          </p>
        )}
        {hint && (
          <p style={{ color: 'rgba(255,255,255,0.85)', fontSize: 14, textAlign: 'center', fontWeight: 600, marginBottom: 6 }}>
            {hint}
          </p>
        )}

        {/* Message d'erreur */}
        {error && (
          <div style={{ background: 'rgba(229,57,53,0.18)', border: '1px solid rgba(229,57,53,0.5)', borderRadius: 12, padding: '12px 16px', marginBottom: 14, display: 'flex', gap: 10, alignItems: 'flex-start' }}>
            <Icon name="x-circle" size={18} color="#ff6b6b" style={{ flexShrink: 0, marginTop: 1 }} />
            <span style={{ color: '#ff9b9b', fontSize: 13, lineHeight: 1.5 }}>{error}</span>
          </div>
        )}

      </div>

      {/* Boutons action */}
      <div style={{ padding: '0 20px 52px', display: 'flex', flexDirection: 'column', gap: 10, flexShrink: 0 }}>

        {isNative ? (
          <button
            onClick={startNativeScan}
            disabled={scanning}
            style={{ width: '100%', padding: '15px', borderRadius: 14, background: scanning ? 'rgba(255,255,255,0.15)' : 'var(--sm-red)', color: 'white', border: 'none', fontWeight: 700, fontSize: 16, cursor: scanning ? 'default' : 'pointer', display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 10 }}
          >
            <Icon name="scan" size={20} color="white" />
            {scanning ? t('qrscan.scan_in_progress') : t('qrscan.open_camera')}
          </button>
        ) : (
          <button
            onClick={() => fileRef.current?.click()}
            disabled={scanning}
            style={{ width: '100%', padding: '15px', borderRadius: 14, background: scanning ? 'rgba(255,255,255,0.15)' : 'var(--sm-red)', color: 'white', border: 'none', fontWeight: 700, fontSize: 16, cursor: scanning ? 'default' : 'pointer', display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 10 }}
          >
            <Icon name="camera" size={20} color="white" />
            {scanning ? t('qrscan.analyzing') : t('qrscan.open_camera_gallery')}
          </button>
        )}

        <button
          onClick={() => goBack(nav)}
          style={{ width: '100%', padding: '14px', borderRadius: 14, background: 'rgba(255,255,255,0.08)', color: 'rgba(255,255,255,0.75)', border: '1px solid rgba(255,255,255,0.15)', fontWeight: 500, fontSize: 15, cursor: 'pointer' }}
        >
          {t('qrscan.back')}
        </button>
      </div>
    </div>
  );
}

Object.assign(window, { QrScannerScreen });
