-- 022_crypto_payments.sql — MooChedda crypto payment sessions

-- Widen currency column to accommodate crypto symbols (B2B, USDC)
ALTER TABLE billing_purchases
  MODIFY COLUMN currency VARCHAR(10) NOT NULL DEFAULT 'usd';

CREATE TABLE IF NOT EXISTS crypto_payment_sessions (
  id          VARCHAR(36)    NOT NULL PRIMARY KEY,
  tenant_id   INT UNSIGNED   NOT NULL,
  invoice_id  VARCHAR(128)   NOT NULL,
  plan_tier   VARCHAR(32)    NOT NULL,
  months      INT            NOT NULL,
  currency    VARCHAR(10)    NOT NULL,
  amount      DECIMAL(12,4)  NOT NULL,
  status      ENUM('pending','paid','expired') NOT NULL DEFAULT 'pending',
  payment_url TEXT           DEFAULT NULL,
  purchase_id INT UNSIGNED   DEFAULT NULL,
  created_at  DATETIME       DEFAULT NOW(),
  updated_at  DATETIME       ON UPDATE NOW(),
  UNIQUE KEY uq_invoice (invoice_id),
  INDEX idx_tenant (tenant_id),
  CONSTRAINT fk_cps_tenant FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
