-- ── Audit Log ────────────────────────────────────────────────────────────────
CREATE TABLE IF NOT EXISTS audit_logs (
  id            BIGINT UNSIGNED  NOT NULL AUTO_INCREMENT PRIMARY KEY,
  tenant_id     INT UNSIGNED     NOT NULL,
  user_id       INT UNSIGNED     DEFAULT NULL,
  user_name     VARCHAR(255)     DEFAULT NULL,
  action        VARCHAR(64)      NOT NULL,
  entity_type   VARCHAR(64)      NOT NULL,
  entity_id     INT UNSIGNED     DEFAULT NULL,
  entity_label  VARCHAR(255)     DEFAULT NULL,
  changes       JSON             DEFAULT NULL,
  ip            VARCHAR(45)      DEFAULT NULL,
  created_at    DATETIME         NOT NULL DEFAULT CURRENT_TIMESTAMP,
  INDEX idx_audit_tenant (tenant_id, created_at),
  INDEX idx_audit_entity (tenant_id, entity_type, entity_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ── Attachments ───────────────────────────────────────────────────────────────
CREATE TABLE IF NOT EXISTS attachments (
  id            INT UNSIGNED     NOT NULL AUTO_INCREMENT PRIMARY KEY,
  tenant_id     INT UNSIGNED     NOT NULL,
  user_id       INT UNSIGNED     DEFAULT NULL,
  entity_type   VARCHAR(64)      NOT NULL,
  entity_id     INT UNSIGNED     NOT NULL,
  filename      VARCHAR(255)     NOT NULL,
  original_name VARCHAR(255)     NOT NULL,
  mime_type     VARCHAR(128)     DEFAULT NULL,
  file_size     INT UNSIGNED     DEFAULT NULL,
  created_at    DATETIME         NOT NULL DEFAULT CURRENT_TIMESTAMP,
  INDEX idx_attach_entity (tenant_id, entity_type, entity_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
