import { BaseDefine, MultipleQuery, MultipleResult } from './base'

/** 用户管理 & 登录 */
export namespace User {
  /** >>> user api <<< */
  /** 用户登录 */
  interface LoginResponse {
    accessToken: string
    activeTime: string
    allowIP: string[]
    denyIP: string[]
    identity: string
    secret: string
    sessionId: string
    /** 名称 */
    name: string
    /** 角色 */
    role: ROLE
    /** 用户ID */
    id: number
    /** 考试批次ID */
    examId: number
    /** 大题ID */
    mainNumber: number
    /** 评卷员状态 */
    stage: STAGE
    /** 科目代码 */
    subjectCode: string
    /** needRealName */
    needRealName: boolean
  }
  type UserLogin = BaseDefine<
    {
      loginName: string
      password: string
    },
    LoginResponse
  >

  /** 导出用户 */
  type ExportUser = BaseDefine<{
    enable?: boolean
    role?: ROLE
    loginName?: string
    name?: string
    mainNumber?: number
    subjectCode?: string
  }>

  interface BaseUserInfo {
    examId?: number
    examName?: string
    loginName: string
    mainNumber?: number
    markingGroupNumber?: number
    name?: string
    role: ROLE
    subjectCode: string
    enable: boolean
  }

  interface ResponseUserInfo extends BaseUserInfo {
    id: number
    createTime: string
    groupId: number
    subjectName: string
    roleName: string
    markDayCount?: number
    markTotalCount?: number
    updateTime: string
    updaterName: string
  }

  /** 新增修改用户信息 */
  type SaveUserInfo = BaseDefine<
    BaseUserInfo & {
      id?: number
      password: string
    }
  >

  /** 批量创建用户 */
  type BulkSaveUser = BaseDefine<{
    /** 启用/禁用 */
    enable: boolean
    /** 账号位数 */
    loginNameLength: number
    /** 大题ID */
    mainNumber?: number
    /** 分组账号数量 */
    markingGroupAccounts?: number
    /** 小组号 */
    markingGroupNumber?: number
    /** 密码 */
    password: string
    /** 自定义前缀 */
    prefix: string
    /** 角色 */
    role: ROLE
    /** 起始数 */
    start?: number
    /** 科目代码 */
    subjectCode: string
  }>

  /** 查询用户信息 */
  type GetUserInfo = BaseDefine<
    {
      id: number
    },
    ResponseUserInfo
  >
  /** 查询当前用户信息 */
  type GetMyUserInfo = BaseDefine<null, ResponseUserInfo>
  /** 查询用户列表 */
  type GetUserList = BaseDefine<
    MultipleQuery<{
      enable?: boolean
      role?: ROLE
      loginName?: string
      name?: string
      mainNumber?: number
      subjectCode?: string
    }>,
    MultipleResult<ResponseUserInfo>
  >
  /** 修改用户密码 */
  type UpdateUserPwd = BaseDefine<{ password: string }>
  /** 修改用户姓名 */
  type UpdateUserName = BaseDefine<{ name: string }>
  /** 重置指定用户密码 */
  type ResetUsersPwd = BaseDefine<{ userIds: number[] }>
  /** 启用/禁用用户 */
  type ToggleEnableUsers = BaseDefine<{ ids: number[]; enable: boolean }>

  interface PrivilegeInfo {
    code: string
    id: number
    name: string
    nodeCode: string
    nodeId: string
    nodeName: string
    parentId: number
    parentNodeId: string
    privilegeType: string
    privilegeUri: string
  }
  /** 获取当前用户菜单 */
  type GetUserPrivilege = BaseDefine<null, PrivilegeInfo[]>

  /** 超管选择考试 */
  type CheckExam = BaseDefine<{ examId: number }>

  /** 获取评卷员Tree */
  interface Marker {
    createTime: string
    enable: boolean
    examId: number
    examName: string
    id: number
    loginName: string
    mainNumber: number
    markingGroupNumber: number
    name: string
    role: string
    roleName: string
    subjectCode: string
    updateTime: string
    updaterName: string
  }

  type GetMarkerTree = BaseDefine<
    { subjectCode: string; mainNumber?: number; name: string },
    { markers: Marker[]; markingGroupNumber: number }[]
  >

  type GetMarkerList = BaseDefine<{ subjectCode: string; mainNumber?: number; markingGroupNumber?: number }, Marker[]>

  interface UserInfo {
    config: string
    createTime: string
    enable: boolean
    examId: number
    examName: string
    id: number
    loginName: string
    mainNumber: number
    markDayCount: number
    markTotalCount: number
    markingGroupNumber: number
    name: string
    role: string
    roleName: string
    subjectCode: string
    subjectName: string
    updateTime: string
    updaterName: string
  }

  type GetUserGroup = BaseDefine<
    null,
    {
      /** 大组长 */
      chiffGroup: UserInfo[]
      /** 小组长 */
      deputyGroup: UserInfo[]
      /** 评卷小组 */
      markerGroup: {
        markingGroupNumber: number
        markers: UserInfo[]
      }[]
    }
  >
  /** 设置试卷配置信息 */
  type SetUserMarkConfig = BaseDefine<{ config: string }>
  /** 获取试卷配置信息 */
  type GetUserMarkConfig = BaseDefine<null, string>

  /** >>> user api end <<< */
  export interface ApiMap {
    userLogin: UserLogin
    userLogout: null
    exportUser: ExportUser
    downloadUserTemplate: null
    saveUserInfo: SaveUserInfo
    getUserInfo: GetUserInfo
    getMyUserInfo: GetMyUserInfo
    getUserList: GetUserList
    updateUserPwd: UpdateUserPwd
    resetUsersPwd: ResetUsersPwd
    toggleEnableUsers: ToggleEnableUsers
    checkExam: CheckExam
    bulkSaveUser: BulkSaveUser
    updateUserName: UpdateUserName
    getUserPrivilege: GetUserPrivilege
    getMarkerTree: GetMarkerTree
    getMarkerList: GetMarkerList
    /** 用户分组 - 发消息 */
    getUserGroup: GetUserGroup
    /** 设置试卷配置信息 */
    setUserMarkConfig: SetUserMarkConfig
    /** 获取试卷配置信息 */
    getUserMarkConfig: GetUserMarkConfig
  }
}
