import { apiClient } from '@/shared/api/client'

export type ChecklistResultDto = {
  passed: boolean
  comment: string | null
  checkedAt: string
}

export type TestChecklistResponse = {
  results: Record<string, ChecklistResultDto>
}

export type UpdateChecklistItemResponse = {
  itemKey: string
  passed: boolean
  comment: string | null
  checkedAt: string
}

export async function fetchTestChecklistResults(): Promise<TestChecklistResponse> {
  const { data } = await apiClient.get<TestChecklistResponse>('admin/test-checklist')
  return data
}

export async function updateTestChecklistItem(
  itemKey: string,
  passed: boolean,
  comment?: string | null,
): Promise<UpdateChecklistItemResponse> {
  const { data } = await apiClient.patch<{ result: UpdateChecklistItemResponse }>('admin/test-checklist', {
    itemKey,
    passed,
    comment: passed ? null : (comment ?? null),
  })
  return data.result
}

export async function resetTestChecklist(): Promise<void> {
  await apiClient.post('admin/test-checklist/reset')
}
