// Event types and the initial-month helper for the Progress Report calendar.

import type { UploadedFileMetadata } from "@/lib/document-file";

// One event shown on the calendar. Only what a card needs to render.
export type ProgressReportCalendarEvent = {
  id: number;
  title: string;
  dateKey: string; // "YYYY-MM-DD" — decides which day cell the event sits in
  startTime: string;
  endTime: string;
  status: "Draft" | "Sent";
};

// The extra fields the detail popup shows. They are fetched when a card is
// clicked, so the report list stays small.
export type ProgressReportCalendarEventDetail = {
  locationName: string;
  locationAddress: string;
  status: "Draft" | "Sent";
  description: string;
  document: UploadedFileMetadata | null;
  documentName: string;
  documentSize: number | null;
};

export type ProgressReportCalendarMonth = {
  year: number;
  monthIndex: number; // 0 = January
};

// Pick the month the calendar opens on: the latest event's month, so the
// visitor lands on a month with fetched meeting content.
export function getInitialCalendarMonth(
  events: ProgressReportCalendarEvent[],
): ProgressReportCalendarMonth {
  const sortedDateKeys = events.map((event) => event.dateKey).sort();
  const latestDateKey = sortedDateKeys[sortedDateKeys.length - 1];

  if (latestDateKey) {
    const [year, month] = latestDateKey.split("-").map(Number);

    if (year && month) {
      return { year, monthIndex: month - 1 };
    }
  }

  const today = new Date();
  return { year: today.getUTCFullYear(), monthIndex: today.getUTCMonth() };
}
