import { describe, expect, test } from "bun:test";
import {
	applyQuickDateShortcut,
	getCalendarMonthForValue,
} from "@/components/date-time-picker";

describe("DateTimePicker quick shortcuts", () => {
	test("Today selects today, navigates the visible month, and keeps the picker open", () => {
		const now = new Date("2026-04-09T10:30:00Z");

		const result = applyQuickDateShortcut({
			shortcut: "Today",
			value: "2026-06-15T14:30:00",
			allDay: false,
			now,
		});

		expect(result.nextValue).toBe("2026-04-09T14:30:00");
		expect(result.nextMonth.toISOString()).toBe("2026-04-09T00:00:00.000Z");
		expect(result.keepOpen).toBe(true);
	});

	test("Next week moves selection forward a week without dropping the existing time", () => {
		const now = new Date("2026-04-09T10:30:00Z");

		const result = applyQuickDateShortcut({
			shortcut: "Next week",
			value: "2026-04-01T09:45:00",
			allDay: false,
			now,
		});

		expect(result.nextValue).toBe("2026-04-16T09:45:00");
		expect(result.nextMonth.toISOString()).toBe("2026-04-16T00:00:00.000Z");
	});

	test("Next month updates the selected all-day date and visible month in place", () => {
		const now = new Date("2026-04-09T10:30:00Z");

		const result = applyQuickDateShortcut({
			shortcut: "Next month",
			value: "2026-04-01",
			allDay: true,
			now,
		});

		expect(result.nextValue).toBe("2026-05-09");
		expect(result.nextMonth.toISOString()).toBe("2026-05-09T00:00:00.000Z");
		expect(result.keepOpen).toBe(true);
	});
});

describe("DateTimePicker visible month", () => {
	test("uses the selected value month when one is present", () => {
		const fallbackDate = new Date("2026-04-09T10:30:00Z");

		const visibleMonth = getCalendarMonthForValue(
			"2026-07-18T08:15:00",
			fallbackDate,
		);

		expect(visibleMonth.toISOString()).toBe("2026-07-18T08:15:00.000Z");
	});

	test("falls back to the current month when no value is selected yet", () => {
		const fallbackDate = new Date("2026-04-09T10:30:00Z");

		const visibleMonth = getCalendarMonthForValue("", fallbackDate);

		expect(visibleMonth.toISOString()).toBe("2026-04-09T10:30:00.000Z");
	});
});
