-- SQLite schema
CREATE TABLE IF NOT EXISTS users (
  pubkey TEXT PRIMARY KEY,
  nostr_first_seen_at INTEGER,
  notes_count INTEGER DEFAULT 0,
  followers_count INTEGER DEFAULT 0,
  following_count INTEGER DEFAULT 0,
  activity_score INTEGER DEFAULT 0,
  last_metadata_fetch_at INTEGER,
  lightning_address TEXT,
  name TEXT,
  created_at INTEGER NOT NULL DEFAULT (unixepoch()),
  updated_at INTEGER NOT NULL DEFAULT (unixepoch())
);

CREATE TABLE IF NOT EXISTS claims (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  pubkey TEXT NOT NULL,
  claimed_at INTEGER NOT NULL,
  payout_sats INTEGER NOT NULL,
  ip_hash TEXT NOT NULL,
  payout_destination_hash TEXT,
  status TEXT NOT NULL CHECK(status IN ('pending','paid','failed')),
  lnbits_payment_hash TEXT,
  error_message TEXT,
  FOREIGN KEY (pubkey) REFERENCES users(pubkey)
);

CREATE TABLE IF NOT EXISTS ip_limits (
  ip_hash TEXT PRIMARY KEY,
  last_claimed_at INTEGER NOT NULL,
  claim_count_period INTEGER NOT NULL DEFAULT 0
);

CREATE TABLE IF NOT EXISTS quotes (
  quote_id TEXT PRIMARY KEY,
  pubkey TEXT NOT NULL,
  payout_sats INTEGER NOT NULL,
  lightning_address TEXT,
  created_at INTEGER NOT NULL,
  expires_at INTEGER NOT NULL,
  status TEXT NOT NULL CHECK(status IN ('active','consumed','expired'))
);

CREATE TABLE IF NOT EXISTS daily_stats (
  date TEXT PRIMARY KEY,
  total_paid_sats INTEGER NOT NULL DEFAULT 0,
  total_claims INTEGER NOT NULL DEFAULT 0,
  unique_pubkeys INTEGER NOT NULL DEFAULT 0
);

CREATE TABLE IF NOT EXISTS nonces (
  nonce TEXT PRIMARY KEY,
  expires_at INTEGER NOT NULL
);

CREATE TABLE IF NOT EXISTS deposits (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  created_at INTEGER NOT NULL,
  amount_sats INTEGER NOT NULL,
  source TEXT NOT NULL CHECK(source IN ('lightning','cashu')),
  lnbits_payment_hash TEXT
);

CREATE INDEX IF NOT EXISTS idx_claims_pubkey ON claims(pubkey);
CREATE INDEX IF NOT EXISTS idx_claims_claimed_at ON claims(claimed_at);
CREATE INDEX IF NOT EXISTS idx_claims_ip_hash ON claims(ip_hash);
CREATE INDEX IF NOT EXISTS idx_quotes_expires_at ON quotes(expires_at);
CREATE INDEX IF NOT EXISTS idx_quotes_status ON quotes(status);
CREATE INDEX IF NOT EXISTS idx_deposits_created_at ON deposits(created_at);
CREATE INDEX IF NOT EXISTS idx_deposits_lnbits_payment_hash ON deposits(lnbits_payment_hash);

CREATE TABLE IF NOT EXISTS sponsors (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  npub TEXT NOT NULL,
  title TEXT NOT NULL,
  description TEXT NOT NULL,
  image_url TEXT,
  link_url TEXT NOT NULL,
  category TEXT,
  lightning_address TEXT,
  invoice_id TEXT,
  payment_hash TEXT,
  price_sats INTEGER NOT NULL,
  duration_days INTEGER NOT NULL,
  status TEXT NOT NULL CHECK(status IN ('pending_payment','pending_review','active','expired','removed')),
  created_at INTEGER NOT NULL,
  activated_at INTEGER,
  expires_at INTEGER,
  views INTEGER DEFAULT 0,
  clicks INTEGER DEFAULT 0,
  extends_sponsor_id INTEGER
);
CREATE INDEX IF NOT EXISTS idx_sponsors_status ON sponsors(status);
CREATE INDEX IF NOT EXISTS idx_sponsors_npub ON sponsors(npub);
CREATE INDEX IF NOT EXISTS idx_sponsors_expires_at ON sponsors(expires_at);
CREATE INDEX IF NOT EXISTS idx_sponsors_payment_hash ON sponsors(payment_hash);
