interface RegisterPayload {
  name: string
  last_name: string
  cpf: string
  registration: string
  email: string
  phone: string
  position_id: number | null
  sector_id: number | null
}

export interface SelectOption {
  id: number
  name: string
}

interface ApiResponse<T> {
  payload: T
}

export const useRegisterApi = () => {
  const { apiUrl } = useRuntimeConfig().public

  const registerLandingUser = async (data: RegisterPayload): Promise<void> => {
    await $fetch(`${apiUrl}/api/landing-page-register-user`, {
      method: 'POST',
      body: data,
    })
  }

  const { data: positionsData } = useAsyncData('public-positions', () =>
    $fetch<ApiResponse<SelectOption[]>>(`${apiUrl}/api/public/positions`).catch(() => null),
  )

  const { data: sectorsData } = useAsyncData('public-sectors', () =>
    $fetch<ApiResponse<SelectOption[]>>(`${apiUrl}/api/public/sectors`).catch(() => null),
  )

  const positions = computed<SelectOption[]>(() => positionsData.value?.payload ?? [])
  const sectors = computed<SelectOption[]>(() => sectorsData.value?.payload ?? [])

  return { registerLandingUser, positions, sectors }
}
