import apiClient from './client'
import type {
  NetworkDiagramResponse,
  NetworkDiagramListItem,
  NetworkDiagramCreate,
  NetworkDiagramUpdate,
  AIGenerateRequest,
  AIGenerateResponse,
  DiagramImportData,
  DiagramImportResponse,
  DiagramExportResponse,
} from '@/types'

export const networkDiagramsApi = {
  async list(params?: { client_name?: string; search?: string }): Promise<NetworkDiagramListItem[]> {
    const response = await apiClient.get<NetworkDiagramListItem[]>('/network-diagrams/', { params })
    return response.data
  },

  async get(id: string): Promise<NetworkDiagramResponse> {
    const response = await apiClient.get<NetworkDiagramResponse>(`/network-diagrams/${id}`)
    return response.data
  },

  async create(data: NetworkDiagramCreate): Promise<NetworkDiagramResponse> {
    const response = await apiClient.post<NetworkDiagramResponse>('/network-diagrams/', data)
    return response.data
  },

  async update(id: string, data: NetworkDiagramUpdate): Promise<NetworkDiagramResponse> {
    const response = await apiClient.put<NetworkDiagramResponse>(`/network-diagrams/${id}`, data)
    return response.data
  },

  async archive(id: string): Promise<void> {
    await apiClient.delete(`/network-diagrams/${id}`)
  },

  async duplicate(id: string): Promise<NetworkDiagramResponse> {
    const response = await apiClient.post<NetworkDiagramResponse>(`/network-diagrams/${id}/duplicate`)
    return response.data
  },

  async exportJson(id: string): Promise<DiagramExportResponse> {
    const response = await apiClient.get<DiagramExportResponse>(`/network-diagrams/${id}/export`)
    return response.data
  },

  async importJson(data: DiagramImportData): Promise<DiagramImportResponse> {
    const response = await apiClient.post<DiagramImportResponse>('/network-diagrams/import', data)
    return response.data
  },

  async uploadThumbnail(id: string, dataUrl: string): Promise<void> {
    await apiClient.post(`/network-diagrams/${id}/thumbnail`, { data_url: dataUrl })
  },

  async aiGenerate(data: AIGenerateRequest): Promise<AIGenerateResponse> {
    const response = await apiClient.post<AIGenerateResponse>('/network-diagrams/ai-generate', data)
    return response.data
  },

  async listClients(): Promise<string[]> {
    const response = await apiClient.get<string[]>('/network-diagrams/clients')
    return response.data
  },
}
