import type { SystemNotification } from "@/features/system-notifications/system-notifications-service";

const PSWG_CALENDAR_URL = "/pswg/meeting-requests?view=calendar";

export function getNotificationDetailUrl(
  notification: SystemNotification,
): string {
  const savedUrl = notification.data?.url?.trim();

  if (notification.type === "MEETING_SCHEDULED") {
    if (savedUrl?.startsWith("/cdc-gpsf/")) {
      return savedUrl;
    }

    const meetingId = Number(notification.data?.meetingId);

    return Number.isInteger(meetingId) && meetingId > 0
      ? `${PSWG_CALENDAR_URL}&meetingId=${meetingId}`
      : PSWG_CALENDAR_URL;
  }

  if (savedUrl) {
    return savedUrl;
  }

  if (notification.type === "RGC_DECISION_SUBMITTED") {
    const plenaryId = Number(notification.data?.plenaryId);

    if (Number.isInteger(plenaryId) && plenaryId > 0) {
      return `/cdc-gpsf/plenary/plenaries/${plenaryId}`;
    }
  }

  const meetingRequestId = Number(
    notification.data?.meetingRequestId ?? notification.meetingRequestId,
  );

  if (!Number.isInteger(meetingRequestId) || meetingRequestId <= 0) {
    return "/";
  }

  return `/ministry/meeting-requests/${meetingRequestId}`;
}
