-- ============================================================
-- 表10：system_public_key（Layer 1 · 公钥注册表）
-- 用途：国家系统逻辑常驻接口 + 光湖系统密钥
-- 双面灯塔架构中的密钥验证基础设施
-- ============================================================

CREATE TABLE IF NOT EXISTS system_public_key (
  key_id            VARCHAR(64)   PRIMARY KEY,
  key_type          VARCHAR(32)   NOT NULL
                    CHECK (key_type IN ('national', 'guanghu', 'emergency', 'persona')),
  key_name          VARCHAR(128)  NOT NULL,
  public_key        TEXT          NOT NULL,
  algorithm         VARCHAR(32)   NOT NULL DEFAULT 'RSA-4096',
  fingerprint       VARCHAR(128),
  purpose           TEXT          NOT NULL,
  status            VARCHAR(16)   NOT NULL DEFAULT 'active'
                    CHECK (status IN ('active', 'revoked', 'expired')),
  valid_from        TIMESTAMP     NOT NULL DEFAULT CURRENT_TIMESTAMP,
  valid_until       TIMESTAMP,
  issued_by         VARCHAR(64)   NOT NULL,
  created_at        TIMESTAMP     NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX IF NOT EXISTS idx_spk_type ON system_public_key(key_type);
CREATE INDEX IF NOT EXISTS idx_spk_status ON system_public_key(status);
