import { account } from './appwrite'

const PROJECT_ADMIN_URL =
  import.meta.env.VITE_PROJECT_ADMIN_URL || 'https://project.webklar.com'

async function adminFetch(path, options = {}) {
  const jwt = await account.createJWT()
  const response = await fetch(`${PROJECT_ADMIN_URL}${path}`, {
    ...options,
    headers: {
      Authorization: `Bearer ${jwt.jwt}`,
      'Content-Type': 'application/json',
      ...options.headers,
    },
  })
  const data = await response.json().catch(() => ({}))
  if (!response.ok) throw new Error(data.error || `API-Fehler ${response.status}`)
  return data
}

export async function createProjectFromTemplate(payload) {
  return adminFetch('/api/admin/website-projects/create-from-template', {
    method: 'POST',
    body: JSON.stringify(payload),
  })
}

export async function syncGiteaRepos() {
  return adminFetch('/api/admin/gitea/sync-repos', { method: 'POST' })
}

export async function updateWebsiteProject(id, payload) {
  return adminFetch(`/api/admin/website-projects/${id}`, {
    method: 'PATCH',
    body: JSON.stringify(payload),
  })
}

export async function archiveWebsiteProject(id) {
  return adminFetch(`/api/admin/website-projects/${id}/archive`, { method: 'POST' })
}

export async function unarchiveWebsiteProject(id) {
  return adminFetch(`/api/admin/website-projects/${id}/unarchive`, { method: 'POST' })
}

export async function fetchProjectReadme(repoFullName) {
  if (!repoFullName) return { found: false }
  return adminFetch(
    `/api/admin/gitea/repo-readme?repoFullName=${encodeURIComponent(repoFullName)}`
  )
}
