import dayjs from 'dayjs';

// 전체 월 범위 생성
export function generateMonthRange(
  startDate: string,
  endDate: string,
): string[] {
  const start = dayjs(startDate).startOf('month');
  const end = dayjs(endDate).endOf('month');
  const months: string[] = [];

  let current = start;
  while (current.isBefore(end) || current.isSame(end, 'month')) {
    months.push(current.format('YYYY-MM'));
    current = current.add(1, 'month');
  }

  return months;
}

// 전체 일 범위 생성
export function generateDayRange(startDate: string, endDate: string): string[] {
  const start = dayjs(startDate).startOf('day');
  const end = dayjs(endDate).endOf('day');
  const days: string[] = [];

  let current = start;
  while (current.isBefore(end) || current.isSame(end, 'day')) {
    days.push(current.format('YYYY-MM-DD'));
    current = current.add(1, 'day');
  }

  return days;
}

//* 서울 기준 오늘(YYYY-MM-DD). 서버 시간대와 무관하다 — 정책 버전 선택·구간 선택·정기갱신일이 같은 출처의 날짜를 본다.
//*   format() 이 내는 모양은 로케일 데이터(ICU·CLDR 판)에 달려 있어 연·월·일 조각을 직접 잇는다.
const SEOUL_DATE_FORMATTER = new Intl.DateTimeFormat('en-US', {
  timeZone: 'Asia/Seoul',
  year: 'numeric',
  month: '2-digit',
  day: '2-digit',
});

export function getSeoulToday(now: Date = new Date()): string {
  const part = Object.fromEntries(
    SEOUL_DATE_FORMATTER.formatToParts(now).map(({ type, value }) => [
      type,
      value,
    ]),
  );
  return `${part.year}-${part.month}-${part.day}`;
}
