import { createEntityHook } from './useEntityBase';
import type { CashWalletDTO, GoldWalletDTO } from '@/types';
import { ENDPOINTS } from '@/constants/endpoints';
import { api } from '@/lib/api';
import i18n from "@/lib/i18n";

export const useCashWallets = createEntityHook<CashWalletDTO, never, never>(
  i18n.t('hooks.useWallets.text_1'),
  ENDPOINTS.CASH_WALLET.LIST,
);

export const useGoldWallets = createEntityHook<GoldWalletDTO, never, never>(
  i18n.t('hooks.useWallets.text_2'),
  ENDPOINTS.GOLD_WALLET.LIST,
);

export function useWalletLedger() {
  const getCashLedger = async (walletId: string, skip = 0, limit = 50, entry_type?: string, from_date?: string, to_date?: string) => {
    const params = new URLSearchParams({ skip: String(skip), limit: String(limit) });
    if (entry_type && entry_type !== "all") params.append("entry_type", entry_type);
    if (from_date) params.append("from_date", from_date);
    if (to_date) params.append("to_date", to_date);
    
    const res = await api.get(`${ENDPOINTS.CASH_WALLET.LEDGER(walletId)}?${params.toString()}`);
    return res?.data || res || [];
  };

  const getGoldLedger = async (walletId: string, skip = 0, limit = 50, entry_type?: string, from_date?: string, to_date?: string) => {
    const params = new URLSearchParams({ skip: String(skip), limit: String(limit) });
    if (entry_type && entry_type !== "all") params.append("entry_type", entry_type);
    if (from_date) params.append("from_date", from_date);
    if (to_date) params.append("to_date", to_date);

    const res = await api.get(`${ENDPOINTS.GOLD_WALLET.LEDGER(walletId)}?${params.toString()}`);
    return res?.data || res || [];
  };

  return { getCashLedger, getGoldLedger };
}