/**
 * vouch.js — Elite-tier Trust Panel for vouching newcomers.
 *
 * Visible only when the authenticated user has tier === 'elite'.
 * Calls POST /api/identity/vouch with a signed Nostr event.
 */

import { getOrRefreshToken, getPubkey, signEvent } from './nostr-identity.js';
import { decode as nip19Decode, npubEncode } from 'nostr-tools/nip19';

const API = '/api';

let _panel = null;
let _tier  = null;  // cached tier from /identity/me

// ── Public API ────────────────────────────────────────────────────────────────

export function initVouchPanel() {
  _panel = document.getElementById('vouch-panel');
  if (!_panel) return;

  document.getElementById('vouch-btn')?.addEventListener('click', _doVouch);

  // Check tier after identity is ready
  window.addEventListener('nostr:identity-ready', () => _checkTier());

  // Also check on init (identity may already be established)
  if (getPubkey()) _checkTier();
}

// ── Tier check ────────────────────────────────────────────────────────────────

async function _checkTier() {
  const token = await getOrRefreshToken(API);
  if (!token) {
    _setVisible(false);
    return;
  }

  try {
    const res = await fetch(`${API}/identity/me`, {
      headers: { 'X-Nostr-Token': token },
    });
    if (!res.ok) { _setVisible(false); return; }

    const data = await res.json();
    _tier = data.trust?.tier;
    _setVisible(_tier === 'elite');
  } catch {
    _setVisible(false);
  }
}

function _setVisible(show) {
  if (_panel) _panel.style.display = show ? '' : 'none';
}

// ── Vouch action ──────────────────────────────────────────────────────────────

async function _doVouch() {
  const input = document.getElementById('vouch-pubkey-input');
  const raw = (input?.value || '').trim();
  if (!raw) { _setStatus('Enter an npub or hex pubkey.', '#994444'); return; }

  // Decode npub1... or accept raw 64-char hex
  let hexPubkey;
  try {
    hexPubkey = _decodePubkey(raw);
  } catch (err) {
    _setStatus(err.message, '#994444');
    return;
  }

  const myPubkey = getPubkey();
  if (hexPubkey === myPubkey) {
    _setStatus('Cannot vouch for yourself.', '#994444');
    return;
  }

  _setStatus('Signing vouch event...', '#ffaa00');
  _setBtn(true);

  try {
    const token = await getOrRefreshToken(API);
    if (!token) { _setStatus('No Nostr token — identify first.', '#994444'); return; }

    // Create and sign a Nostr event with ["p", voucheePubkey] tag
    const eventTemplate = {
      kind:       1,
      created_at: Math.floor(Date.now() / 1000),
      tags:       [['p', hexPubkey]],
      content:    `Vouching for ${hexPubkey}`,
    };

    const signedEvent = await signEvent(eventTemplate);
    if (!signedEvent) { _setStatus('Signing failed.', '#994444'); return; }

    _setStatus('Submitting vouch...', '#ffaa00');

    const res = await fetch(`${API}/identity/vouch`, {
      method:  'POST',
      headers: {
        'Content-Type': 'application/json',
        'X-Nostr-Token': token,
      },
      body: JSON.stringify({ voucheePubkey: hexPubkey, event: signedEvent }),
    });

    const data = await res.json();

    if (res.status === 409) {
      // Duplicate vouch
      const shortNpub = _shortNpub(hexPubkey);
      _setStatus(`Already vouched for ${shortNpub}.`, '#ffaa00');
      return;
    }

    if (!res.ok) {
      _setStatus(data.error || `Error ${res.status}`, '#994444');
      return;
    }

    // Success
    const shortNpub = _shortNpub(hexPubkey);
    _setStatus(`\u2713 Vouched for ${shortNpub} \u2014 +20 trust pts granted`, '#22aa66');
    if (input) input.value = '';

  } catch (err) {
    _setStatus('Network error: ' + err.message, '#994444');
  } finally {
    _setBtn(false);
  }
}

// ── Helpers ───────────────────────────────────────────────────────────────────

function _decodePubkey(input) {
  // npub1... bech32
  if (input.startsWith('npub1')) {
    const decoded = nip19Decode(input);
    if (decoded.type !== 'npub') throw new Error('Invalid npub — wrong bech32 type.');
    return decoded.data;
  }
  // Raw 64-char hex
  if (/^[0-9a-f]{64}$/i.test(input)) {
    return input.toLowerCase();
  }
  throw new Error('Enter a valid npub1... or 64-char hex pubkey.');
}

function _shortNpub(hexPubkey) {
  try {
    const npub = npubEncode(hexPubkey);
    return npub.slice(0, 12) + '\u2026';
  } catch {
    return hexPubkey.slice(0, 12) + '\u2026';
  }
}

function _setStatus(msg, color = '#22aa66') {
  const el = document.getElementById('vouch-status');
  if (el) { el.textContent = msg; el.style.color = color; }
}

function _setBtn(disabled) {
  const el = document.getElementById('vouch-btn');
  if (el) el.disabled = disabled;
}
