import { format, isSameDay, isToday, isTomorrow, parseISO } from "date-fns";
import type { CalendarEvent } from "@/lib/types";

const getFriendlyDayLabel = (value: Date): string => {
	if (isToday(value)) return "Today";
	if (isTomorrow(value)) return "Tomorrow";
	return format(value, "MMM d, yyyy");
};

export const formatEventStartLabel = (
	start: string,
	allDay?: boolean,
): string => {
	const parsed = parseISO(start);
	const dayLabel = getFriendlyDayLabel(parsed);

	if (allDay) return dayLabel;
	return `${dayLabel} · ${format(parsed, "HH:mm")}`;
};

export const formatEventRangeLabel = (
	event: Pick<CalendarEvent, "start" | "end" | "allDay">,
): string => {
	const startDate = parseISO(event.start);
	const startLabel = getFriendlyDayLabel(startDate);

	if (event.allDay || !event.end) {
		return event.allDay
			? startLabel
			: `${startLabel} · ${format(startDate, "HH:mm")}`;
	}

	const endDate = parseISO(event.end);
	if (isSameDay(startDate, endDate)) {
		return `${startLabel} · ${format(startDate, "HH:mm")}–${format(endDate, "HH:mm")}`;
	}

	return `${startLabel} · ${format(startDate, "HH:mm")} → ${getFriendlyDayLabel(endDate)} · ${format(endDate, "HH:mm")}`;
};
