-- Migration: Timmy as Economic Peer (Task #29)
-- New tables for Timmy's outbound Nostr events and trust vouching,
-- plus lightning address storage on existing identities.

-- ── timmy_nostr_events ────────────────────────────────────────────────────────
-- Audit log of every Nostr event Timmy signs and broadcasts (zaps, DMs, etc).

CREATE TABLE IF NOT EXISTS timmy_nostr_events (
  id                TEXT PRIMARY KEY,
  kind              INTEGER NOT NULL,
  recipient_pubkey  TEXT,
  event_json        TEXT NOT NULL,
  amount_sats       INTEGER,
  relay_url         TEXT,
  created_at        TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_timmy_nostr_events_kind
  ON timmy_nostr_events(kind);

CREATE INDEX IF NOT EXISTS idx_timmy_nostr_events_recipient
  ON timmy_nostr_events(recipient_pubkey);

-- ── nostr_trust_vouches ───────────────────────────────────────────────────────
-- Records elite-tier co-signing events. Each row grants a one-time trust boost
-- to the vouchee. Two uniqueness guards prevent trust fraud:
--   1. (voucher_pubkey, vouchee_pubkey) — one vouch per pair, ever.
--   2. event_id — a specific signed event cannot be replayed.

CREATE TABLE IF NOT EXISTS nostr_trust_vouches (
  id                TEXT PRIMARY KEY,
  voucher_pubkey    TEXT NOT NULL REFERENCES nostr_identities(pubkey),
  vouchee_pubkey    TEXT NOT NULL,
  event_id          TEXT NOT NULL,
  vouch_event_json  TEXT NOT NULL,
  trust_boost       INTEGER NOT NULL DEFAULT 20,
  created_at        TIMESTAMPTZ NOT NULL DEFAULT NOW(),

  CONSTRAINT uq_nostr_trust_vouches_event_id UNIQUE (event_id)
);

CREATE UNIQUE INDEX IF NOT EXISTS uq_nostr_trust_vouches_pair
  ON nostr_trust_vouches(voucher_pubkey, vouchee_pubkey);

CREATE INDEX IF NOT EXISTS idx_nostr_trust_vouches_voucher
  ON nostr_trust_vouches(voucher_pubkey);

CREATE INDEX IF NOT EXISTS idx_nostr_trust_vouches_vouchee
  ON nostr_trust_vouches(vouchee_pubkey);

-- ── nostr_identities: lightning address ──────────────────────────────────────
-- NIP-57 lud16 field stored on first verify so ZapService can resolve invoices.

ALTER TABLE nostr_identities
  ADD COLUMN IF NOT EXISTS lightning_address TEXT;
