/**
 * SDK API Client — Wiki (read-only knowledge base) methods.
 * (listWikiCategories, listWikiArticles, getWikiArticle, searchWiki)
 */

import { FetchContext } from './api-client-types'
import { WikiCategory, WikiArticle, WikiSearchResult } from './types'

/**
 * List all wiki categories with article counts
 */
export async function listWikiCategories(ctx: FetchContext): Promise<WikiCategory[]> {
  const data = await ctx.fetchWithRetry<{ categories: Array<{
    id: string; name: string; description: string; icon: string;
    sort_order: number; article_count: number
  }> }>(
    `${ctx.baseUrl}/wiki?endpoint=categories`,
    { method: 'GET' }
  )
  return (data.categories || []).map(c => ({
    id: c.id,
    name: c.name,
    description: c.description,
    icon: c.icon,
    sortOrder: c.sort_order,
    articleCount: c.article_count,
  }))
}

/**
 * List wiki articles, optionally filtered by category
 */
export async function listWikiArticles(
  ctx: FetchContext,
  categoryId?: string
): Promise<WikiArticle[]> {
  const params = new URLSearchParams({ endpoint: 'articles' })
  if (categoryId) params.set('category_id', categoryId)
  const data = await ctx.fetchWithRetry<{ articles: Array<{
    id: string; category_id: string; category_name: string; title: string;
    summary: string; content: string; legal_refs: string[]; tags: string[];
    relevance: string; source_urls: string[]; version: number; updated_at: string
  }> }>(
    `${ctx.baseUrl}/wiki?${params.toString()}`,
    { method: 'GET' }
  )
  return (data.articles || []).map(a => ({
    id: a.id,
    categoryId: a.category_id,
    categoryName: a.category_name,
    title: a.title,
    summary: a.summary,
    content: a.content,
    legalRefs: a.legal_refs || [],
    tags: a.tags || [],
    relevance: a.relevance as WikiArticle['relevance'],
    sourceUrls: a.source_urls || [],
    version: a.version,
    updatedAt: a.updated_at,
  }))
}

/**
 * Get a single wiki article by ID
 */
export async function getWikiArticle(
  ctx: FetchContext,
  id: string
): Promise<WikiArticle> {
  const data = await ctx.fetchWithRetry<{
    id: string; category_id: string; category_name: string; title: string;
    summary: string; content: string; legal_refs: string[]; tags: string[];
    relevance: string; source_urls: string[]; version: number; updated_at: string
  }>(
    `${ctx.baseUrl}/wiki?endpoint=article&id=${encodeURIComponent(id)}`,
    { method: 'GET' }
  )
  return {
    id: data.id,
    categoryId: data.category_id,
    categoryName: data.category_name,
    title: data.title,
    summary: data.summary,
    content: data.content,
    legalRefs: data.legal_refs || [],
    tags: data.tags || [],
    relevance: data.relevance as WikiArticle['relevance'],
    sourceUrls: data.source_urls || [],
    version: data.version,
    updatedAt: data.updated_at,
  }
}

/**
 * Full-text search across wiki articles
 */
export async function searchWiki(
  ctx: FetchContext,
  query: string
): Promise<WikiSearchResult[]> {
  const data = await ctx.fetchWithRetry<{ results: Array<{
    id: string; title: string; summary: string; category_name: string;
    relevance: string; highlight: string
  }> }>(
    `${ctx.baseUrl}/wiki?endpoint=search&q=${encodeURIComponent(query)}`,
    { method: 'GET' }
  )
  return (data.results || []).map(r => ({
    id: r.id,
    title: r.title,
    summary: r.summary,
    categoryName: r.category_name,
    relevance: r.relevance,
    highlight: r.highlight,
  }))
}
