declare namespace Api {
  /**
   * namespace SystemManage
   *
   * backend api module: "system"
   */
  namespace SystemManage {
    type CommonStatus = 0 | 1;

    interface PageParams {
      pageNo: number;
      pageSize: number;
    }

    interface PageResult<T = any> {
      total: number;
      list: T[];
    }

    type RoleType = 1 | 2;

    type ScopeType = 'global' | 'object';

    type ObjectType = 'product' | 'project';

    interface ScopeQueryParams {
      scopeType?: ScopeType;
      objectType?: ObjectType;
    }

    interface Role {
      /** role id */
      id: string;
      /** role name */
      name: string;
      /** role code */
      code: string;
      /** scope type */
      scopeType?: ScopeType;
      /** object type */
      objectType?: ObjectType | '' | null;
      /** display sort */
      sort: number;
      /** status: 0 enabled, 1 disabled */
      status: CommonStatus;
      /** role type */
      type: RoleType;
      /** remark */
      remark?: string | null;
      /** 是否在前端选择面板可见：0 不可见 / 1 可见，缺省视作可见 */
      visible?: 0 | 1 | null;
      /** create time */
      createTime: number;
    }

    type RoleSearchParams = CommonType.RecordNullable<Pick<Role, 'name' | 'code' | 'status'>> &
      PageParams & { createTime?: string[] } & ScopeQueryParams;

    type SaveRoleParams = (Pick<Role, 'name' | 'code' | 'sort' | 'status'> & {
      remark?: string | null;
    }) &
      ScopeQueryParams;

    type RoleList = PageResult<Role>;

    /**
     * keep the legacy fields for the user page until it is migrated
     */
    type AllRole = RoleSimple & {
      roleName: string;
      roleCode: string;
    };

    type DeptOrgType = 'company' | 'dept' | 'function' | 'direction' | 'team';

    interface Dept {
      id: number;
      name: string;
      parentId: number;
      orgType: DeptOrgType;
      path?: string | null;
      level?: number | null;
      code?: string | null;
      sort?: number | null;
      status: CommonStatus;
      createTime?: number | null;
      children?: Dept[] | null;
    }

    interface DeptSimple {
      id: number;
      name: string;
      parentId: number;
      children?: DeptSimple[] | null;
    }

    type DeptList = Dept[];

    type DeptSimpleList = DeptSimple[];

    type DeptSelfAndChildrenList = DeptSimple[];

    type DeptSearchParams = CommonType.RecordNullable<Pick<Dept, 'name' | 'orgType' | 'status'>>;

    type SaveDeptParams = Pick<Dept, 'name' | 'parentId' | 'orgType' | 'code' | 'sort' | 'status'>;

    interface OrgLeaderRelation {
      id: number;
      deptId: number;
      userId: string;
      userNickname: string;
      effectiveFrom?: number | null;
      effectiveUntil?: number | null;
      remark?: string | null;
      createTime?: number | null;
      updateTime?: number | null;
    }

    type OrgLeaderRelationList = OrgLeaderRelation[];

    interface OrgLeaderCandidateUser {
      id: string;
      nickname: string;
      deptId: number;
      deptName?: string | null;
    }

    type OrgLeaderCandidateUserList = OrgLeaderCandidateUser[];

    type SaveOrgLeaderRelationParams = {
      deptId: number;
      userId: string | null;
      effectiveFrom?: number | null;
      effectiveUntil?: number | null;
      remark?: string | null;
    };

    type UserGender = 0 | 1 | 2;

    interface User {
      id: number;
      username: string;
      nickname?: string | null;
      remark?: string | null;
      deptId: number;
      deptName?: string | null;
      positionId?: number | null;
      positionName?: string | null;
      company?: string | null;
      email?: string | null;
      mobile?: string | null;
      sex?: UserGender | null;
      avatar?: string | null;
      status: CommonStatus;
      sort?: number;
      loginIp?: string | null;
      resignedAt?: number | null;
      loginDate?: number | null;
      createTime: number;
    }

