import { useState, useEffect, useCallback } from 'react';
import { api } from '@/lib/api';
import type { Asset } from '@/types';

export function useAssets(params?: { accountId?: string; type?: string }) {
  const [assets, setAssets] = useState<Asset[]>([]);
  const [loading, setLoading] = useState(true);

  const refresh = useCallback(() => {
    setLoading(true);
    api.listAssets(params).then(setAssets).finally(() => setLoading(false));
  }, [params?.accountId, params?.type]);

  useEffect(() => { refresh(); }, [refresh]);

  const remove = (id: string) => api.deleteAsset(id).then(refresh);

  return { assets, loading, refresh, remove };
}
