const BASE = '/api'

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

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

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

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

export async function deleteProject(name) {
  await fetch(`${BASE}/projects/${encodeURIComponent(name)}`, { method: 'DELETE' })
}

export async function reworkGuide(id, instructions) {
  const res = await fetch(`${BASE}/guides/${id}/rework`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ instructions }),
  })
  return res.json()
}

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

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

export function pdfUrl(id) {
  return `${BASE}/guides/${id}/pdf`
}

export function htmlUrl(id) {
  return `${BASE}/guides/${id}/html`
}

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

export async function setProgress(id, chapter, done) {
  const res = await fetch(`${BASE}/guides/${id}/progress`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ chapter, done }),
  })
  return res.json()
}

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

export async function suggestTopics(problem) {
  const res = await fetch(`${BASE}/topic-suggestions`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ problem }),
  })
  return res.json()
}

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

export async function createBaustein(topic, title, instructions = '') {
  const res = await fetch(`${BASE}/bausteine`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ topic, title, instructions }),
  })
  return res.json()
}

export async function deleteBaustein(id) {
  await fetch(`${BASE}/bausteine/${id}`, { method: 'DELETE' })
}

export async function reworkBaustein(id, instructions) {
  await fetch(`${BASE}/bausteine/${id}/rework`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ instructions }),
  })
}

export async function sortBausteine(topic, instructions = '') {
  await fetch(`${BASE}/bausteine/sort?topic=${encodeURIComponent(topic)}`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ instructions }),
  })
}

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

export async function fetchSuggestions(topic) {
  const res = await fetch(`${BASE}/bausteine/suggestions?topic=${encodeURIComponent(topic)}`)
  return res.json()
}

export async function generateSuggestions(topic) {
  await fetch(`${BASE}/bausteine/suggestions/generate?topic=${encodeURIComponent(topic)}`, { method: 'POST' })
}

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

export async function addSuggestion(id) {
  const res = await fetch(`${BASE}/bausteine/suggestions/${id}/add`, { method: 'POST' })
  return res.json()
}

export async function ignoreSuggestion(id) {
  await fetch(`${BASE}/bausteine/suggestions/${id}/ignore`, { method: 'POST' })
}
