/**
 * 认证模块接口
 * 包含: 登录、注册、获取验证码、退出登录
 */
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 ProfessionalTitle {
  CHIEF_PHYSICIAN = 'CHIEF_PHYSICIAN',
  DEPUTY_CHIEF_PHYSICIAN = 'DEPUTY_CHIEF_PHYSICIAN',
  ATTENDING_PHYSICIAN = 'ATTENDING_PHYSICIAN',
  LICENSED_PHYSICIAN = 'LICENSED_PHYSICIAN',
  ASSISTANT_PHYSICIAN = 'ASSISTANT_PHYSICIAN',
  CHIEF_PHARMACIST = 'CHIEF_PHARMACIST',
  DEPUTY_CHIEF_PHARMACIST = 'DEPUTY_CHIEF_PHARMACIST',
  SUPERVISOR_PHARMACIST = 'SUPERVISOR_PHARMACIST',
  LICENSED_PHARMACIST = 'LICENSED_PHARMACIST',
  CHIEF_NURSE = 'CHIEF_NURSE',
  DEPUTY_CHIEF_NURSE = 'DEPUTY_CHIEF_NURSE',
  SUPERVISOR_NURSE = 'SUPERVISOR_NURSE',
  LICENSED_NURSE = 'LICENSED_NURSE',
  RESEARCHER = 'RESEARCHER',
  DEPUTY_RESEARCHER = 'DEPUTY_RESEARCHER',
  ASSISTANT_RESEARCHER = 'ASSISTANT_RESEARCHER',
  CHIEF_TECHNICIAN = 'CHIEF_TECHNICIAN',
  DEPUTY_CHIEF_TECHNICIAN = 'DEPUTY_CHIEF_TECHNICIAN',
  SUPERVISOR_TECHNICIAN = 'SUPERVISOR_TECHNICIAN',
  PROFESSOR = 'PROFESSOR',
  ASSOCIATE_PROFESSOR = 'ASSOCIATE_PROFESSOR',
  LECTURER = 'LECTURER',
  OTHER_SENIOR_TITLE = 'OTHER_SENIOR_TITLE',
  OTHER_DEPUTY_SENIOR_TITLE = 'OTHER_DEPUTY_SENIOR_TITLE',
  OTHER_INTERMEDIATE_TITLE = 'OTHER_INTERMEDIATE_TITLE',
  OTHER_JUNIOR_TITLE = 'OTHER_JUNIOR_TITLE',
}

export const ProfessionalTitleLabel: Record<ProfessionalTitle, string> = {
  [ProfessionalTitle.CHIEF_PHYSICIAN]: '主任(中)医师',
  [ProfessionalTitle.DEPUTY_CHIEF_PHYSICIAN]: '副主任(中)医师',
  [ProfessionalTitle.ATTENDING_PHYSICIAN]: '主治(中)医师',
  [ProfessionalTitle.LICENSED_PHYSICIAN]: '执业(中)医师',
  [ProfessionalTitle.ASSISTANT_PHYSICIAN]: '助理(中)医师',
  [ProfessionalTitle.CHIEF_PHARMACIST]: '主任(中)药师',
  [ProfessionalTitle.DEPUTY_CHIEF_PHARMACIST]: '副主任(中)药师',
  [ProfessionalTitle.SUPERVISOR_PHARMACIST]: '主管(中)药师',
  [ProfessionalTitle.LICENSED_PHARMACIST]: '执业(中)药师',
  [ProfessionalTitle.CHIEF_NURSE]: '主任护师',
  [ProfessionalTitle.DEPUTY_CHIEF_NURSE]: '副主任护师',
  [ProfessionalTitle.SUPERVISOR_NURSE]: '主管护师',
  [ProfessionalTitle.LICENSED_NURSE]: '执业护师',
  [ProfessionalTitle.RESEARCHER]: '研究员',
  [ProfessionalTitle.DEPUTY_RESEARCHER]: '副研究员',
  [ProfessionalTitle.ASSISTANT_RESEARCHER]: '助理研究员',
  [ProfessionalTitle.CHIEF_TECHNICIAN]: '主任技师',
  [ProfessionalTitle.DEPUTY_CHIEF_TECHNICIAN]: '副主任技师',
  [ProfessionalTitle.SUPERVISOR_TECHNICIAN]: '主管技师',
  [ProfessionalTitle.PROFESSOR]: '教授',
  [ProfessionalTitle.ASSOCIATE_PROFESSOR]: '副教授',
  [ProfessionalTitle.LECTURER]: '讲师',
  [ProfessionalTitle.OTHER_SENIOR_TITLE]: '其他高级职称',
  [ProfessionalTitle.OTHER_DEPUTY_SENIOR_TITLE]: '其他副高职称',
  [ProfessionalTitle.OTHER_INTERMEDIATE_TITLE]: '其他中级职称',
  [ProfessionalTitle.OTHER_JUNIOR_TITLE]: '其他初级职称',
}

