import request from './request'

export interface MemberStatistics {
  statDate: string
  newMembers: number
  activeMembers: number
  totalMembers: number
  signInMembers: number
  bookingMembers: number
  cancelMembers: number
}

export interface BookingStatistics {
  statDate: string
  newBookings: number
  cancelBookings: number
  attendBookings: number
  absentBookings: number
  attendanceRate: number
  cancelRate: number
}

export interface SignInStatistics {
  statDate: string
  totalSignIns: number
  successSignIns: number
  failSignIns: number
  successRate: number
  signInTypeDistribution: Record<string, number>
}

export interface StatisticsSummary {
  statDate: string
  generatedAt: string
  memberStatistics: MemberStatistics
  bookingStatistics: BookingStatistics
  signInStatistics: SignInStatistics
}

export function getSummary(params?: {
  statType?: string
  periodType?: string
  startTime?: string
  endTime?: string
}): Promise<StatisticsSummary> {
  return request.get('/api/datacount/summary', { params })
}

export function getMemberStatistics(params?: {
  periodType?: string
  startTime?: string
  endTime?: string
}): Promise<MemberStatistics> {
  return request.get('/api/datacount/member', { params })
}

export function getBookingStatistics(params?: {
  periodType?: string
  startTime?: string
  endTime?: string
}): Promise<BookingStatistics> {
  return request.get('/api/datacount/booking', { params })
}

export interface MemberSignInDetail {
  memberId: number
  memberName: string
  memberPhone: string
  totalSignIns: number
  successSignIns: number
  failedSignIns: number
  lastSignInTime: string
}

export interface MemberSignInResponse {
  statDate: string
  total: number
  page: number
  size: number
  list: MemberSignInDetail[]
}

export function getSignInStatistics(params?: {
  periodType?: string
  startTime?: string
  endTime?: string
}): Promise<SignInStatistics> {
  return request.get('/api/datacount/signin', { params })
}

export interface MemberMonthlySignIn {
  memberId: number
  memberName: string
  month: string
  totalSignIns: number
  successSignIns: number
  failedSignIns: number
}

export interface MemberDailySignIn {
  memberId: number
  signInTime: string
  signInStatus: string
  signInType: string
  failReason: string | null
}

export function getMemberSignInDetails(params?: {
  periodType?: string
  startTime?: string
  endTime?: string
  page?: number
  size?: number
}): Promise<MemberSignInResponse> {
  return request.get('/api/datacount/member/signin', { params })
}

export function getMemberMonthlySignIns(memberId: number): Promise<MemberMonthlySignIn[]> {
  return request.get(`/api/datacount/member/${memberId}/monthly-signin`)
}

export function getMemberDailySignIns(memberId: number, month: string): Promise<MemberDailySignIn[]> {
  return request.get(`/api/datacount/member/${memberId}/monthly-signin`, { params: { month } })
}

/**
 * 导出数据统计报表（返回Excel文件Blob）
 */
export function exportStatistics(params?: {
  statType?: string
  periodType?: string
  startTime?: string
  endTime?: string
}): Promise<Blob> {
  return request.get('/api/datacount/export', {
    params,
    responseType: 'blob',
  })
}