import { api } from './chatApiService'

const BASE = '/api/secretaria'

// ─── Types ────────────────────────────────────────────────────────────────────

export interface SecAgent {
  id: string
  name: string
  description: string | null
  model: string
  provider: string
  temperature: number
  context_window: number
  active: boolean
  created_at: string
  updated_at: string
}

export type BrainNodeType = 'persona' | 'knowledge' | 'rules' | 'calendar' | 'escalation'

export interface BrainNode {
  id: string
  agent_id: string
  type: BrainNodeType
  title: string
  content: string
  active: boolean
  sort_order: number
  node_model: string | null   // modelo específico para este nó (opcional)
  created_at: string
  updated_at: string
}

export interface SecConversation {
  id: string
  agent_id: string
  contact_name: string
  protocol_number: string
  status: 'active' | 'closed' | 'escalated'
  summary: string | null
  created_at: string
  updated_at: string
}

export interface SecMessage {
  id: string
  conversation_id: string
  role: 'user' | 'assistant' | 'system'
  content: string
  created_at: string
}

export interface CalendarSlot {
  id: string
  title: string
  date: string
  time_start: string
  time_end: string
  attendee_name: string | null
  attendee_phone: string | null
  status: 'available' | 'booked' | 'cancelled'
  notes: string | null
  created_at: string
}

// ─── Agents ───────────────────────────────────────────────────────────────────

export const secAgentApi = {
  list: () => api.get<SecAgent[]>(`${BASE}/agents`).then((r) => r.data),
  create: (d: Partial<SecAgent>) => api.post<SecAgent>(`${BASE}/agents`, d).then((r) => r.data),
  update: (id: string, d: Partial<SecAgent>) => api.put<SecAgent>(`${BASE}/agents/${id}`, d).then((r) => r.data),
  delete: (id: string) => api.delete(`${BASE}/agents/${id}`).then((r) => r.data),
}

// ─── Brain Nodes ──────────────────────────────────────────────────────────────

export const secNodeApi = {
  list: (agentId: string) => api.get<BrainNode[]>(`${BASE}/agents/${agentId}/nodes`).then((r) => r.data),
  create: (agentId: string, d: Partial<BrainNode>) =>
    api.post<BrainNode>(`${BASE}/agents/${agentId}/nodes`, d).then((r) => r.data),
  update: (id: string, d: Partial<BrainNode>) => api.put<BrainNode>(`${BASE}/nodes/${id}`, d).then((r) => r.data),
  delete: (id: string) => api.delete(`${BASE}/nodes/${id}`).then((r) => r.data),
}

// ─── Conversations ────────────────────────────────────────────────────────────

export const secConvApi = {
  list: (agentId?: string) =>
    api.get<SecConversation[]>(`${BASE}/conversations`, { params: agentId ? { agent_id: agentId } : {} }).then((r) => r.data),
  create: (agentId: string, contactName: string) =>
    api.post<SecConversation>(`${BASE}/conversations`, { agent_id: agentId, contact_name: contactName }).then((r) => r.data),
  patch: (id: string, d: Partial<SecConversation>) =>
    api.patch<SecConversation>(`${BASE}/conversations/${id}`, d).then((r) => r.data),
  delete: (id: string) => api.delete(`${BASE}/conversations/${id}`).then((r) => r.data),
  messages: (id: string) => api.get<SecMessage[]>(`${BASE}/conversations/${id}/messages`).then((r) => r.data),
  chat: (id: string, message: string) =>
    api.post<{ reply: string }>(`${BASE}/conversations/${id}/chat`, { message }).then((r) => r.data),
  finalize: (id: string) =>
    api.post<{ summary: string; protocol_number: string }>(`${BASE}/conversations/${id}/finalize`).then((r) => r.data),
}

// ─── Numbers ──────────────────────────────────────────────────────────────────

export type NumberRole =
  | 'secretary_virtual'
  | 'clinic'
  | 'doctor'
  | 'specialist'
  | 'manager'
  | 'reserve'
  | 'human_secretary'

export interface SecNumber {
  id: string
  instance_id: string
  label: string
  role: NumberRole
  area: string | null
  priority: number
  active: boolean
  notes: string | null
  created_at: string
  updated_at: string
}

export const secNumberApi = {
  list: () => api.get<SecNumber[]>(`${BASE}/numbers`).then((r) => r.data),
  // upsert por instance_id — cria ou atualiza o papel da instância
  assign: (d: Partial<SecNumber>) => api.post<SecNumber>(`${BASE}/numbers`, d).then((r) => r.data),
  update: (id: string, d: Partial<SecNumber>) => api.put<SecNumber>(`${BASE}/numbers/${id}`, d).then((r) => r.data),
  delete: (id: string) => api.delete(`${BASE}/numbers/${id}`).then((r) => r.data),
}

// ─── Calendar ─────────────────────────────────────────────────────────────────

export const secCalApi = {
  list: (params?: { from?: string; to?: string; status?: string }) =>
    api.get<CalendarSlot[]>(`${BASE}/calendar`, { params }).then((r) => r.data),
  create: (d: Partial<CalendarSlot>) => api.post<CalendarSlot>(`${BASE}/calendar`, d).then((r) => r.data),
  update: (id: string, d: Partial<CalendarSlot>) => api.put<CalendarSlot>(`${BASE}/calendar/${id}`, d).then((r) => r.data),
  delete: (id: string) => api.delete(`${BASE}/calendar/${id}`).then((r) => r.data),
}
