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

export const paymentMethodKeys = {
  all: (accountId: string) => ['accounts', accountId, 'payment-methods'] as const,
  list: (accountId: string, params: PaginationInput) => [...paymentMethodKeys.all(accountId), params] as const,
}

export function paymentMethodListOptions(accountId: string, params: PaginationInput) {
  return queryOptions({
    queryKey: paymentMethodKeys.list(accountId, params),
    queryFn: () => api.get<PaginatedResponse<PaymentMethod>>(`/v1/accounts/${accountId}/payment-methods`, params),
  })
}

export const paymentMethodMutations = {
  create: (accountId: string, data: Record<string, unknown>) =>
    api.post<PaymentMethod>(`/v1/accounts/${accountId}/payment-methods`, data),

  update: (id: string, data: Record<string, unknown>) =>
    api.patch<PaymentMethod>(`/v1/payment-methods/${id}`, data),

  delete: (id: string) =>
    api.del<PaymentMethod>(`/v1/payment-methods/${id}`),
}
