/**
 * Shared date/month utilities used across the native app.
 */

/** Returns the current month as "YYYY-MM". */
export function currentMonthStr(): string {
  const now = new Date();
  return `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, "0")}`;
}

/** Adds `delta` months to a "YYYY-MM" string and returns the resulting "YYYY-MM". */
export function addMonths(monthStr: string, delta: number): string {
  const [y, m] = monthStr.split("-").map(Number);
  const d = new Date(y!, m! - 1 + delta);
  return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, "0")}`;
}

/** Formats a "YYYY-MM" string as a localized German month+year label (e.g. "März 2026"). */
export function monthLabel(month: string): string {
  const [year, m] = month.split("-");
  return new Intl.DateTimeFormat("de-DE", { month: "long", year: "numeric" }).format(
    new Date(Number(year), Number(m) - 1),
  );
}

/**
 * Returns the first-of-month and last-of-month ISO timestamps for a "YYYY-MM" string.
 * Useful for building date-range query filters.
 */
export function monthDateRange(month: string): [from: string, to: string] {
  const [y, m] = month.split("-").map(Number);
  const lastDay = new Date(y!, m!, 0).getDate();
  return [
    `${month}-01T00:00:00.000Z`,
    `${month}-${String(lastDay).padStart(2, "0")}T23:59:59.999Z`,
  ];
}

/** Returns today's date as "YYYY-MM-DD". */
export function todayIso(): string {
  const now = new Date();
  return `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, "0")}-${String(now.getDate()).padStart(2, "0")}`;
}
