/**
 * One row of `GET /my-calendar/`.
 *
 * Transcribed from `CalendarController::formatOpportunity()` and
 * `::formatEvent()` — the two shapes that endpoint merges, which deliberately
 * carry the same keys so a single card renders either. `calendar_id` is spliced
 * in by `savedItems()` only, which is why it is optional here: a registered or
 * organized entry the reader has not saved has no `my_calendars` row to point
 * at, and the save/unsave control has to tell those apart.
 *
 * `type` is the **coarse** grouping (`Volunteer` / `Learn` / `Event`) the
 * calendar routes on; `type_en` / `type_ar` are the specific labels shown to the
 * reader (a learning type for a development opportunity, the event type for an
 * event, falling back to `Opportunity` / `فرصة`). `calendarEventTypes.ts`
 * documents why `detailPathForType` checks the coarse names first.
 */
export interface CalendarEntryRecord {
  /** The opportunity's or event's own id — **not** unique across types. */
  id: number;
  /** `Volunteer` | `Learn` | `Event`. */
  type: string;
  type_en: string;
  type_ar: string;
  /** `Saved` | `Registered` | `Organized` — the reader's relationship to it. */
  status: string;
  title_en: string | null;
  title_ar: string | null;
  /** `Y-m-d`. */
  start_date: string | null;
  end_date: string | null;
  /** `H:i:s` as stored, or null. */
  start_time: string | null;
  end_time: string | null;
  opportunity_status: string | null;
  /** Present only on an entry that has a `my_calendars` row. */
  calendar_id?: number;
}

/**
 * The composite key the calendar identifies an entry by.
 *
 * `id` alone is not unique: a volunteer opportunity, a development opportunity
 * and an event each have their own sequence, so three different records can all
 * be id 7. The backend's own `deduplicateByStatusPriority()` keys on
 * `type:id` for the same reason.
 */
export function calendarEntryKey(entry: CalendarEntryRecord): string {
  return `${entry.type_en || entry.type || ""}:${entry.id}`;
}
