import { useState, useEffect, useCallback } from 'react';
import { autoLabService } from '../services/autoLabService';

export const useAutoLab = (type) => {
  const [data, setData] = useState([]);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState(null);

  const fetchData = useCallback(async () => {
    try {
      setLoading(true);
      let res;
      switch (type) {
        case 'veiculos':
          res = await autoLabService.getVeiculos();
          break;
        case 'companies':
          res = await autoLabService.getCompanies();
          break;
        case 'vendedores':
          res = await autoLabService.getVendedores();
          break;
        case 'setores':
          res = await autoLabService.getSetores();
          break;
        case 'bancos':
          res = await autoLabService.getBancos();
          break;
        case 'estoque':
          res = await autoLabService.getEstoque();
          break;
        case 'vendas':
          res = await autoLabService.getVendas();
          break;
        case 'usuarios':
          res = await autoLabService.getUsuarios();
          break;
        default:
          res = [];
      }
      setData(res);
      setError(null);
    } catch (err) {
      setError(err.message);
    } finally {
      setLoading(false);
    }
  }, [type]);

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

  return { data, loading, error, refresh: fetchData };
};