/** 所属地区/单位 (金华市内区县与医院) */
export enum RegionOrUnit {
  JINHUA_TCM_HOSPITAL = 'JINHUA_TCM_HOSPITAL',
  JINHUA_CENTRAL_HOSPITAL = 'JINHUA_CENTRAL_HOSPITAL',
  JINHUA_PEOPLE_HOSPITAL = 'JINHUA_PEOPLE_HOSPITAL',
  JINHUA_SECOND_HOSPITAL = 'JINHUA_SECOND_HOSPITAL',
  JINHUA_FIFTH_HOSPITAL = 'JINHUA_FIFTH_HOSPITAL',
  WUCHENG_DISTRICT = 'WUCHENG_DISTRICT',
  JINDONG_DISTRICT = 'JINDONG_DISTRICT',
  DEVELOPMENT_ZONE = 'DEVELOPMENT_ZONE',
  YIWU_CITY = 'YIWU_CITY',
  LANXI_CITY = 'LANXI_CITY',
  DONGYANG_CITY = 'DONGYANG_CITY',
  YONGKANG_CITY = 'YONGKANG_CITY',
  PUJIANG_COUNTY = 'PUJIANG_COUNTY',
  WUYI_COUNTY = 'WUYI_COUNTY',
  PANAN_COUNTY = 'PANAN_COUNTY',
}

export const RegionOrUnitLabel: Record<RegionOrUnit, string> = {
  [RegionOrUnit.JINHUA_TCM_HOSPITAL]: '金华市中医医院',
  [RegionOrUnit.JINHUA_CENTRAL_HOSPITAL]: '金华市中心医院',
  [RegionOrUnit.JINHUA_PEOPLE_HOSPITAL]: '金华市人民医院',
  [RegionOrUnit.JINHUA_SECOND_HOSPITAL]: '金华市第二医院',
  [RegionOrUnit.JINHUA_FIFTH_HOSPITAL]: '金华市第五医院',
  [RegionOrUnit.WUCHENG_DISTRICT]: '婺城区',
  [RegionOrUnit.JINDONG_DISTRICT]: '金东区',
  [RegionOrUnit.DEVELOPMENT_ZONE]: '开发区',
  [RegionOrUnit.YIWU_CITY]: '义乌市',
  [RegionOrUnit.LANXI_CITY]: '兰溪市',
  [RegionOrUnit.DONGYANG_CITY]: '东阳市',
  [RegionOrUnit.YONGKANG_CITY]: '永康市',
  [RegionOrUnit.PUJIANG_COUNTY]: '浦江县',
  [RegionOrUnit.WUYI_COUNTY]: '武义县',
  [RegionOrUnit.PANAN_COUNTY]: '磐安县',
}

/** 会员类型 (个人会员注册专用) */
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 {
  /** 事业单位 (含医院、高校、科研院所等) */
  PUBLIC_INSTITUTION = 'PUBLIC_INSTITUTION',
  /** 企业 */
  ENTERPRISE = 'ENTERPRISE',
  /** 其他 */
  OTHER = 'OTHER',
}

export const UnitOrOrgTypeLabel: Record<UnitOrOrgType, string> = {
  [UnitOrOrgType.PUBLIC_INSTITUTION]: '事业单位',
  [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
  /** 会员编号 (后端主会员业务编号,会员权益页展示及发票接口路径参数均使用此值) */
  memberNo?: string
  /** 注册时间 (后端返回的会员注册时间,会员权益页展示) */
  registerTime?: string
  /** 会员过期时间 (后端返回的会员过期时间,会员权益页展示) */
  expireTime?: string
  /** 发票抬头 (后端返回的会员发票抬头,会员权益页展示) */
  invoiceTitle?: string
  /** 发票税号 (后端返回的会员发票税号,会员权益页展示) */
  invoiceTaxNo?: string
  /** 账号类型 */
  accountType?: AccountType
  /** 单位/组类型 (单位会员专用,用于在会员权益页/支付页区分事业单位与企业/其他) */
  unitOrOrgType?: UnitOrOrgType
  /** 用户昵称/单位名称 */
  nickname?: string
  /** 是否已充值/开通会员 (后端主字段,用于登录后跳转判断) */
  isVip?: boolean
  /** 是否已支付 (旧字段,保留兼容) */
  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?: ProfessionalTitle
  /** 职务 */
  position?: string
  /** 所属地区/单位 */
  regionOrUnit?: RegionOrUnit
  /** 单位名称 */
  unitName?: string
  /** 单位地址 */
  unitAddress?: string
  /** 会员类型 */
  memberType?: PersonalMemberType
  /** 专科分会委员类型 (memberType = SPECIAL_COMMITTEE_MEMBER 时填写) */
  specialCommitteeMemberType?: string
  /** 密码 */
  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')
