-- Distributed locks table for D1-based locking across CF Worker isolates
CREATE TABLE IF NOT EXISTS _locks (
  name TEXT PRIMARY KEY,
  owner TEXT NOT NULL,
  expires_at INTEGER NOT NULL
);

-- Unique index on Stripe invoice ID to prevent duplicate mirroring
CREATE UNIQUE INDEX IF NOT EXISTS idx_invoices_stripe_id
  ON invoices(json_extract(metadata, '$.stripe_id'))
  WHERE json_extract(metadata, '$.stripe_id') IS NOT NULL;

-- DID Connect token storage (D1 for strong consistency instead of KV)
CREATE TABLE IF NOT EXISTS _did_connect_tokens (
  token TEXT PRIMARY KEY,
  data TEXT NOT NULL,
  expires_at INTEGER NOT NULL
);
