import { useState, useCallback } from 'react';

/**
 * @typedef {Object} Vehicle
 * @property {number} id
 * @property {string} vehicle
 * @property {string} plate
 * @property {string} driver
 * @property {string} status
 */

/**
 * Hook customizado para gestão de veículos (Padrão Integra Finance).
 */
export const useVehicles = (initialData = []) => {
  const [vehicles, setVehicles] = useState(initialData);

  const addVehicle = useCallback((newVehicle) => {
    setVehicles((prev) => [
      ...prev,
      { ...newVehicle, id: prev.length > 0 ? Math.max(...prev.map(v => v.id)) + 1 : 1 }
    ]);
  }, []);

  const updateVehicle = useCallback((id, updatedData) => {
    setVehicles((prev) => 
      prev.map((v) => (v.id === id ? { ...v, ...updatedData } : v))
    );
  }, []);

  const deleteVehicle = useCallback((id) => {
    setVehicles((prev) => prev.filter((v) => v.id !== id));
  }, []);

  return {
    vehicles,
    addVehicle,
    updateVehicle,
    deleteVehicle
  };
};
