export type DatePeriod = '1month' | '3months' | '6months' | 'custom';

export type TransactionType = 'all' | 'deposit' | 'withdrawal';

export type SortOrder = 'latest' | 'oldest';

export interface DateRange {
  startDate: Date;
  endDate: Date;
}

export interface SearchFilter {
  period: DatePeriod;
  dateRange?: DateRange;
  transactionType: TransactionType;
  sortOrder: SortOrder;
}

export interface SearchFilterOptions {
  periods: Array<{
    value: DatePeriod;
    label: string;
  }>;
  transactionTypes: Array<{
    value: TransactionType;
    label: string;
  }>;
  sortOrders: Array<{
    value: SortOrder;
    label: string;
  }>;
}

export interface SearchFilterModalProps {
  isOpen: boolean;
  onClose: () => void;
  onConfirm: (filter: SearchFilter) => void;
  initialFilter?: SearchFilter;
  options?: Partial<SearchFilterOptions>;
}

// 기본 옵션 상수
// locale 적용: label을 i18n key로 지정
export const DEFAULT_FILTER_OPTIONS: SearchFilterOptions = {
  periods: [
    { value: '1month', label: 'filter.periods.1month' },
    { value: '3months', label: 'filter.periods.3months' },
    { value: '6months', label: 'filter.periods.6months' },
    { value: 'custom', label: 'filter.periods.custom' }
  ],
  transactionTypes: [
    { value: 'all', label: 'filter.transactionTypes.all' },
    { value: 'deposit', label: 'filter.transactionTypes.deposit' },
    { value: 'withdrawal', label: 'filter.transactionTypes.withdrawal' }
  ],
  sortOrders: [
    { value: 'latest', label: 'filter.sortOrders.latest' },
    { value: 'oldest', label: 'filter.sortOrders.oldest' }
  ]
};

// 기본 필터 값
export const DEFAULT_SEARCH_FILTER: SearchFilter = {
  period: '1month',
  transactionType: 'all',
  sortOrder: 'latest'
};

// 날짜 유틸리티 함수들
export const getDateRangeFromPeriod = (period: DatePeriod): DateRange | null => {
  const endDate = new Date();
  const startDate = new Date();
  
  switch (period) {
    case '1month':
      startDate.setMonth(endDate.getMonth() - 1);
      break;
    case '3months':
      startDate.setMonth(endDate.getMonth() - 3);
      break;
    case '6months':
      startDate.setMonth(endDate.getMonth() - 6);
      break;
    case 'custom':
      return null; // 사용자가 직접 설정
    default:
      return null;
  }
  
  return { startDate, endDate };
};

export const formatDateRange = (dateRange: DateRange): string => {
  const formatDate = (date: Date) => {
    return date.toLocaleDateString('ko-KR', {
      year: 'numeric',
      month: '2-digit',
      day: '2-digit'
    });
  };
  
  return `${formatDate(dateRange.startDate)} ~ ${formatDate(dateRange.endDate)}`;
};

export const getFilterDisplayText = (filter: SearchFilter, t: (key: string) => string): string => {
  const periodOption = DEFAULT_FILTER_OPTIONS.periods.find(p => p.value === filter.period);
  const transactionOption = DEFAULT_FILTER_OPTIONS.transactionTypes.find(tt => tt.value === filter.transactionType);
  const sortOption = DEFAULT_FILTER_OPTIONS.sortOrders.find(s => s.value === filter.sortOrder);
  
  const parts = [
    periodOption?.label ? t(periodOption.label) : undefined,
    transactionOption?.label ? t(transactionOption.label) : undefined,
    sortOption?.label ? t(sortOption.label) : undefined
  ].filter(Boolean);
  
  return parts.join(' · ');
};