/**
 * 认证模块接口
 * 包含: 登录、注册、获取验证码、退出登录
 */
import request from '../utils/request'

/** 账号类型枚举 (后端约定) */
export enum AccountType {
  /** 个人会员 */
  PERSONAL = 'PERSONAL',
  /** 单位/组织会员 */
  UNIT_OR_ORG = 'UNIT_OR_ORG',
}

/** 性别 */
export enum Gender {
  MALE = 'MALE',
  FEMALE = 'FEMALE',
  OTHER = 'OTHER',
}

/** 性别中文映射 */
export const GenderLabel: Record<Gender, string> = {
  [Gender.MALE]: '男',
  [Gender.FEMALE]: '女',
  [Gender.OTHER]: '其他',
}

/** 政治面貌 */
export enum PoliticalStatus {
  CPC_MEMBER = 'CPC_MEMBER',
  CYL_MEMBER = 'CYL_MEMBER',
  RCCK_MEMBER = 'RCCK_MEMBER',
  CDL_MEMBER = 'CDL_MEMBER',
  CDNCA_MEMBER = 'CDNCA_MEMBER',
  CADP_MEMBER = 'CADP_MEMBER',
  CPWDP_MEMBER = 'CPWDP_MEMBER',
  CZDP_MEMBER = 'CZDP_MEMBER',
  JSSS_MEMBER = 'JSSS_MEMBER',
  TAIWAN_LEAGUE_MEMBER = 'TAIWAN_LEAGUE_MEMBER',
  NON_PARTISAN = 'NON_PARTISAN',
  DEMOCRATIC_PARTIES = 'DEMOCRATIC_PARTIES',
  MASSES = 'MASSES',
  OTHER = 'OTHER',
}

export const PoliticalStatusLabel: Record<PoliticalStatus, string> = {
  [PoliticalStatus.CPC_MEMBER]: '中共党员',
  [PoliticalStatus.CYL_MEMBER]: '共青团员',
  [PoliticalStatus.RCCK_MEMBER]: '民革党员',
  [PoliticalStatus.CDL_MEMBER]: '民盟盟员',
  [PoliticalStatus.CDNCA_MEMBER]: '民建会员',
  [PoliticalStatus.CADP_MEMBER]: '民进会员',
  [PoliticalStatus.CPWDP_MEMBER]: '农工党党员',
  [PoliticalStatus.CZDP_MEMBER]: '致公党党员',
  [PoliticalStatus.JSSS_MEMBER]: '九三学社社员',
  [PoliticalStatus.TAIWAN_LEAGUE_MEMBER]: '台盟盟员',
  [PoliticalStatus.NON_PARTISAN]: '无党派人士',
  [PoliticalStatus.DEMOCRATIC_PARTIES]: '民主党派',
  [PoliticalStatus.MASSES]: '群众',
  [PoliticalStatus.OTHER]: '其他',
}

/** 学历 */
export enum EducationLevel {
  DOCTORAL = 'DOCTORAL',
  MASTER = 'MASTER',
  BACHELOR = 'BACHELOR',
  COLLEGE_AND_BELOW = 'COLLEGE_AND_BELOW',
  OTHER = 'OTHER',
}

export const EducationLevelLabel: Record<EducationLevel, string> = {
  [EducationLevel.DOCTORAL]: '博士研究生',
  [EducationLevel.MASTER]: '硕士研究生',
  [EducationLevel.BACHELOR]: '大学本科',
  [EducationLevel.COLLEGE_AND_BELOW]: '大专及以下',
  [EducationLevel.OTHER]: '其他',
}

/** 所属地区/单位 */
export enum RegionOrUnit {
  HANGZHOU = 'HANGZHOU',
  NINGBO = 'NINGBO',
  HUZHOU = 'HUZHOU',
  JIAXING = 'JIAXING',
  SHAOXING = 'SHAOXING',
  JINHUA = 'JINHUA',
  QUZHOU = 'QUZHOU',
  ZHOUSHAN = 'ZHOUSHAN',
  TAIZHOU = 'TAIZHOU',
  LISHUI = 'LISHUI',
  ZJCMU = 'ZJCMU',
  ZJTDH = 'ZJTDH',
  ZJTCMH = 'ZJTCMH',
  ZJXH = 'ZJXH',
  ZJZS = 'ZJZS',
  OTHER = 'OTHER',
}

