import { getDb } from './db.js';
const DDL = `
CREATE TABLE IF NOT EXISTS users (
  id TEXT PRIMARY KEY,
  email TEXT UNIQUE NOT NULL,
  stripe_customer_id TEXT UNIQUE,
  created_at TEXT NOT NULL DEFAULT (datetime('now'))
);

CREATE TABLE IF NOT EXISTS license_keys (
  id TEXT PRIMARY KEY,
  user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
  key_hash TEXT UNIQUE NOT NULL,
  key_prefix TEXT NOT NULL,
  balance_cents INTEGER NOT NULL DEFAULT 0 CHECK (balance_cents >= 0),
  status TEXT NOT NULL DEFAULT 'active' CHECK (status IN ('active', 'revoked')),
  created_at TEXT NOT NULL DEFAULT (datetime('now'))
);

CREATE TABLE IF NOT EXISTS charges (
  id TEXT PRIMARY KEY,
  license_key_id TEXT NOT NULL REFERENCES license_keys(id),
  amount_cents INTEGER NOT NULL DEFAULT 3,
  document_hash TEXT NOT NULL,
  status TEXT NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'confirmed', 'refunded', 'expired')),
  machine_id TEXT,
  file_type TEXT,
  file_size_bytes INTEGER,
  processing_ms INTEGER,
  created_at TEXT NOT NULL DEFAULT (datetime('now'))
);

CREATE TABLE IF NOT EXISTS payments (
  id TEXT PRIMARY KEY,
  user_id TEXT NOT NULL REFERENCES users(id),
  amount_cents INTEGER NOT NULL,
  stripe_payment_intent_id TEXT UNIQUE NOT NULL,
  created_at TEXT NOT NULL DEFAULT (datetime('now'))
);

CREATE TABLE IF NOT EXISTS sessions (
  token TEXT PRIMARY KEY,
  user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
  expires_at TEXT NOT NULL,
  created_at TEXT NOT NULL DEFAULT (datetime('now'))
);

CREATE INDEX IF NOT EXISTS idx_license_keys_user ON license_keys(user_id);
CREATE INDEX IF NOT EXISTS idx_license_keys_active ON license_keys(user_id, status) WHERE status = 'active';
CREATE INDEX IF NOT EXISTS idx_charges_key_status ON charges(license_key_id, status);
CREATE INDEX IF NOT EXISTS idx_charges_stale ON charges(status, created_at) WHERE status = 'pending';
CREATE INDEX IF NOT EXISTS idx_sessions_expires ON sessions(expires_at);
CREATE INDEX IF NOT EXISTS idx_charges_created ON charges(created_at);
CREATE INDEX IF NOT EXISTS idx_payments_created ON payments(created_at);
CREATE INDEX IF NOT EXISTS idx_users_created ON users(created_at);
CREATE INDEX IF NOT EXISTS idx_charges_confirmed_created ON charges(created_at) WHERE status = 'confirmed';
`;
// Migration: drop legacy free_machines table (replaced by auto-provisioning)
const MIGRATIONS = `
DROP TABLE IF EXISTS free_machines;
`;
export function ensureSchema() {
    getDb().exec(DDL);
    getDb().exec(MIGRATIONS);
    // Add spending limit columns (idempotent — ignores "duplicate column" errors)
    const alterStatements = [
        'ALTER TABLE license_keys ADD COLUMN spending_limit_cents INTEGER DEFAULT NULL',
        'ALTER TABLE license_keys ADD COLUMN spending_limit_reset_at TEXT DEFAULT NULL',
        "ALTER TABLE license_keys ADD COLUMN tier TEXT NOT NULL DEFAULT 'paid' CHECK (tier IN ('paid'))",
        'ALTER TABLE license_keys ADD COLUMN balance_hmac TEXT',
        'ALTER TABLE users ADD COLUMN real_email TEXT',
    ];
    for (const stmt of alterStatements) {
        try {
            getDb().exec(stmt);
        }
        catch (err) {
            const msg = err instanceof Error ? err.message : String(err);
            if (!msg.includes('duplicate column')) {
                throw err;
            }
        }
    }
}
//# sourceMappingURL=schema.js.map