import { apiClient } from './client'
import type { PsaConnectionResponse, PsaConnectionCreate, PsaConnectionUpdate, PsaConnectionTestResponse } from '@/types'
import type { PSABoard, TicketLinkResponse, PSATicketInfo, PSATicketStatusItem, PsaPreviewResponse, PsaPostResponse, PsaPostLogEntry, PsaMemberResponse, PsaMemberMappingResponse, AutoMatchResult, FlowpilotSettings } from '@/types/integrations'
import type { TicketListResponse } from '@/types/tickets'

export const integrationsApi = {
  getConnection: () =>
    apiClient.get<PsaConnectionResponse | null>('/integrations/psa/connections').then(r => r.data),
  createConnection: (data: PsaConnectionCreate) =>
    apiClient.post<PsaConnectionResponse>('/integrations/psa/connections', data).then(r => r.data),
  updateConnection: (id: string, data: PsaConnectionUpdate) =>
    apiClient.put<PsaConnectionResponse>(`/integrations/psa/connections/${id}`, data).then(r => r.data),
  deleteConnection: (id: string) =>
    apiClient.delete(`/integrations/psa/connections/${id}`),
  testConnection: (id: string) =>
    apiClient.post<PsaConnectionTestResponse>(`/integrations/psa/connections/${id}/test`).then(r => r.data),
  listBoards: () =>
    apiClient.get<PSABoard[]>('/integrations/psa/boards').then(r => r.data),
  searchTickets: (params: { query?: string; board_id?: number; include_closed?: boolean }): Promise<TicketListResponse> =>
    apiClient.get<TicketListResponse>('/integrations/psa/tickets/search', { params }).then(r => r.data),
  searchTicketsQueue: (params: {
    assigned_to_me?: boolean
    unassigned?: boolean
    board_ids?: string
    page?: number
    page_size?: number
  }): Promise<TicketListResponse> =>
    apiClient.get<TicketListResponse>('/integrations/psa/tickets/search', { params }).then(r => r.data),
  getTicket: (id: string) =>
    apiClient.get<PSATicketInfo>(`/integrations/psa/tickets/${id}`).then(r => r.data),
  getTicketStatuses: (ticketId: string) =>
    apiClient.get<PSATicketStatusItem[]>(`/integrations/psa/tickets/${ticketId}/statuses`).then(r => r.data),
  getBoardStatuses: (boardId: number | string) =>
    apiClient.get<PSATicketStatusItem[]>(`/integrations/psa/boards/${boardId}/statuses`).then(r => r.data),
  listMembers: () =>
    apiClient.get<PsaMemberResponse[]>('/integrations/psa/members').then(r => r.data),
  getMemberMappings: () =>
    apiClient.get<PsaMemberMappingResponse[]>('/integrations/psa/member-mappings').then(r => r.data),
  saveMemberMappings: (mappings: { user_id: string; external_member_id: string; external_member_name: string }[]) =>
    apiClient.post<PsaMemberMappingResponse[]>('/integrations/psa/member-mappings', mappings).then(r => r.data),
  autoMatchMembers: () =>
    apiClient.post<AutoMatchResult>('/integrations/psa/member-mappings/auto-match').then(r => r.data),
  getFlowpilotSettings: (connectionId: string) =>
    apiClient.get<FlowpilotSettings>(`/integrations/psa/connections/${connectionId}/flowpilot-settings`).then(r => r.data),
  updateFlowpilotSettings: (connectionId: string, data: Partial<FlowpilotSettings>) =>
    apiClient.put<FlowpilotSettings>(`/integrations/psa/connections/${connectionId}/flowpilot-settings`, data).then(r => r.data),
}

export const sessionPsaApi = {
  linkTicket: (sessionId: string, psaTicketId: string | null) =>
    apiClient.patch<TicketLinkResponse>(`/sessions/${sessionId}/ticket-link`, { psa_ticket_id: psaTicketId }).then(r => r.data),
  getPostPreview: (sessionId: string) =>
    apiClient.get<PsaPreviewResponse>(`/sessions/${sessionId}/psa-post/preview`).then(r => r.data),
  postToTicket: (sessionId: string, data: { note_type: string; content: string; update_status_id?: number }) =>
    apiClient.post<PsaPostResponse>(`/sessions/${sessionId}/psa-post`, data).then(r => r.data),
  getPostHistory: (sessionId: string) =>
    apiClient.get<PsaPostLogEntry[]>(`/sessions/${sessionId}/psa-posts`).then(r => r.data),
}
