-- M02-B WeChat identities, users and revocable sessions.

CREATE TABLE IF NOT EXISTS qipai_users (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  tenant_id BIGINT UNSIGNED NOT NULL,
  user_type VARCHAR(32) NOT NULL DEFAULT 'CUSTOMER',
  status VARCHAR(32) NOT NULL DEFAULT 'ACTIVE',
  role_version INT UNSIGNED NOT NULL DEFAULT 1,
  nickname VARCHAR(128) NOT NULL DEFAULT '',
  avatar_url VARCHAR(512) NOT NULL DEFAULT '',
  phone VARCHAR(32) NOT NULL DEFAULT '',
  last_login_at DATETIME(3) NULL,
  created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
  deleted_at DATETIME(3) NULL,
  CONSTRAINT fk_qipai_users_tenant FOREIGN KEY (tenant_id) REFERENCES qipai_tenants(id),
  KEY idx_qipai_users_tenant_status (tenant_id, status, deleted_at),
  KEY idx_qipai_users_tenant_type (tenant_id, user_type, deleted_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS qipai_user_identities (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  tenant_id BIGINT UNSIGNED NOT NULL,
  platform_app_id BIGINT UNSIGNED NOT NULL,
  user_id BIGINT UNSIGNED NOT NULL,
  provider VARCHAR(32) NOT NULL DEFAULT 'WECHAT_MINIAPP',
  openid VARCHAR(128) NOT NULL,
  unionid VARCHAR(128) NULL,
  created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
  deleted_at DATETIME(3) NULL,
  CONSTRAINT fk_qipai_user_identities_tenant FOREIGN KEY (tenant_id) REFERENCES qipai_tenants(id),
  CONSTRAINT fk_qipai_user_identities_app FOREIGN KEY (platform_app_id) REFERENCES qipai_platform_apps(id),
  CONSTRAINT fk_qipai_user_identities_user FOREIGN KEY (user_id) REFERENCES qipai_users(id),
  UNIQUE KEY uq_qipai_user_identities_tenant_app_openid (tenant_id, platform_app_id, openid),
  UNIQUE KEY uq_qipai_user_identities_tenant_app_user (tenant_id, platform_app_id, user_id),
  KEY idx_qipai_user_identities_tenant_unionid (tenant_id, unionid, deleted_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS qipai_auth_sessions (
  id CHAR(36) NOT NULL PRIMARY KEY,
  tenant_id BIGINT UNSIGNED NOT NULL,
  platform_app_id BIGINT UNSIGNED NOT NULL,
  user_id BIGINT UNSIGNED NOT NULL,
  role_version INT UNSIGNED NOT NULL,
  status VARCHAR(32) NOT NULL DEFAULT 'ACTIVE',
  expires_at DATETIME(3) NOT NULL,
  revoked_at DATETIME(3) NULL,
  revoke_reason VARCHAR(128) NOT NULL DEFAULT '',
  last_seen_at DATETIME(3) NULL,
  ip VARCHAR(64) NOT NULL DEFAULT '',
  user_agent VARCHAR(255) NOT NULL DEFAULT '',
  created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
  CONSTRAINT fk_qipai_auth_sessions_tenant FOREIGN KEY (tenant_id) REFERENCES qipai_tenants(id),
  CONSTRAINT fk_qipai_auth_sessions_app FOREIGN KEY (platform_app_id) REFERENCES qipai_platform_apps(id),
  CONSTRAINT fk_qipai_auth_sessions_user FOREIGN KEY (user_id) REFERENCES qipai_users(id),
  KEY idx_qipai_auth_sessions_user_active (tenant_id, user_id, status, expires_at),
  KEY idx_qipai_auth_sessions_expiry (status, expires_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

INSERT IGNORE INTO qipai_schema_migrations (version, name)
VALUES ('2026061804', 'm02b_wechat_auth');
