import { apiClient } from '@/shared/api/client'
import type { InfoPageBlock } from '../model/types'

export async function fetchPublicInfoBlocks(): Promise<{ items: InfoPageBlock[] }> {
  const { data } = await apiClient.get<{ items: InfoPageBlock[] }>('info-page/blocks')
  return data
}

export async function fetchAdminInfoBlocks(): Promise<{ items: InfoPageBlock[] }> {
  const { data } = await apiClient.get<{ items: InfoPageBlock[] }>('admin/info-page/blocks')
  return data
}

export async function createInfoBlock(
  body: Pick<InfoPageBlock, 'key' | 'title' | 'body' | 'sort' | 'published'>,
): Promise<{ item: InfoPageBlock }> {
  const { data } = await apiClient.post<{ item: InfoPageBlock }>('admin/info-page/blocks', body)
  return data
}

export async function updateInfoBlock(
  id: string,
  body: Partial<Pick<InfoPageBlock, 'key' | 'title' | 'body' | 'sort' | 'published'>>,
): Promise<{ item: InfoPageBlock }> {
  const { data } = await apiClient.patch<{ item: InfoPageBlock }>(`admin/info-page/blocks/${id}`, body)
  return data
}

export async function deleteInfoBlock(id: string): Promise<void> {
  await apiClient.delete(`admin/info-page/blocks/${id}`)
}
