import { apiClient } from './client'

// TypeScript interfaces matching backend Pydantic schemas in psa_context.py

export interface TicketDetails {
  id: number
  summary: string
  status: string
  priority: string
  board: string
  sla: string | null
  date_entered: string
  resources: string | null
}

export interface CompanyInfo {
  id: number
  name: string
  site: string | null
  address: string | null
  phone: string | null
  type: string | null
  territory: string | null
}

export interface ContactInfo {
  name: string
  email: string | null
  phone: string | null
  title: string | null
}

export interface ConfigItemInfo {
  device_identifier: string
  type: string | null
  os_type: string | null
  serial_number: string | null
  ip_address: string | null
  model_number: string | null
}

export interface TicketNote {
  text: string
  member: string | null
  date_created: string
  internal_analysis_flag: boolean
}

export interface RelatedTicket {
  id: number
  summary: string
  status: string
  priority: string
  board: string
}

export interface TicketContext {
  ticket: TicketDetails
  company: CompanyInfo
  contact: ContactInfo | null
  configurations: ConfigItemInfo[]
  notes: TicketNote[]
  related_tickets: RelatedTicket[]
  fetched_at: string
}

export const psaContextApi = {
  getTicketContext: (ticketId: string | number): Promise<TicketContext> =>
    apiClient.get<TicketContext>(`/integrations/psa/tickets/${ticketId}/context`).then(r => r.data),
}