    type UserSearchParams = CommonType.RecordNullable<
      Pick<User, 'status'> &
        Pick<PageParams, 'pageNo' | 'pageSize'> & {
          username?: string;
          nickname?: string;
          mobile?: string;
          deptId?: number;
          roleId?: string;
          company?: string;
        }
    >;

    type UserList = PageResult<User>;

    type SaveUserParams = Pick<User, 'username' | 'deptId'> & {
      nickname?: string | null;
      remark?: string | null;
      positionId?: number | null;
      resignedAt?: number | null;
      company?: string | null;
      email?: string | null;
      mobile?: string | null;
      sex?: UserGender | null;
      avatar?: string | null;
      sort?: number;
      password?: string;
    };

    interface UpdateUserStatusParams {
      id: number;
      status: CommonStatus;
    }

    interface UpdateUserPasswordParams {
      id: number;
      password: string;
    }

    type PostType = 'management' | 'technical' | 'business';

    interface Post {
      id: number;
      name: string;
      code?: string | null;
      postType?: PostType | null;
      levelRank?: number | null;
      sort?: number | null;
      status: CommonStatus;
      remark?: string | null;
      createTime: number;
    }

    interface PostSimple {
      id: number;
      name: string;
      code?: string | null;
      postType?: PostType | null;
      levelRank?: number | null;
      sort?: number | null;
    }

    type PostSimpleList = PostSimple[];

    type PostSearchParams = CommonType.RecordNullable<Pick<Post, 'name' | 'code' | 'postType' | 'status'>> & PageParams;

    type SavePostParams = Pick<Post, 'name' | 'code' | 'postType' | 'levelRank' | 'sort' | 'status'> & {
      remark?: string | null;
    };

    type PostList = PageResult<Post>;

    type RoleSimple = Pick<Role, 'id' | 'name' | 'code' | 'status' | 'sort' | 'remark' | 'visible'>;

    type RoleSimpleList = RoleSimple[];

    interface AssignUserRoleParams {
      userId: number;
      roleIds: string[];
    }

    /**
     * menu type
     *
     * - 1: directory
     * - 2: menu
     * - 3: button
     */
    type MenuType = 1 | 2 | 3;

    /**
     * menu route kind
     *
     * - dir: directory route
     * - view: normal page route
     * - single: top level single page
     * - iframe: iframe page
     * - external: external link
     * - redirect: redirect route
     */
    type MenuRouteKind = 'dir' | 'view' | 'single' | 'iframe' | 'external' | 'redirect';

    interface Menu {
      /** menu id */
      id: string;
      /** menu name */
      name: string;
      /** permission code */
      permission?: string | null;
      /** scope type */
      scopeType?: ScopeType;
      /** object type */
      objectType?: ObjectType | '' | null;
      /** menu type */
      type: MenuType;
      /** display sort */
      sort: number;
      /** parent menu id */
      parentId: string;
      /** route path */
      path?: string | null;
      /** menu icon */
      icon?: string | null;
      /** component path */
      component?: string | null;
      /** component name */
      componentName?: string | null;
      /** route kind */
      routeKind?: MenuRouteKind | null;
      /** route props json */
      routePropsJson?: string | null;
      /** status: 0 enabled, 1 disabled */
      status: CommonStatus;
      /** visible in menu */
      visible?: boolean | null;
      /** keep alive */
      keepAlive?: boolean | null;
      /** always show children */
      alwaysShow?: boolean | null;
      /** create time */
      createTime: number;
      /** frontend tree children */
      children?: Menu[] | null;
    }

    type MenuSearchParams = CommonType.RecordNullable<Pick<Menu, 'name' | 'status'>> & ScopeQueryParams;

    type SaveMenuParams = Pick<
      Menu,
      | 'name'
      | 'permission'
      | 'type'
      | 'sort'
      | 'parentId'
      | 'path'
      | 'icon'
      | 'component'
      | 'componentName'
      | 'routeKind'
      | 'routePropsJson'
      | 'status'
      | 'visible'
      | 'keepAlive'
      | 'alwaysShow'
    > &
      ScopeQueryParams;

