import { queryOptions } from '@tanstack/react-query'
import { api } from '@/lib/api-client'
import type { ProcessorLink } from '@/types/account'
import type { PaginatedResponse, PaginationInput } from '@lunarfront/shared/schemas'

export const processorLinkKeys = {
  all: (accountId: string) => ['accounts', accountId, 'processor-links'] as const,
  list: (accountId: string, params: PaginationInput) => [...processorLinkKeys.all(accountId), params] as const,
}

export function processorLinkListOptions(accountId: string, params: PaginationInput) {
  return queryOptions({
    queryKey: processorLinkKeys.list(accountId, params),
    queryFn: () => api.get<PaginatedResponse<ProcessorLink>>(`/v1/accounts/${accountId}/processor-links`, params),
  })
}

export const processorLinkMutations = {
  create: (accountId: string, data: Record<string, unknown>) =>
    api.post<ProcessorLink>(`/v1/accounts/${accountId}/processor-links`, data),

  update: (id: string, data: Record<string, unknown>) =>
    api.patch<ProcessorLink>(`/v1/processor-links/${id}`, data),

  delete: (id: string) =>
    api.del<ProcessorLink>(`/v1/processor-links/${id}`),
}
