CREATE TABLE IF NOT EXISTS metrics (
  id BIGSERIAL PRIMARY KEY,
  name TEXT NOT NULL,
  unit TEXT NOT NULL,
  value NUMERIC(24, 8) NOT NULL,
  category TEXT NOT NULL,
  source TEXT NOT NULL,
  crawl_time TIMESTAMPTZ,
  metric_timestamp BIGINT,
  tags JSONB
);

CREATE TABLE IF NOT EXISTS system_events (
  id BIGSERIAL PRIMARY KEY,
  name TEXT NOT NULL,
  severity TEXT NOT NULL,
  payload JSONB NOT NULL,
  labels JSONB NOT NULL DEFAULT '{}',
  timestamp BIGINT NOT NULL
);

CREATE TABLE IF NOT EXISTS system_audits (
  id BIGSERIAL PRIMARY KEY,
  node_id TEXT NOT NULL,
  node_type TEXT NOT NULL,
  operation TEXT NOT NULL,
  status TEXT NOT NULL,
  message_id TEXT,
  duration_ms BIGINT NOT NULL,
  error TEXT,
  labels JSONB NOT NULL DEFAULT '{}',
  timestamp BIGINT NOT NULL
);

CREATE TABLE IF NOT EXISTS system_dlq (
  id UUID PRIMARY KEY,
  source_node TEXT NOT NULL,
  val_timestamp BIGINT NOT NULL,
  correlation_id UUID,
  chain_depth INTEGER NOT NULL,
  tags JSONB NOT NULL DEFAULT '{}',
  payload JSONB NOT NULL
);