import { site } from "@/content/site";

/** Event times in Addis Ababa (EAT, UTC+3) */
const EVENT_START = "20250131T080000";
const EVENT_END = "20250201T180000";

function formatIcsDate(iso: string) {
  return iso.replace(/[-:]/g, "").split(".")[0] + "Z";
}

export function buildGoogleCalendarUrl() {
  const params = new URLSearchParams({
    action: "TEMPLATE",
    text: site.name,
    dates: `${EVENT_START}/${EVENT_END}`,
    details: `${site.tagline}\n\nPresented by ${site.presentedBy}.\n${site.links.legacySite}`,
    location: `${site.venue.name}, ${site.venue.address}`,
    ctz: "Africa/Addis_Ababa",
  });
  return `https://calendar.google.com/calendar/render?${params.toString()}`;
}

export function buildOutlookCalendarUrl() {
  const params = new URLSearchParams({
    path: "/calendar/action/compose",
    rru: "addevent",
    subject: site.name,
    startdt: "2025-01-31T08:00:00",
    enddt: "2025-02-01T18:00:00",
    body: site.tagline,
    location: `${site.venue.name}, ${site.venue.address}`,
  });
  return `https://outlook.live.com/calendar/0/deeplink/compose?${params.toString()}`;
}

export function buildIcsFileContent() {
  const uid = `grv-summit-${site.dates.start}@grvsummit.com`;
  const now = formatIcsDate(new Date().toISOString());

  return [
    "BEGIN:VCALENDAR",
    "VERSION:2.0",
    "PRODID:-//GRV Summit//EN",
    "CALSCALE:GREGORIAN",
    "METHOD:PUBLISH",
    "BEGIN:VEVENT",
    `UID:${uid}`,
    `DTSTAMP:${now}`,
    `DTSTART;TZID=Africa/Addis_Ababa:20250131T080000`,
    `DTEND;TZID=Africa/Addis_Ababa:20250201T180000`,
    `SUMMARY:${site.name}`,
    `DESCRIPTION:${site.tagline.replace(/\n/g, "\\n")}`,
    `LOCATION:${site.venue.name}, ${site.venue.address}`,
    "END:VEVENT",
    "END:VCALENDAR",
  ].join("\r\n");
}
