import apiClient from './client'
import type {
  TeamAnalyticsResponse,
  PersonalAnalyticsResponse,
  FlowAnalyticsResponse,
  AnalyticsPeriod,
} from '@/types'

const analyticsApi = {
  async getTeamAnalytics(period: AnalyticsPeriod = '30d', engineerId?: string): Promise<TeamAnalyticsResponse> {
    const params: Record<string, string> = { period }
    if (engineerId) params.engineer_id = engineerId
    const response = await apiClient.get<TeamAnalyticsResponse>('/analytics/team', { params })
    return response.data
  },

  async getPersonalAnalytics(period: AnalyticsPeriod = '30d'): Promise<PersonalAnalyticsResponse> {
    const response = await apiClient.get<PersonalAnalyticsResponse>('/analytics/me', { params: { period } })
    return response.data
  },

  async getFlowAnalytics(treeId: string, period: AnalyticsPeriod = '30d'): Promise<FlowAnalyticsResponse> {
    const response = await apiClient.get<FlowAnalyticsResponse>(`/analytics/flows/${treeId}`, { params: { period } })
    return response.data
  },

  async rateSession(sessionId: string, rating: number, comment?: string): Promise<void> {
    await apiClient.post(`/sessions/${sessionId}/rate`, { rating, comment })
  },

  async submitStepFeedback(stepId: string, sessionId: string, wasHelpful: boolean): Promise<void> {
    await apiClient.post(`/steps/${stepId}/feedback`, { session_id: sessionId, was_helpful: wasHelpful })
  },
}

export default analyticsApi
