/**
 * 庫存表單 Hook
 * 處理新增庫存項目的表單狀態和邏輯
 */

import { useState } from "react";
import { WarehouseInventory } from "../types/warehouse";

export interface InventoryFormData {
  productId: string;
  batchNumber: string;
  quantity: number;
  expiryDate: string;
  safetyStock: number;
  lastInboundDate: string;
  lastOutboundDate: string;
}

const initialFormData: InventoryFormData = {
  productId: "",
  batchNumber: "",
  quantity: 0,
  expiryDate: "",
  safetyStock: 0,
  lastInboundDate: "",
  lastOutboundDate: "",
};

export const useInventoryForm = () => {
  const [formData, setFormData] = useState<InventoryFormData>(initialFormData);

  const resetForm = (defaultProductId?: string) => {
    setFormData({
      ...initialFormData,
      productId: defaultProductId || "",
    });
  };

  const updateField = <K extends keyof InventoryFormData>(
    field: K,
    value: InventoryFormData[K]
  ) => {
    setFormData((prev) => ({ ...prev, [field]: value }));
  };

  return {
    formData,
    setFormData,
    resetForm,
    updateField,
  };
};
