export interface ScheduleSlot {
  label: string;
  hour: number;
  minute: number;
  display: string;
}

const SCHEDULE_WINDOWS = [
  { label: "Morning", hour: [7, 10] },
  { label: "Afternoon", hour: [12, 15] },
  { label: "Evening", hour: [17, 20] },
] as const;

/** Morning only for 1/day; morning + evening for 2/day. */
const WINDOW_INDICES: Record<1 | 2, number[]> = {
  1: [0],
  2: [0, 2],
};

export function planScheduleSlots(count: 1 | 2, seed: number): ScheduleSlot[] {
  const indices = WINDOW_INDICES[count];
  const picks: ScheduleSlot[] = [];

  for (let i = 0; i < count; i++) {
    const window = SCHEDULE_WINDOWS[indices[i]];
    const hour =
      window.hour[0] + ((seed + i * 3) % (window.hour[1] - window.hour[0] + 1));
    const minute = [0, 15, 30, 45][(seed + i) % 4];
    const h12 = hour % 12 || 12;
    const ampm = hour < 12 ? "AM" : "PM";
    picks.push({
      label: window.label,
      hour,
      minute,
      display: `${window.label} · ${h12}:${minute.toString().padStart(2, "0")} ${ampm}`,
    });
  }

  return picks;
}

export function parseProjectTimezone(raw: string): string {
  const trimmed = raw.trim();
  const match = trimmed.match(/^([A-Za-z0-9_+/]+)/);
  return match?.[1] ?? "UTC";
}
