const BASE = '/api'

// Backend-Fehler (400/409 mit detail) als Error werfen statt sie zu verschlucken
async function jsonOrThrow(res) {
  if (!res.ok) {
    let detail = `Fehler (HTTP ${res.status})`
    try {
      const data = await res.json()
      if (data.detail) detail = typeof data.detail === 'string' ? data.detail : JSON.stringify(data.detail)
    } catch { /* kein JSON-Body */ }
    throw new Error(detail)
  }
  return res.json()
}

export async function fetchGuides() {
  const res = await fetch(`${BASE}/guides`)
  return res.json()
}

export async function createGuide(topic, format, instructions = '', provider = 'claude', abStep = null) {
  const res = await fetch(`${BASE}/guides`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ topic, format, instructions, provider, ab_step: abStep }),
  })
  return jsonOrThrow(res)
}

export async function fetchActiveBlocks() {
  const res = await fetch(`${BASE}/blocks/active`)
  return res.json()
}

export async function fetchBlocksStatus(topic) {
  const res = await fetch(`${BASE}/blocks/status?topic=${encodeURIComponent(topic)}`)
  return res.json()
}

export async function createBlocks(topic, instructions = '', provider = 'claude', sourceType = 'thema', sourceOrt = '', research = true, qaForce = false) {
  const res = await fetch(`${BASE}/blocks`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ topic, instructions, provider, source_type: sourceType, source_location: sourceOrt, research, qa_force: qaForce }),
  })
  return jsonOrThrow(res)
}

// Live-Kanban-Board der Blocks-Erzeugung (Spalten + Karten + Agenten + Dead-Letter).
export async function fetchBlocksBoard(topic) {
  const res = await fetch(`${BASE}/blocks/board?topic=${encodeURIComponent(topic)}`)
  return jsonOrThrow(res)
}

// Manueller QA-Lauf (wie das Gate, inkl. LLM-Stichprobe); Badge liest den neuen Report.
export async function runQa(topic, llm = true) {
  const res = await fetch(`${BASE}/blocks/qa`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ topic, llm }),
  })
  return jsonOrThrow(res)
}

// QA-Befunde gezielt beheben (Hygiene, bestätigte Dubletten, Fremd/Unecht nach Gegen-Judge).
export async function runRepair(topic) {
  const res = await fetch(`${BASE}/blocks/repair`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ topic }),
  })
  return jsonOrThrow(res)
}

// Karten ab Spalte zurücksetzen (keine Generierung).
export async function resetBlocksStage(topic, board, stage) {
  const res = await fetch(`${BASE}/blocks/reset-stage`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ topic, board, stage }),
  })
  return jsonOrThrow(res)
}

// Einen weiteren Research-Agenten anhängen (Attach-or-Start).
export async function addBlocksResearch(topic, provider = 'claude') {
  const res = await fetch(`${BASE}/blocks/research?topic=${encodeURIComponent(topic)}&provider=${encodeURIComponent(provider)}`, { method: 'POST' })
  return jsonOrThrow(res)
}

export async function restartBlocksCard(topic, cardId) {
  const res = await fetch(`${BASE}/blocks/card-restart`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ topic, card_id: cardId }),
  })
  return jsonOrThrow(res)
}

export async function removeGuideFormat(topic, format) {
  const res = await fetch(`${BASE}/guides/board/remove`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ topic, format }),
  })
  return jsonOrThrow(res)
}

export async function resetGuideCard(topic, format, blockNorm, abStage) {
  const res = await fetch(`${BASE}/guides/board/card-reset`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ topic, format, block_norm: blockNorm, ab_stage: abStage }),
  })
  return jsonOrThrow(res)
}

export async function requeueBlocksDead(topic) {
  const res = await fetch(`${BASE}/blocks/requeue-dead?topic=${encodeURIComponent(topic)}`, { method: 'POST' })
  return jsonOrThrow(res)
}

// Live-Board der Guide-Erzeugung.
export async function fetchGuideBoard(topic, format = 'Guide') {
  const res = await fetch(`${BASE}/guides/board?topic=${encodeURIComponent(topic)}&format=${encodeURIComponent(format)}`)
  return jsonOrThrow(res)
}

export async function resetGuideBoard(topic, format, abStage) {
  const res = await fetch(`${BASE}/guides/board/reset`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ topic, format, ab_stage: abStage }),
  })
  return jsonOrThrow(res)
}

// Befunde beheben: Karten mit QA-Befunden zurück auf Prüfen + Resume-Lauf.
export async function repairGuideBoard(topic, format) {
  const res = await fetch(`${BASE}/guides/board/repair`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ topic, format, ab_stage: 0 }),
  })
  return jsonOrThrow(res)
}

export async function cancelBlocks(topic) {
  await fetch(`${BASE}/blocks/cancel?topic=${encodeURIComponent(topic)}`, { method: 'POST' })
}

export async function deleteBlocks(topic) {
  await fetch(`${BASE}/blocks?topic=${encodeURIComponent(topic)}`, { method: 'DELETE' })
}

// --- Block-Learning: Chat, Exam ---

export async function fetchBlockLearnState(topic) {
  const res = await fetch(`${BASE}/blocks/learnstate?topic=${encodeURIComponent(topic)}`)
  return jsonOrThrow(res)
}