export const RegionOrUnitLabel: Record<RegionOrUnit, string> = {
  [RegionOrUnit.HANGZHOU]: '杭州',
  [RegionOrUnit.NINGBO]: '宁波',
  [RegionOrUnit.HUZHOU]: '湖州',
  [RegionOrUnit.JIAXING]: '嘉兴',
  [RegionOrUnit.SHAOXING]: '绍兴',
  [RegionOrUnit.JINHUA]: '金华',
  [RegionOrUnit.QUZHOU]: '衢州',
  [RegionOrUnit.ZHOUSHAN]: '舟山',
  [RegionOrUnit.TAIZHOU]: '台州',
  [RegionOrUnit.LISHUI]: '丽水',
  [RegionOrUnit.ZJCMU]: '浙江中医药大学',
  [RegionOrUnit.ZJTDH]: '浙江省立同德医院',
  [RegionOrUnit.ZJTCMH]: '浙江省中医院',
  [RegionOrUnit.ZJXH]: '浙江省新华医院',
  [RegionOrUnit.ZJZS]: '浙江省中山医院',
  [RegionOrUnit.OTHER]: '其他',
}

/** 会员类型 (个人会员注册专用) */
export enum PersonalMemberType {
  SPECIAL_COMMITTEE_MEMBER = 'SPECIAL_COMMITTEE_MEMBER',
  ORDINARY_MEMBER = 'ORDINARY_MEMBER',
  REGIONAL_ASSOCIATION_LEADER = 'REGIONAL_ASSOCIATION_LEADER',
}

export const PersonalMemberTypeLabel: Record<PersonalMemberType, string> = {
  [PersonalMemberType.SPECIAL_COMMITTEE_MEMBER]: '专科分会委员',
  [PersonalMemberType.ORDINARY_MEMBER]: '普通会员',
  [PersonalMemberType.REGIONAL_ASSOCIATION_LEADER]: '地区中医药学(协)会会长、秘书长',
}

/** 单位/组织类型 */
export enum UnitOrOrgType {
  /** 高等院校 */
  COLLEGE = 'COLLEGE',
  /** 三级医疗机构 */
  TERTIARY_MEDICAL = 'TERTIARY_MEDICAL',
  /** 二级及以下医疗机构 */
  SECONDARY_AND_BELOW_MEDICAL = 'SECONDARY_AND_BELOW_MEDICAL',
  /** 企业 */
  ENTERPRISE = 'ENTERPRISE',
  /** 其他 */
  OTHER = 'OTHER',
}

export const UnitOrOrgTypeLabel: Record<UnitOrOrgType, string> = {
  [UnitOrOrgType.COLLEGE]: '高等院校',
  [UnitOrOrgType.TERTIARY_MEDICAL]: '三级医疗机构',
  [UnitOrOrgType.SECONDARY_AND_BELOW_MEDICAL]: '二级及以下医疗机构',
  [UnitOrOrgType.ENTERPRISE]: '企业',
  [UnitOrOrgType.OTHER]: '其他',
}

/** 统一登录请求参数 */
export interface LoginParams {
  /** 手机号 */
  phone: string
  /** 密码 */
  password: string
  /** 账号类型 */
  accountType: AccountType
}

/** 登录响应 (字段以 yapi 实际返回为准,先做宽松定义) */
export interface LoginResult {
  /** 访问令牌 (后端主返回字段,需透传于后续接口 Authorization 请求头) */
  accessToken?: string
  /** 刷新令牌 (可选) */
  refreshToken?: string
  /** 过期时间(秒或时间戳,可选) */
  expiresIn?: number
  /** 登录令牌 (旧字段名,保留兼容) */
  token?: string
  /** 用户ID */
  userId?: string
  /** 会员ID */
  memberId?: string
  /** 账号类型 */
  accountType?: AccountType
  /** 用户昵称/单位名称 */
  nickname?: string
  /** 是否已支付 */
  isPaid?: boolean
  /** 是否已过期 */
  isExpired?: boolean
  /** 兼容 yapi mock 其他字段 */
  [key: string]: unknown
}

