-- Capsule exports: one-shot download tokens for backend-generated ZIP exports

CREATE TABLE IF NOT EXISTS capsule_exports (
  id CHAR(36) PRIMARY KEY,
  user_id CHAR(36) NOT NULL,
  capsule_id CHAR(36) NOT NULL,

  token_hash CHAR(64) NOT NULL,
  expires_at DATETIME NOT NULL,
  used_at DATETIME NULL,

  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,

  UNIQUE KEY uq_capsule_exports_token_hash (token_hash),
  KEY idx_capsule_exports_user_capsule_created (user_id, capsule_id, created_at),
  KEY idx_capsule_exports_expires_at_used_at (expires_at, used_at),
  CONSTRAINT fk_capsule_exports_capsules FOREIGN KEY (capsule_id) REFERENCES capsules(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

