/**
 * 驗證相關工具函式
 */



/**
 * 驗證撥補單表單資料
 */
export const validateTransferOrder = (formData: {
  sourceWarehouseId: string;
  targetWarehouseId: string;
  productId: string;
  quantity: number;
}): { isValid: boolean; error?: string } => {
  if (!formData.sourceWarehouseId) {
    return { isValid: false, error: "請選擇來源倉庫" };
  }

  if (!formData.targetWarehouseId) {
    return { isValid: false, error: "請選擇目標倉庫" };
  }

  if (formData.sourceWarehouseId === formData.targetWarehouseId) {
    return { isValid: false, error: "來源倉庫與目標倉庫不能相同" };
  }

  if (!formData.productId) {
    return { isValid: false, error: "請選擇撥補商品" };
  }

  if (formData.quantity <= 0) {
    return { isValid: false, error: "撥補數量必須大於0" };
  }

  return { isValid: true };
};

/**
 * 驗證撥補數量是否超過可用庫存
 */
export const validateTransferQuantity = (
  quantity: number,
  availableQuantity: number
): { isValid: boolean; error?: string } => {
  if (quantity > availableQuantity) {
    return {
      isValid: false,
      error: `撥補數量不能超過可用庫存 (${availableQuantity})`,
    };
  }
  return { isValid: true };
};

/**
 * 驗證倉庫表單資料
 */
export const validateWarehouse = (formData: {
  code: string;
  name: string;
  address: string;
}): { isValid: boolean; error?: string } => {
  if (!formData.name.trim()) {
    return { isValid: false, error: "倉庫名稱為必填欄位" };
  }

  if (!formData.address.trim()) {
    return { isValid: false, error: "倉庫地址為必填欄位" };
  }

  return { isValid: true };
};

/**
 * 驗證發票號碼格式 (AA-12345678)
 */
export const validateInvoiceNumber = (invoiceNumber?: string): { isValid: boolean; error?: string } => {
  if (!invoiceNumber) return { isValid: true };

  const regex = /^[A-Z]{2}-\d{8}$/;
  if (!regex.test(invoiceNumber)) {
    return {
      isValid: false,
      error: "發票號碼格式錯誤，應為：AB-12345678",
    };
  }

  return { isValid: true };
};
