SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;

SET @now_ms = CAST(UNIX_TIMESTAMP(UTC_TIMESTAMP(3)) * 1000 AS UNSIGNED);

CREATE TABLE IF NOT EXISTS admin_teams (
  id BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT COMMENT '主键 ID',
  parent_id BIGINT UNSIGNED NULL COMMENT '父级团队 ID',
  name VARCHAR(80) NOT NULL UNIQUE COMMENT '团队名称',
  description VARCHAR(255) NOT NULL DEFAULT '' COMMENT '团队说明',
  sort INT NOT NULL DEFAULT 0 COMMENT '排序值',
  created_at_ms BIGINT NOT NULL COMMENT '创建时间，UTC epoch ms',
  updated_at_ms BIGINT NOT NULL COMMENT '更新时间，UTC epoch ms',
  INDEX idx_admin_teams_parent_sort (parent_id, sort, id),
  INDEX idx_admin_teams_sort (sort, id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='后台团队表';

SET @ddl := IF(
  (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'admin_teams' AND COLUMN_NAME = 'parent_id') = 0,
  'ALTER TABLE admin_teams ADD COLUMN parent_id BIGINT UNSIGNED NULL COMMENT ''父级团队 ID'' AFTER id',
  'SELECT 1'
);
PREPARE stmt FROM @ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl := IF(
  (SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'admin_teams' AND INDEX_NAME = 'idx_admin_teams_parent_sort') = 0,
  'ALTER TABLE admin_teams ADD INDEX idx_admin_teams_parent_sort (parent_id, sort, id)',
  'SELECT 1'
);
PREPARE stmt FROM @ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

INSERT INTO admin_teams (id, parent_id, name, description, sort, created_at_ms, updated_at_ms) VALUES
  (1, NULL, '产研团队', '产品和研发后台用户', 1, @now_ms, @now_ms),
  (2, NULL, '运营团队', '运营后台用户；运营人员接口固定读取该团队', 2, @now_ms, @now_ms)
ON DUPLICATE KEY UPDATE
  parent_id = VALUES(parent_id),
  name = VALUES(name),
  description = VALUES(description),
  sort = VALUES(sort),
  updated_at_ms = @now_ms;

INSERT INTO admin_teams (parent_id, name, description, sort, created_at_ms, updated_at_ms) VALUES
  (2, '运营一部', '运营团队子部门', 1, @now_ms, @now_ms),
  (2, '运营二部', '运营团队子部门', 2, @now_ms, @now_ms),
  (2, '运营三部', '运营团队子部门', 3, @now_ms, @now_ms)
ON DUPLICATE KEY UPDATE
  parent_id = VALUES(parent_id),
  description = VALUES(description),
  sort = VALUES(sort),
  updated_at_ms = @now_ms;

-- 老环境的 admin_users 只有 team 文本字段；新增 team_id 后保留文本字段用于历史兼容和数据权限兜底。
SET @ddl := IF(
  (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'admin_users' AND COLUMN_NAME = 'team_id') = 0,
  'ALTER TABLE admin_users ADD COLUMN team_id BIGINT UNSIGNED NULL COMMENT ''后台团队 ID'' AFTER team',
  'SELECT 1'
);
PREPARE stmt FROM @ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl := IF(
  (SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'admin_users' AND INDEX_NAME = 'idx_admin_users_team_id') = 0,
  'ALTER TABLE admin_users ADD INDEX idx_admin_users_team_id (team_id)',
  'SELECT 1'
);
PREPARE stmt FROM @ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- 历史环境可能已经写过平台运营或平台运维等文本；这些都只作为迁移兼容来源，目标统一为运营团队 ID=2。
UPDATE admin_users
SET team_id = 2,
    team = '运营团队',
    updated_at_ms = @now_ms
WHERE team_id IS NULL
  AND TRIM(COALESCE(team, '')) IN ('运营', '运营团队', '平台运营', '平台运营团队', '平台运维', '平台运维团队', '运维', '运维团队');

UPDATE admin_users
SET team = '运营团队',
    updated_at_ms = @now_ms
WHERE team_id = 2
  AND TRIM(COALESCE(team, '')) <> '运营团队';

INSERT INTO admin_permissions (name, code, kind, description, created_at_ms, updated_at_ms) VALUES
  ('团队查看', 'team:view', 'menu', '查看后台团队配置和团队成员', @now_ms, @now_ms),
  ('团队创建', 'team:create', 'button', '创建后台团队', @now_ms, @now_ms),
  ('团队更新', 'team:update', 'button', '更新后台团队名称、说明和排序', @now_ms, @now_ms)
ON DUPLICATE KEY UPDATE
  name = VALUES(name),
  kind = VALUES(kind),
  description = VALUES(description),
  updated_at_ms = @now_ms;

INSERT INTO admin_menus (parent_id, title, code, path, icon, permission_code, sort, visible, created_at_ms, updated_at_ms)
SELECT parent.id, '团队配置', 'system-teams', '/system/teams', 'team', 'team:view', 36, TRUE, @now_ms, @now_ms
FROM admin_menus parent
WHERE parent.code = 'system'
ON DUPLICATE KEY UPDATE
  parent_id = VALUES(parent_id),
  title = VALUES(title),
  path = VALUES(path),
  icon = VALUES(icon),
  permission_code = VALUES(permission_code),
  sort = VALUES(sort),
  visible = VALUES(visible),
  updated_at_ms = @now_ms;

INSERT IGNORE INTO admin_role_permissions (role_id, permission_id)
SELECT admin_role.id, admin_permission.id
FROM admin_roles admin_role
JOIN admin_permissions admin_permission
WHERE admin_role.code = 'platform-admin'
  AND admin_permission.code IN ('team:view', 'team:create', 'team:update');

INSERT IGNORE INTO admin_role_permissions (role_id, permission_id)
SELECT admin_role.id, admin_permission.id
FROM admin_roles admin_role
JOIN admin_permissions admin_permission
WHERE admin_role.code = 'ops-admin'
  AND admin_permission.code IN ('team:view', 'team:create', 'team:update');

INSERT IGNORE INTO admin_role_permissions (role_id, permission_id)
SELECT admin_role.id, admin_permission.id
FROM admin_roles admin_role
JOIN admin_permissions admin_permission
WHERE admin_role.code IN ('auditor', 'readonly')
  AND admin_permission.code = 'team:view';