/** 个人会员注册参数 */
export interface PersonalRegisterParams {
  /** 姓名 */
  name?: string
  /** 手机号 */
  phone?: string
  /** 邮箱 */
  email?: string
  /** 邮编 */
  zipCode?: string
  /** 性别 */
  gender?: Gender
  /** 身份证号 */
  identityCard?: string
  /** 政治面貌 */
  politicalStatus?: PoliticalStatus
  /** 学历 */
  educationLevel?: EducationLevel
  /** 职称 */
  title?: string
  /** 职务 */
  position?: string
  /** 所属地区/单位 */
  regionOrUnit?: RegionOrUnit
  /** 单位名称 */
  unitName?: string
  /** 单位地址 */
  unitAddress?: string
  /** 会员类型 */
  memberType?: PersonalMemberType
  /** 密码 */
  password?: string
}

/** 注册参数(通用,预留) */
export interface RegisterParams {
  phone: string
  password: string
  /** 短信验证码 */
  code: string
  /** 账号类型 */
  accountType: AccountType
  /** 真实姓名 / 单位名称 */
  name?: string
  /** 统一社会信用代码 (单位会员) */
  creditCode?: string
}

/**
 * 统一登录接口
 * @url POST /v1/wechat/official-account/auth/login
 * @example
 *   login({ phone: '13800138000', password: '123456', accountType: AccountType.PERSONAL })
 */
export const login = (data: LoginParams) =>
  request.post<LoginResult>('/auth/login', data)

/** 个人会员登录(对统一登录接口的便捷封装) */
export const loginPersonal = (params: { phone: string; password: string }) =>
  login({ ...params, accountType: AccountType.PERSONAL })

/** 单位会员登录(对统一登录接口的便捷封装) */
export const loginCompany = (params: { phone: string; password: string }) =>
  login({ ...params, accountType: AccountType.UNIT_OR_ORG })

/** 注册 (通用) */
export const register = (data: RegisterParams) =>
  request.post<LoginResult>('/auth/register', data)

/**
 * 个人会员注册
 * @url POST /api/v1/wechat/official-account/auth/register/personal
 * @returns success === true 表示注册成功, 返回值为 data 字段内容
 */
export const registerPersonal = (data: PersonalRegisterParams) =>
  request.post<LoginResult>('/auth/register/personal', data)

/** 单位/组织会员注册参数 */
export interface UnitOrOrgRegisterParams {
  /** 联系人姓名 */
  name?: string
  /** 联系人手机号 */
  phone?: string
  /** 单位/组织类型 */
  unitOrOrgType?: UnitOrOrgType
  /** 单位地址 */
  address?: string
  /** 邮编 */
  zipCode?: string
  /** 法人代表姓名 */
  legalPersonName?: string
  /** 法人代表手机号 */
  legalPersonPhone?: string
  /** 法人代表邮箱 */
  legalPersonEmail?: string
  /** 推荐人姓名 */
  referrerName?: string
  /** 推荐人手机号 */
  referrerPhone?: string
  /** 推荐人邮箱 */
  referrerEmail?: string
  /** 密码 */
  password?: string
}

/**
 * 单位/组织会员注册
 * @url POST /api/v1/wechat/official-account/auth/register/unit-or-org
 * @returns success === true 表示注册成功, 返回值为 data 字段内容
 */
export const registerUnitOrOrg = (data: UnitOrOrgRegisterParams) =>
  request.post<LoginResult>('/auth/register/unit-or-org', data)

/** 单位会员注册 (registerUnitOrOrg 的别名,保持命名一致性) */
export const registerCompany = registerUnitOrOrg

/** 发送短信验证码 */
export const sendSmsCode = (phone: string) =>
  request.post<{ expireSeconds: number }>('/auth/sms/send', { phone })

/** 退出登录 */
export const logout = () => request.post<void>('/auth/logout')
