import apiClient from './client'

export interface SidebarActiveSession {
  session_id: string
  tree_name: string
  tree_id: string
  tree_type: 'troubleshooting' | 'procedural' | 'maintenance'
  started_at: string
  ticket_number: string | null
  psa_ticket_id: string | null
}

export interface SidebarRecentSession {
  session_id: string
  tree_name: string
  tree_id: string
  tree_type: 'troubleshooting' | 'procedural' | 'maintenance'
  completed_at: string
}

export interface SidebarTreeCounts {
  total: number
  troubleshooting: number
  procedural: number
  maintenance: number
}

export interface SidebarStatsResponse {
  resolved_today: number
  active_count: number
  total_session_minutes_today: number
  escalation_count: number
  tree_counts: SidebarTreeCounts
  active_sessions: SidebarActiveSession[]
  recent_completions: SidebarRecentSession[]
}

export const sidebarApi = {
  getStats: async (): Promise<SidebarStatsResponse> => {
    const tzOffset = new Date().getTimezoneOffset()
    const response = await apiClient.get<SidebarStatsResponse>(
      `/sessions/sidebar-stats?tz_offset=${tzOffset}`
    )
    return response.data
  },
}
