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

export type AISessionDTO = {
  id: string
  projectId: string
  title: string
  context: string
  status: string
  expert: AIExpertDTO | null
  createdAt: string
  updatedAt: string
}

export type AIExpertDTO = {
  id: string
  slug: string
  category: string
  categoryName: string
  name: string
  description: string
  emoji: string
  color: string
}

export type AIExpertDetailDTO = AIExpertDTO & {
  systemPrompt: string
  source: string
  sourceLicense: string
}

export type CreateAISessionInput = {
  title: string
  context: string
  expertId?: string
}

export async function listAIExperts(session: ApiSession, signal?: AbortSignal) {
  return apiRequest<AIExpertDTO[]>('/api/v1/ai-experts', {
    token: session.token,
    signal,
  })
}

export async function getAIExpert(session: ApiSession, expertId: string, signal?: AbortSignal) {
  return apiRequest<AIExpertDetailDTO>(`/api/v1/ai-experts/${expertId}`, {
    token: session.token,
    signal,
  })
}

export async function listAISessions(session: ApiSession, projectId: string, signal?: AbortSignal) {
  return apiRequest<AISessionDTO[]>(`/api/v1/projects/${projectId}/ai-sessions`, {
    token: session.token,
    signal,
  })
}

export async function createAISession(session: ApiSession, projectId: string, input: CreateAISessionInput, signal?: AbortSignal) {
  return apiRequest<AISessionDTO>(`/api/v1/projects/${projectId}/ai-sessions`, {
    method: 'POST',
    token: session.token,
    body: input,
    signal,
  })
}
