import { useQuery } from '@tanstack/vue-query'
import type { components } from './generated/schema'
import { apiClient } from './client'
import { throwApiProblem } from './contracts'
import { queryKeys } from './queryClient'

export async function fetchSecurityStatus(
  signal?: AbortSignal,
): Promise<components['schemas']['SecurityStatusDto']> {
  const { data, error, response } = await apiClient.GET(
    '/api/v1/security/status',
    { signal },
  )
  if (!response.ok || error || !data) {
    await throwApiProblem(response, 'Security-Status konnte nicht geladen werden')
  }
  return data as components['schemas']['SecurityStatusDto']
}

export function useSecurityStatusQuery() {
  return useQuery({
    queryKey: queryKeys.securityStatus(),
    queryFn: ({ signal }) => fetchSecurityStatus(signal),
  })
}
