import type { FileResponse } from '../types'

const API_BASE = '/api'

async function handleResponse<T>(response: Response): Promise<T> {
  if (!response.ok) {
    const error = await response.json().catch(() => ({ error: 'Unknown error' }))
    throw new Error(error.error || `HTTP ${response.status}`)
  }
  if (response.status === 204) {
    return null as T
  }
  return response.json()
}

export const api = {
  async listFiles(): Promise<string[]> {
    const response = await fetch(`${API_BASE}/files`)
    return handleResponse<string[]>(response)
  },

  async getFile(name: string): Promise<FileResponse> {
    const response = await fetch(`${API_BASE}/files/${encodeURIComponent(name)}`)
    return handleResponse<FileResponse>(response)
  },

  async createFile(name: string, content: string): Promise<FileResponse> {
    const response = await fetch(`${API_BASE}/files`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ name, content }),
    })
    return handleResponse<FileResponse>(response)
  },

  async updateFile(name: string, content: string): Promise<FileResponse> {
    const response = await fetch(`${API_BASE}/files/${encodeURIComponent(name)}`, {
      method: 'PUT',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ content }),
    })
    return handleResponse<FileResponse>(response)
  },

  async deleteFile(name: string): Promise<void> {
    const response = await fetch(`${API_BASE}/files/${encodeURIComponent(name)}`, {
      method: 'DELETE',
    })
    return handleResponse<void>(response)
  },
}
