'use client';

import { useQuery } from '@tanstack/react-query';
import { api } from '@/lib/api';
import type { ListLedgerInput } from '@alqove/api-client';

export const STATEMENTS_KEYS = {
  list: (storeId: string, params: ListLedgerInput) =>
    [
      'statements',
      storeId,
      params.from ?? '',
      params.to ?? '',
      params.page ?? 1,
      params.per_page ?? 20,
    ] as const,
};

export function useStoreStatements(
  storeId: string | null | undefined,
  params: ListLedgerInput = {},
) {
  return useQuery({
    queryKey: storeId
      ? STATEMENTS_KEYS.list(storeId, params)
      : ['statements', 'none'],
    queryFn: () => api.statements.listForStore(storeId!, params),
    enabled: Boolean(storeId),
    staleTime: 30_000,
  });
}
