SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;

-- external_admin_accounts is a small credential table. The fixed-width identity and
-- nullable parent columns backfill existing rows as country_manager without reading
-- users, rooms, wallet ledgers or any other business table. The combined index/FK
-- addition validates and may rebuild this table depending on the exact MySQL 8 minor
-- version, so inspect its row count and run this away from account-creation peaks.
ALTER TABLE external_admin_accounts
  ADD COLUMN identity_type VARCHAR(32) NOT NULL DEFAULT 'country_manager' COMMENT 'local/country_manager/external_super_admin' AFTER permission_revision,
  ADD COLUMN parent_account_id BIGINT UNSIGNED NULL COMMENT '外管层级上级账号 ID' AFTER identity_type,
  ADD KEY idx_external_admin_accounts_hierarchy (app_code, parent_account_id, identity_type, status),
  ADD CONSTRAINT chk_external_admin_accounts_identity
    CHECK (identity_type IN ('local', 'country_manager', 'external_super_admin')),
  ADD CONSTRAINT fk_external_admin_accounts_parent
    FOREIGN KEY (parent_account_id) REFERENCES external_admin_accounts(id);
