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

export const identifierKeys = {
  all: (memberId: string) => ['members', memberId, 'identifiers'] as const,
}

export function identifierListOptions(memberId: string) {
  return queryOptions({
    queryKey: identifierKeys.all(memberId),
    queryFn: () => api.get<PaginatedResponse<MemberIdentifier>>(`/v1/members/${memberId}/identifiers`, { page: 1, limit: 100, order: 'asc' }),
  })
}

export const identifierMutations = {
  create: (memberId: string, data: Record<string, unknown>) =>
    api.post<MemberIdentifier>(`/v1/members/${memberId}/identifiers`, data),

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

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