/**
 * Temporal foundation tests.
 *
 * Purpose: ensure runtime contracts are enforced on native Temporal.
 */
import { describe, expect, test } from "vitest";

import {
	durationFromObject,
	hasNativeTemporal,
	instantFromISO,
	instantNow,
	plainDateFromISO,
	serializeTemporal,
	validateTemporalRecord,
} from "../temporal-foundation.js";

const hasTemporal = hasNativeTemporal();
const Temporal = globalThis.Temporal;

if (!hasTemporal) {
	test.skip("native Temporal is required by the Node 26 baseline", () => {});
} else {
	describe("temporal foundation", () => {
		test("hasNativeTemporal_reports_true_in_runtime", () => {
			expect(hasTemporal).toBe(true);
			expect(Temporal).toBeTypeOf("object");
		});

		test("instantFromISO_parses_iso_to_native_instant", () => {
			const iso = "2026-05-08T12:00:00Z";
			const instant = instantFromISO(iso);
			expect(instant).toBeInstanceOf(Temporal.Instant);
			expect(instant.toString()).toBe(iso);
		});

		test("instantNow_returns_current_native_instant", () => {
			const before = Date.now();
			const instant = instantNow();
			const after = Date.now();
			expect(instant).toBeInstanceOf(Temporal.Instant);
			expect(instant.epochMilliseconds).toBeGreaterThanOrEqual(before);
			expect(instant.epochMilliseconds).toBeLessThanOrEqual(after);
		});

		test("durationFromObject_returns_native_duration", () => {
			const dur = durationFromObject({ hours: 1, minutes: 30 });
			expect(dur).toBeInstanceOf(Temporal.Duration);
			expect(dur.hours).toBe(1);
			expect(dur.minutes).toBe(30);
		});

		test("plainDateFromISO_returns_native_plain_date", () => {
			const date = plainDateFromISO("2026-05-08");
			expect(date).toBeInstanceOf(Temporal.PlainDate);
			expect(date.year).toBe(2026);
			expect(date.month).toBe(5);
			expect(date.day).toBe(8);
		});

		test("serializeTemporal_roundtrips_native_instant", () => {
			const instant = instantFromISO("2026-05-08T12:00:00Z");
			const serialized = serializeTemporal(instant);
			expect(serialized).toEqual({
				type: "Instant",
				iso: "2026-05-08T12:00:00Z",
			});
		});

		test("validateTemporalRecord_passes_valid", () => {
			const record = {
				createdAt: { type: "Instant", iso: "2026-05-08T12:00:00Z" },
				leaseDuration: { type: "Duration", iso: "PT1H" },
			};
			const result = validateTemporalRecord(record);
			expect(result.valid).toBe(true);
			expect(result.errors).toHaveLength(0);
		});

		test("validateTemporalRecord_fails_unknown_type", () => {
			const record = {
				bad: { type: "UnknownType", iso: "x" },
			};
			const result = validateTemporalRecord(record);
			expect(result.valid).toBe(false);
			expect(
				result.errors.some((e) => e.includes("unknown Temporal type")),
			).toBe(true);
		});

		test("validateTemporalRecord_fails_missing_iso", () => {
			const record = {
				bad: { type: "Instant" },
			};
			const result = validateTemporalRecord(record);
			expect(result.valid).toBe(false);
			expect(result.errors.some((e) => e.includes("missing ISO string"))).toBe(
				true,
			);
		});
	});
}
