/**
 * KST timezone helpers — main + renderer 양쪽에서 import 가능.
 * v0.2.6 C1: backlog #3+#19+#34 통합 (기존 src/main/util/kstDate.ts 이동).
 */

export const KST_OFFSET_MS = 9 * 60 * 60 * 1000;
export const DAY_MS = 24 * 60 * 60 * 1000;

/**
 * KST 자정 기준 today YYYY-MM-DD.
 *
 * 기존 todayInKstString (NoteRepository.findExpiredCandidates),
 * TelemetryService.todayKstIso, telemetryStats.kstDate, AiWorker.todayKstAsIso
 * 4 callsite 통합.
 */
export function kstTodayIso(now: Date = new Date()): string {
  const k = new Date(now.getTime() + KST_OFFSET_MS);
  return new Date(Date.UTC(k.getUTCFullYear(), k.getUTCMonth(), k.getUTCDate()))
    .toISOString().slice(0, 10);
}

/**
 * 다음 KST 자정의 epoch ms (UTC).
 *
 * 기존 nextKstMidnightMs (store.snoozeExpired) + store.snoozeRecall inline 통합.
 */
export function nextKstMidnightMs(now: number = Date.now()): number {
  const kstNow = now + KST_OFFSET_MS;
  const kstMidnightFloor = Math.floor(kstNow / DAY_MS) * DAY_MS;
  const nextKstMidnight = kstMidnightFloor + DAY_MS;
  return nextKstMidnight - KST_OFFSET_MS;
}

/**
 * KST today (00:00 KST 의 UTC Date 객체). AiWorker 의 dueDateParser 가 candidate 비교용.
 *
 * 기존 AiWorker.todayKstAsDate 통합.
 */
export function kstTodayAsDate(now: Date = new Date()): Date {
  const k = new Date(now.getTime() + KST_OFFSET_MS);
  return new Date(Date.UTC(k.getUTCFullYear(), k.getUTCMonth(), k.getUTCDate()));
}
