import request from './request'

export interface Member {
  id: number
  memberNo: string
  nickname: string
  phone: string
  gender: number
  genderDesc?: string
  birthday: string | null
  address: string
  avatar: string
  subscribed: boolean
  lastLoginAt: string
  createdAt: string
  updatedAt: string
  deletedAt: string | null
  isDeleted: boolean
  openid?: string
  unionId?: string
}

export interface MemberCardInfo {
  id: number
  status: string
  statusDesc: string
  remainingTimes: number
  remainingAmount: number
  expireTime: string | null
  purchaseTime: string | null
  memberCardId: number
  memberCardName: string
  memberCardType: string
  memberCardTypeDesc: string
  memberCardPrice: number
  memberCardValidityDays: number | null
  memberCardTotalTimes: number | null
  memberCardAmount: number | null
  memberCardStatus: number
  memberCardStatusDesc: string
  extraConfig: string | null
  createdAt: string
  updatedAt: string
}

export interface MemberDetail {
  id: number
  memberNo: string
  nickname: string
  phone: string
  genderDesc: string
  birthday: string | null
  address: string
  avatar: string
  subscribed: boolean
  lastLoginAt: string
  createdAt: string
  memberCards: MemberCardInfo[]
  activeCardCount: number
  inactiveCardCount: number
}

export interface UpdateMemberDto {
  nickname?: string
  gender?: number
  birthday?: string
  avatar?: string
  address?: string
}

export function getAllMembers(pageNum: number = 1, pageSize: number = 10): Promise<Member[]> {
  return request.get('/api/admin/members/all', {
    params: { pageNum, pageSize },
  })
}

export function searchMembers(
  searchValue: string,
  pageNum: number = 1,
  pageSize: number = 10,
): Promise<Member[]> {
  return request.get('/api/admin/members', {
    params: { searchValue, pageNum, pageSize },
  })
}

export function getMemberDetail(id: number): Promise<MemberDetail> {
  return request.get(`/api/admin/member/${id}`)
}

export function updateMember(id: number, data: UpdateMemberDto): Promise<boolean> {
  return request.put(`/api/admin/member/${id}`, data)
}

export function adminUpdatePhone(id: number, phone: string): Promise<boolean> {
  return request.post(`/api/admin/member/${id}/phone`, { phone })
}

// ==================== 会员卡管理 ====================

/** 可用会员卡类型 */
export interface MemberCardType {
  id: number
  memberCardId: number
  memberCardName: string
  memberCardType: string
  memberCardPrice: number
  memberCardValidityDays: number | null
  memberCardTotalTimes: number | null
  memberCardAmount: number | null
  memberCardStatus: number
  extraConfig: string | null
  createdAt: string
  updatedAt: string
}

/** 会员持有的卡记录 */
export interface MemberCardRecord {
  id: number
  memberCardRecordId: number
  memberId: number
  memberCardId: number
  status: string
  remainingTimes: number
  remainingAmount: number
  expireTime: string | null
  sourceOrderId: number | null
  purchaseTime: string
  version: number
  cardComposition: string | null
  createdAt: string
  updatedAt: string
}

/** 获取上架中的会员卡类型 */
export function getActiveCards(): Promise<MemberCardType[]> {
  return request.get('/api/member-cards/active')
}

/** 获取会员持有的卡记录 */
export function getMemberCardRecords(memberId: number): Promise<MemberCardRecord[]> {
  return request.get(`/api/member-card-records/my-cards/${memberId}`)
}

/** 为会员购买/绑定会员卡 */
export function purchaseCard(memberId: number, memberCardId: number): Promise<MemberCardRecord> {
  return request.post('/api/member-card-records/purchase', {
    memberId,
    memberCardId,
    sourceOrderId: null,
  })
}

/** 退卡/解绑 */
export function refundCard(recordId: number): Promise<string> {
  return request.post(`/api/member-card-records/${recordId}/refund`)
}

/** 验证管理员密码 */
export function verifyAdminPassword(password: string): Promise<boolean> {
  return request.post('/api/admin/verify-password', { password })
}
