import { apiRequest, type ApiSession } from './client'

export type BackendInboxItem = {
  id?: number
  ID?: number
  project_id?: number
  projectID?: number
  source_type?: string
  sourceType?: string
  title?: string
  body?: string
  status?: string
}

export type InboxSuggestion = {
  type: string
  title: string
  body: string
}

export type CaptureInboxInput = {
  sourceType: string
  title: string
  body: string
}

export async function captureProjectInbox(session: ApiSession, projectId: string | number, input: CaptureInboxInput) {
  return apiRequest<BackendInboxItem>(`/api/projects/${projectId}/inbox`, {
    method: 'POST',
    token: session.token,
    body: {
      source_type: input.sourceType,
      title: input.title,
      body: input.body,
    },
  })
}

export async function analyzeInboxItem(session: ApiSession, inboxId: string | number) {
  return apiRequest<{ suggestions: InboxSuggestion[] }>(`/api/inbox/${inboxId}/analyze`, {
    method: 'POST',
    token: session.token,
  })
}

export async function confirmInboxItem(session: ApiSession, inboxId: string | number, suggestions: InboxSuggestion[]) {
  return apiRequest<void>(`/api/inbox/${inboxId}/confirm`, {
    method: 'POST',
    token: session.token,
    body: { suggestions },
  })
}