    interface MenuSimple {
      id: string;
      name: string;
      parentId: string;
      scopeType?: ScopeType;
      objectType?: ObjectType | '' | null;
      type: MenuType;
      children?: MenuSimple[] | null;
    }

    type MenuList = Menu[];

    type MenuSimpleList = MenuSimple[];

    interface AssignRoleMenuParams {
      roleId: string;
      menuIds: string[];
    }

    /**
     * 用户管理链路记录
     *
     * 用于管理用户之间的管理链路，支持多层级上下级关系
     * 对应后端 UserManagementRelationRespVO
     */
    interface UserManagementRelation {
      /** 主键 ID */
      id: string | null;
      /** 管理者用户 ID */
      managerUserId: string | null;
      /** 被管理用户 ID */
      subordinateUserId: string | null;
      /** 生效开始时间 */
      effectiveFrom?: number | null;
      /** 生效结束时间 */
      effectiveUntil?: number | null;
      /** 备注 */
      remark?: string | null;
      /** 创建时间 */
      createTime: number;
    }

    /**
     * 用户管理链路树形响应 VO
     *
     * 专门用于树形结构展示的响应对象
     * 对应后端 UserManagementRelationTreeRespVO
     */
    interface UserManagementRelationTreeRespVO {
      /** 关系记录主键 ID（最高领导为 null） */
      id: string | null;
      /** 用户 ID */
      userId: string;
      /** 用户昵称 */
      userNickname: string;
      /** 上级用户 ID（最高领导为 null） */
      managerUserId: string | null;
      /** 上级用户昵称（最高领导为 null） */
      managerNickname: string | null;
      /** 下级用户列表（基层员工为空列表） */
      children?: UserManagementRelationTreeRespVO[] | null;
    }

    /**
     * 当前登录用户的下属树
     *
     * 用于团队视角选择器；根节点代表“全部下属范围”
     */
    interface MySubordinateTreeNode {
      /** 用户 ID */
      userId: string;
      /** 用户昵称 */
      userNickname: string;
      /** 是否为当前登录用户根节点 */
      isRoot: boolean;
      /** 全链路下属人数 */
      subordinateCount: number;
      /** 下级用户列表 */
      children?: MySubordinateTreeNode[] | null;
    }

    /**
     * 用户管理链路保存参数
     *
     * 用于创建和更新操作
     * 对应后端 UserManagementRelationSaveReqVO
     */
    interface UserManagementRelationSaveReqVO {
      /** 主键 ID（更新时需要） */
      id?: string;
      /** 管理者用户 ID */
      managerUserId: string | null;
      /** 被管理用户 ID */
      subordinateUserId: string | null;
      /** 生效开始时间 */
      effectiveFrom?: number | null;
      /** 生效结束时间 */
      effectiveUntil?: number | null;
      /** 备注 */
      remark?: string | null;
    }

    /**
     * 用户管理链路查询参数（列表）
     *
     * 用于搜索框的查询和导出
     * 对应后端 UserManagementRelationQueryReqVO
     */
    type UserManagementRelationQueryReqVO = CommonType.RecordNullable<
      Pick<UserManagementRelation, 'managerUserId' | 'subordinateUserId'> & {
        /** 是否来自管理链路的index组件 */
        fromUserIndex: boolean;
        /** 部门ID */
        deptId?: number | null;
      }
    >;

    /**
     * 用户简单信息
     *
     * 用于用户选择下拉框
     */
    interface UserSimple {
      /** 用户 ID */
      id: string;
      /** 用户昵称 */
      nickname: string;
      /** 用户账号 */
      username?: string | null;
      /** 部门 ID */
      deptId?: string | null;
      /** 部门名称 */
      deptName?: string | null;
    }

    type UserSimpleList = UserSimple[];
  }
}
