CREATE TABLE IF NOT EXISTS admin_users (
  id BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
  username VARCHAR(64) NOT NULL UNIQUE,
  name VARCHAR(80) NOT NULL,
  password_hash VARCHAR(255) NOT NULL,
  team VARCHAR(80),
  status VARCHAR(24) NOT NULL DEFAULT 'active',
  mfa_enabled BOOLEAN NOT NULL DEFAULT FALSE,
  last_login_at_ms BIGINT NULL,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  INDEX idx_admin_users_status (status)
);

CREATE TABLE IF NOT EXISTS admin_roles (
  id BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
  name VARCHAR(80) NOT NULL UNIQUE,
  code VARCHAR(80) NOT NULL UNIQUE,
  description VARCHAR(255),
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL
);

CREATE TABLE IF NOT EXISTS admin_permissions (
  id BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
  name VARCHAR(100) NOT NULL,
  code VARCHAR(100) NOT NULL UNIQUE,
  kind VARCHAR(32) NOT NULL,
  description VARCHAR(255),
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  INDEX idx_admin_permissions_kind (kind)
);

CREATE TABLE IF NOT EXISTS admin_user_roles (
  user_id BIGINT UNSIGNED NOT NULL,
  role_id BIGINT UNSIGNED NOT NULL,
  PRIMARY KEY (user_id, role_id)
);

CREATE TABLE IF NOT EXISTS admin_role_permissions (
  role_id BIGINT UNSIGNED NOT NULL,
  permission_id BIGINT UNSIGNED NOT NULL,
  PRIMARY KEY (role_id, permission_id)
);

CREATE TABLE IF NOT EXISTS admin_menus (
  id BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
  parent_id BIGINT UNSIGNED NULL,
  title VARCHAR(80) NOT NULL,
  code VARCHAR(100) NOT NULL UNIQUE,
  path VARCHAR(160),
  icon VARCHAR(64),
  permission_code VARCHAR(100),
  sort INT NOT NULL DEFAULT 0,
  visible BOOLEAN NOT NULL DEFAULT TRUE,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  INDEX idx_admin_menus_parent_id (parent_id),
  INDEX idx_admin_menus_permission_code (permission_code)
);

CREATE TABLE IF NOT EXISTS admin_refresh_tokens (
  id BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
  user_id BIGINT UNSIGNED NOT NULL,
  token_hash VARCHAR(128) NOT NULL UNIQUE,
  user_agent VARCHAR(255),
  ip VARCHAR(64),
  expires_at_ms BIGINT NOT NULL,
  revoked_at_ms BIGINT NULL,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  INDEX idx_admin_refresh_tokens_user_id (user_id),
  INDEX idx_admin_refresh_tokens_expires_at (expires_at_ms)
);

CREATE TABLE IF NOT EXISTS admin_login_logs (
  id BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
  username VARCHAR(64),
  user_id BIGINT UNSIGNED NULL,
  ip VARCHAR(64),
  user_agent VARCHAR(255),
  status VARCHAR(32) NOT NULL,
  message VARCHAR(255),
  created_at_ms BIGINT NOT NULL,
  INDEX idx_admin_login_logs_username (username),
  INDEX idx_admin_login_logs_status (status)
);

CREATE TABLE IF NOT EXISTS admin_operation_logs (
  id BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
  user_id BIGINT UNSIGNED NOT NULL,
  username VARCHAR(64) NOT NULL,
  action VARCHAR(80) NOT NULL,
  resource VARCHAR(120),
  method VARCHAR(12),
  path VARCHAR(180),
  ip VARCHAR(64),
  status VARCHAR(32) NOT NULL,
  detail TEXT,
  created_at_ms BIGINT NOT NULL,
  INDEX idx_admin_operation_logs_user_id (user_id),
  INDEX idx_admin_operation_logs_action (action)
);

CREATE TABLE IF NOT EXISTS admin_notifications (
  id BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
  title VARCHAR(120) NOT NULL,
  content VARCHAR(500),
  level VARCHAR(24) NOT NULL DEFAULT 'info',
  read_at_ms BIGINT NULL,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  INDEX idx_admin_notifications_level (level)
);

CREATE TABLE IF NOT EXISTS admin_service_assets (
  id BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
  code VARCHAR(80) NOT NULL UNIQUE,
  name VARCHAR(120) NOT NULL,
  description VARCHAR(255),
  status VARCHAR(24) NOT NULL,
  deploy VARCHAR(80),
  version VARCHAR(40),
  port VARCHAR(32),
  replicas VARCHAR(32),
  response VARCHAR(32),
  cpu INT NOT NULL DEFAULT 0,
  memory INT NOT NULL DEFAULT 0,
  trend_json TEXT,
  logs_json TEXT,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  INDEX idx_admin_service_assets_status (status)
);
