/**
 * 数据分析/运营监控 API
 */
import { request } from '../utils/request'
import type {
  OverviewStats,
  ApiCallStat,
  ErrorStat,
  PerformanceMetric,
  RegionStat,
  DeviceStat,
  BrowserStat,
  OsStat,
  SourceStat,
  PageStat,
  EventTrack,
  AnalyticsParam,
} from '../types/analytics'

// ==================== 概览统计 ====================

/** 获取概览统计 */
export const getOverviewStats = (websiteId: number, date?: string) => {
  return request<OverviewStats>({
    url: '/developer/analytics/overview',
    method: 'GET',
    params: { websiteId, date },
  })
}

/** 获取趋势数据 */
export const getTrendData = (
  websiteId: number,
  startDate: string,
  endDate: string,
  granularity: 'hour' | 'day' | 'week' | 'month' = 'day'
) => {
  return request<{ list: ApiCallStat[] }>({
    url: '/developer/analytics/trend',
    method: 'GET',
    params: { websiteId, startDate, endDate, granularity },
  })
}

/** 获取今日实时数据 */
export const getRealtimeData = (websiteId: number) => {
  return request<{
    uv: number
    pv: number
    apiCalls: number
    errors: number
    activeUsers: number
  }>({
    url: '/developer/analytics/realtime',
    method: 'GET',
    params: { websiteId },
  })
}

// ==================== API 调用统计 ====================

/** 获取 API 调用统计 */
export const pageApiCalls = (params: AnalyticsParam) => {
  return request<{ list: ApiCallStat[]; total: number }>({
    url: '/developer/analytics/api-calls',
    method: 'GET',
    params,
  })
}

// ==================== 错误统计 ====================

/** 获取错误列表 */
export const pageErrors = (params: AnalyticsParam) => {
  return request<{ list: ErrorStat[]; total: number }>({
    url: '/developer/analytics/errors',
    method: 'GET',
    params,
  })
}

/** 获取错误详情 */
export const getErrorDetail = (id: number) => {
  return request<ErrorStat & { samples: any[] }>({
    url: `/developer/analytics/errors/${id}`,
    method: 'GET',
  })
}

// ==================== 性能指标 ====================

/** 获取性能指标 */
export const getPerformanceMetrics = (websiteId: number) => {
  return request<{ list: PerformanceMetric[] }>({
    url: '/developer/analytics/performance',
    method: 'GET',
    params: { websiteId },
  })
}

/** 获取性能趋势 */
export const getPerformanceTrend = (
  websiteId: number,
  metric: string,
  startDate: string,
  endDate: string
) => {
  return request<{ list: PerformanceMetric[] }>({
    url: '/developer/analytics/performance/trend',
    method: 'GET',
    params: { websiteId, metric, startDate, endDate },
  })
}

// ==================== 用户统计 ====================

/** 获取用户统计 */
export const getUserStats = (websiteId: number) => {
  return request<{
    total: number
    active: number
    new: number
    retention: number
  }>({
    url: '/developer/analytics/users',
    method: 'GET',
    params: { websiteId },
  })
}

/** 获取新增用户趋势 */
export const getNewUsersTrend = (
  websiteId: number,
  startDate: string,
  endDate: string
) => {
  return request<{ list: { date: string; count: number }[] }>({
    url: '/developer/analytics/users/trend',
    method: 'GET',
    params: { websiteId, startDate, endDate },
  })
}

// ==================== 区域分布 ====================

/** 获取区域分布 */
export const getRegionStats = (websiteId: number) => {
  return request<{ list: RegionStat[] }>({
    url: '/developer/analytics/regions',
    method: 'GET',
    params: { websiteId },
  })
}

// ==================== 设备统计 ====================

/** 获取设备类型统计 */
export const getDeviceStats = (websiteId: number) => {
  return request<{ list: DeviceStat[] }>({
    url: '/developer/analytics/devices',
    method: 'GET',
    params: { websiteId },
  })
}

/** 获取浏览器统计 */
export const getBrowserStats = (websiteId: number) => {
  return request<{ list: BrowserStat[] }>({
    url: '/developer/analytics/browsers',
    method: 'GET',
    params: { websiteId },
  })
}

/** 获取操作系统统计 */
export const getOsStats = (websiteId: number) => {
  return request<{ list: OsStat[] }>({
    url: '/developer/analytics/os',
    method: 'GET',
    params: { websiteId },
  })
}

// ==================== 来源统计 ====================

/** 获取流量来源 */
export const getSourceStats = (websiteId: number) => {
  return request<{ list: SourceStat[] }>({
    url: '/developer/analytics/sources',
    method: 'GET',
    params: { websiteId },
  })
}

/** 获取页面访问排行 */
export const getPageStats = (params: AnalyticsParam) => {
  return request<{ list: PageStat[]; total: number }>({
    url: '/developer/analytics/pages',
    method: 'GET',
    params,
  })
}

// ==================== 事件追踪 ====================

/** 获取事件列表 */
export const pageEvents = (params: AnalyticsParam) => {
  return request<{ list: EventTrack[]; total: number }>({
    url: '/developer/analytics/events',
    method: 'GET',
    params,
  })
}

/** 触发自定义事件 */
export const trackEvent = (data: {
  websiteId: number
  event: string
  properties?: Record<string, any>
}) => {
  return request<void>({
    url: '/developer/analytics/track',
    method: 'POST',
    data,
  })
}
