-- permission-v2 stage 1 schema for MySQL 8.0
-- execution target: existing rdms system schema
-- execution mode: manual
--
-- purpose:
-- 1. add target-model columns
-- 2. create visibility relation tables
-- 3. keep legacy fields temporarily for subsequent code migration
--
-- note:
-- 1. run this script before stage 2/3 code changes
-- 2. do not drop leader_user_id / post_ids / data_scope in this stage

START TRANSACTION;

ALTER TABLE system_dept
  ADD COLUMN org_type VARCHAR(20) NOT NULL DEFAULT 'dept' COMMENT '组织节点类型：company/dept/direction/team' AFTER parent_id,
  ADD COLUMN path VARCHAR(1024) CHARACTER SET ascii NOT NULL DEFAULT '/' COMMENT '组织物化路径，格式如 /1/2/3/' AFTER org_type,
  ADD COLUMN level INT NOT NULL DEFAULT 1 COMMENT '组织层级，根节点为 1' AFTER path,
  ADD COLUMN code VARCHAR(64) NULL COMMENT '组织编码' AFTER level;

ALTER TABLE system_users
  ADD COLUMN position_id BIGINT NULL COMMENT '主岗位ID' AFTER dept_id,
  ADD COLUMN resigned_at DATETIME NULL COMMENT '离职时间' AFTER position_id;

ALTER TABLE system_post
  ADD COLUMN post_type VARCHAR(20) NULL COMMENT '岗位类型：management/technical/business' AFTER code,
  ADD COLUMN level_rank INT NULL COMMENT '岗位等级排序，越大级别越高' AFTER post_type;

ALTER TABLE system_dept
  ADD KEY idx_system_dept_parent_id (parent_id),
  ADD KEY idx_system_dept_org_type (org_type),
  ADD KEY idx_system_dept_path (path(191)),
  ADD UNIQUE KEY uk_system_dept_code (code);

ALTER TABLE system_users
  ADD KEY idx_system_users_dept_id (dept_id),
  ADD KEY idx_system_users_position_id (position_id);

ALTER TABLE system_post
  ADD KEY idx_system_post_type (post_type),
  ADD KEY idx_system_post_level_rank (level_rank);

CREATE TABLE system_org_leader_relation (
  id BIGINT NOT NULL COMMENT '主键ID',
  dept_id BIGINT NOT NULL COMMENT '组织节点ID',
  user_id BIGINT NOT NULL COMMENT '负责人用户ID',
  effective_from DATETIME NULL COMMENT '生效开始时间，空表示立即长期生效',
  effective_until DATETIME NULL COMMENT '生效结束时间，空表示长期有效',
  remark VARCHAR(500) NULL COMMENT '备注',
  creator VARCHAR(64) NULL DEFAULT '' COMMENT '创建者',
  create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  updater VARCHAR(64) NULL DEFAULT '' COMMENT '更新者',
  update_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  deleted BIT(1) NOT NULL DEFAULT b'0' COMMENT '是否删除',
  PRIMARY KEY (id),
  KEY idx_org_leader_user (user_id, deleted, effective_from, effective_until),
  KEY idx_org_leader_dept (dept_id, deleted, effective_from, effective_until),
  UNIQUE KEY uk_org_leader_once (dept_id, user_id, effective_from)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='组织负责人关系表';

CREATE TABLE system_user_management_relation (
  id BIGINT NOT NULL COMMENT '主键ID',
  manager_user_id BIGINT NOT NULL COMMENT '管理者用户ID',
  subordinate_user_id BIGINT NOT NULL COMMENT '被管理用户ID',
  effective_from DATETIME NULL COMMENT '生效开始时间，空表示立即长期生效',
  effective_until DATETIME NULL COMMENT '生效结束时间，空表示长期有效',
  remark VARCHAR(500) NULL COMMENT '备注',
  creator VARCHAR(64) NULL DEFAULT '' COMMENT '创建者',
  create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  updater VARCHAR(64) NULL DEFAULT '' COMMENT '更新者',
  update_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  deleted BIT(1) NOT NULL DEFAULT b'0' COMMENT '是否删除',
  PRIMARY KEY (id),
  KEY idx_mgr_user (manager_user_id, deleted, effective_from, effective_until),
  KEY idx_sub_user (subordinate_user_id, deleted, effective_from, effective_until),
  UNIQUE KEY uk_mgr_sub_once (manager_user_id, subordinate_user_id, effective_from)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户直接管理关系表';

CREATE TABLE system_user_visibility_config (
  id BIGINT NOT NULL COMMENT '主键ID',
  user_id BIGINT NOT NULL COMMENT '用户ID',
  visibility_type VARCHAR(20) NOT NULL COMMENT '可见范围类型：all/directions/projects',
  visible_direction_ids JSON NULL COMMENT '补充可见方向ID集合',
  visible_project_ids JSON NULL COMMENT '补充可见项目ID集合',
  remark VARCHAR(500) NULL COMMENT '备注',
  creator VARCHAR(64) NULL DEFAULT '' COMMENT '创建者',
  create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  updater VARCHAR(64) NULL DEFAULT '' COMMENT '更新者',
  update_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  deleted BIT(1) NOT NULL DEFAULT b'0' COMMENT '是否删除',
  PRIMARY KEY (id),
  UNIQUE KEY uk_visibility_user (user_id),
  KEY idx_visibility_type (visibility_type),
  CONSTRAINT chk_visibility_type CHECK (visibility_type IN ('all', 'directions', 'projects'))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户补充可见范围配置表';

CREATE TABLE system_project_member (
  id BIGINT NOT NULL COMMENT '主键ID',
  project_id BIGINT NOT NULL COMMENT '项目ID',
  user_id BIGINT NOT NULL COMMENT '用户ID',
  project_role VARCHAR(20) NOT NULL COMMENT '项目角色：pm/product/developer/tester/viewer',
  member_type VARCHAR(20) NOT NULL DEFAULT 'core' COMMENT '成员类型：core/support',
  joined_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '加入时间',
  left_at DATETIME NULL COMMENT '退出时间，空表示仍在项目中',
  remark VARCHAR(500) NULL COMMENT '备注',
  creator VARCHAR(64) NULL DEFAULT '' COMMENT '创建者',
  create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  updater VARCHAR(64) NULL DEFAULT '' COMMENT '更新者',
  update_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  deleted BIT(1) NOT NULL DEFAULT b'0' COMMENT '是否删除',
  active_user_id BIGINT GENERATED ALWAYS AS (
    CASE WHEN left_at IS NULL THEN user_id ELSE NULL END
  ) STORED,
  active_pm_project_id BIGINT GENERATED ALWAYS AS (
    CASE WHEN project_role = 'pm' AND left_at IS NULL THEN project_id ELSE NULL END
  ) STORED,
  PRIMARY KEY (id),
  UNIQUE KEY uk_project_active_member (project_id, active_user_id),
  UNIQUE KEY uk_project_active_pm (active_pm_project_id),
  KEY idx_pm_user_active (user_id, left_at),
  KEY idx_pm_project_role_active (project_id, project_role, left_at),
  CONSTRAINT chk_project_role CHECK (project_role IN ('pm', 'product', 'developer', 'tester', 'viewer')),
  CONSTRAINT chk_member_type CHECK (member_type IN ('core', 'support'))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='项目成员关系表';

COMMIT;
