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

export const accountKeys = {
  all: ['accounts'] as const,
  lists: () => [...accountKeys.all, 'list'] as const,
  list: (params: PaginationInput) => [...accountKeys.all, 'list', params] as const,
  details: () => [...accountKeys.all, 'detail'] as const,
  detail: (id: string) => [...accountKeys.all, 'detail', id] as const,
}

export function accountListOptions(params: PaginationInput) {
  return queryOptions({
    queryKey: accountKeys.list(params),
    queryFn: () => api.get<PaginatedResponse<Account>>('/v1/accounts', params),
  })
}

export function accountDetailOptions(id: string) {
  return queryOptions({
    queryKey: accountKeys.detail(id),
    queryFn: () => api.get<Account>(`/v1/accounts/${id}`),
  })
}

export const accountMutations = {
  create: (data: Record<string, unknown>) =>
    api.post<Account>('/v1/accounts', data),

  update: (id: string, data: Record<string, unknown>) =>
    api.patch<Account>(`/v1/accounts/${id}`, data),

  delete: (id: string) =>
    api.del<Account>(`/v1/accounts/${id}`),
}