export async function chatBlock({ topic, block, section, section_compact = '', messages, provider }) {
  const res = await fetch(`${BASE}/blocks/chat`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ topic, block, section, section_compact, messages, provider }),
  })
  return jsonOrThrow(res)
}

export async function examBlock({
  topic, block, section, section_compact = '', provider,
  action = 'question', question = '', last_rating = '', avoid = [],
  asked_again = false, reason = '', pattern = '', cap = 6, messages = [], thorough = false,
  selection = [], correct = [], solution = '', alternatives = [], input = '', schwer = false,
}) {
  const res = await fetch(`${BASE}/blocks/exam`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ topic, block, section, section_compact, action, question, last_rating, avoid, asked_again, reason, pattern, cap, messages, provider, thorough, selection, correct, solution, alternatives, input, schwer }),
  })
  return jsonOrThrow(res)
}

export async function fetchQuestionPattern(topic, block) {
  const res = await fetch(`${BASE}/blocks/question-pattern?topic=${encodeURIComponent(topic)}&block=${encodeURIComponent(block)}`)
  return jsonOrThrow(res)
}

export async function fetchTopicProgress(topic) {
  const res = await fetch(`${BASE}/topics/progress?topic=${encodeURIComponent(topic)}`)
  return res.json()
}

export async function fetchStats() {
  const res = await fetch(`${BASE}/stats`)
  return res.json()
}

export async function fetchProviders() {
  const res = await fetch(`${BASE}/providers`)
  return res.json()
}

export async function fetchFolders(kind) {
  const res = await fetch(`${BASE}/folders?kind=${encodeURIComponent(kind)}`)
  return jsonOrThrow(res)
}

export async function fetchSource(topic) {
  const res = await fetch(`${BASE}/blocks/source?topic=${encodeURIComponent(topic)}`)
  return jsonOrThrow(res)
}

export async function updateSource(topic, { type, ort = '', spec = '' }) {
  const res = await fetch(`${BASE}/blocks/source`, {
    method: 'PUT',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ topic, type, location: ort, spec }),
  })
  return jsonOrThrow(res)
}

export async function fetchBlocksCompleteness(topic) {
  const res = await fetch(`${BASE}/blocks/completeness?topic=${encodeURIComponent(topic)}`)
  return jsonOrThrow(res)
}

export async function fetchBlocksOverview(topic) {
  const res = await fetch(`${BASE}/blocks/overview?topic=${encodeURIComponent(topic)}`)
  return jsonOrThrow(res)
}

export async function cancelGuide(id) {
  await fetch(`${BASE}/guides/${id}/cancel`, { method: 'POST' })
}

export async function deleteGuide(id, slots = false) {
  await fetch(`${BASE}/guides/${id}${slots ? '?slots=1' : ''}`, { method: 'DELETE' })
}

export async function fetchGuideContent(id, level = 4) {
  const res = await fetch(`${BASE}/guides/${id}/content?level=${level}`)
  if (!res.ok) throw new Error(`Content not available (${res.status})`)
  return res.json()
}

// Übungspool: fällige + neue Flashcards des Themas (Leitner, ein Stapel).
export async function fetchPracticeDeck(topic) {
  const res = await fetch(`${BASE}/practice/deck?topic=${encodeURIComponent(topic)}`)
  return jsonOrThrow(res)
}

// Leitner-Schritt buchen (correct = „Gewusst").
export async function answerPracticeCard({ topic, block_norm, sub_norm, correct }) {
  const res = await fetch(`${BASE}/practice/answer`, {
    method: 'POST', headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ topic, block_norm, sub_norm, correct }),
  })
  return jsonOrThrow(res)
}

// Einen Markdown-Block on-demand gegen die Guide-Rules prüfen (Fokus, Rechtsklick).
export async function pruefeBlock(id, { block, spot, snippet, hint = '', provider }) {
  const res = await fetch(`${BASE}/guides/${id}/block/pruefen`, {
    method: 'POST', headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ block, spot, snippet, hint, provider }),
  })
  return jsonOrThrow(res)
}

// Geprüften Block persistent übernehmen (alt → new im jeweiligen Feld).
export async function uebernehmeBlock(id, { block, spot, alt, revised, provider }) {
  const res = await fetch(`${BASE}/guides/${id}/block/uebernehmen`, {
    method: 'POST', headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ block, spot, alt, revised, provider }),
  })
  return jsonOrThrow(res)
}

// Reset a block's learning progress to zero (score/streak/flags/open question).
export async function resetBlockProgress(topic, block) {
  const res = await fetch(`${BASE}/blocks/progress?topic=${encodeURIComponent(topic)}&block=${encodeURIComponent(block)}`, {
    method: 'DELETE',
  })
  return jsonOrThrow(res)
}

export async function fetchTopics() {
  const res = await fetch(`${BASE}/topics`)
  return res.json()
}

export async function createTopic(name) {
  await fetch(`${BASE}/topics`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ name }),
  })
}

export async function deleteTopic(name) {
  await fetch(`${BASE}/topics?topic=${encodeURIComponent(name)}`, { method: 'DELETE' })
}

export async function chatGuide(id, { section, outline, messages, provider = 'claude' }) {
  const res = await fetch(`${BASE}/guides/${id}/chat`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ section, outline, messages, provider }),
  })
  return res.json()
}

