
/**
 * 예산 데이터 검증 유틸리티
 */
import { BudgetData, CategoryBudgets } from './types';

/**
 * 월간 예산이 유효한지 확인
 */
export const isValidMonthlyBudget = (budgetData: BudgetData | null): boolean => {
  if (!budgetData || !budgetData.monthly) return false;
  
  const { targetAmount } = budgetData.monthly;
  return typeof targetAmount === 'number' && targetAmount > 0;
};

/**
 * 카테고리 예산이 유효한지 확인
 */
export const isValidCategoryBudgets = (categoryBudgets: CategoryBudgets | null): boolean => {
  if (!categoryBudgets) return false;
  
  // 총 예산 금액 계산
  const totalAmount = Object.values(categoryBudgets).reduce((sum, amount) => {
    return sum + (typeof amount === 'number' ? amount : 0);
  }, 0);
  
  return totalAmount > 0;
};

/**
 * 카테고리 예산 총액 계산
 */
export const calculateTotalCategoryBudget = (categoryBudgets: CategoryBudgets | null): number => {
  if (!categoryBudgets) return 0;
  
  return Object.values(categoryBudgets).reduce((sum, amount) => {
    return sum + (typeof amount === 'number' ? amount : 0);
  }, 0);
};

/**
 * 유효한 카테고리 예산 항목만 필터링
 */
export const filterValidCategoryBudgets = (categoryBudgets: CategoryBudgets): CategoryBudgets => {
  return Object.fromEntries(
    Object.entries(categoryBudgets)
      .filter(([_, amount]) => typeof amount === 'number' && amount > 0)
  );
};
