/**
 * Integration tests for Schedule URL round-tripping.
 *
 * Verifies: build URL from params -> parse URL -> params match original.
 */

import { describe, it, expect } from "vitest";
import {
  buildScheduleUrl,
  parseScheduleUrl,
  type ScheduleParams,
} from "@/features/schedule/url.js";

describe("Schedule URL round-trip integration", () => {
  const roundTrip = (params: ScheduleParams) => {
    const url = buildScheduleUrl(params);
    const parsed = parseScheduleUrl(url);
    return { url, parsed };
  };

  it("round-trips start page", () => {
    const { parsed } = roundTrip({ type: "start" });
    expect(parsed).toEqual({ type: "start" });
  });

  it("round-trips one-way route search", () => {
    const params: ScheduleParams = {
      type: "route",
      outbound: { departure: "MOW", arrival: "KUF", dateFrom: "20220425", dateTo: "20220501" },
    };
    const { url, parsed } = roundTrip(params);
    expect(url).toBe("schedule/route/MOW-KUF-20220425-20220501");
    expect(parsed).toEqual(params);
  });

  it("round-trips one-way with time range and connections", () => {
    const params: ScheduleParams = {
      type: "route",
      outbound: {
        departure: "SVO",
        arrival: "LED",
        dateFrom: "20250115",
        dateTo: "20250122",
        timeFrom: "0800",
        timeTo: "1800",
        connections: 1,
      },
    };
    const { url, parsed } = roundTrip(params);
    expect(url).toBe("schedule/route/SVO-LED-20250115-20250122-08001800-C1");
    expect(parsed).toEqual(params);
  });

  it("round-trips round-trip search", () => {
    const params: ScheduleParams = {
      type: "roundtrip",
      outbound: { departure: "MOW", arrival: "KUF", dateFrom: "20220425", dateTo: "20220501" },
      inbound: { departure: "KUF", arrival: "MOW", dateFrom: "20220502", dateTo: "20220508" },
    };
    const { url, parsed } = roundTrip(params);
    expect(url).toBe("schedule/route/MOW-KUF-20220425-20220501/KUF-MOW-20220502-20220508");
    expect(parsed).toEqual(params);
  });

  it("round-trips single-flight details", () => {
    const params: ScheduleParams = {
      type: "details",
      flights: [{ carrier: "SU", flightNumber: "0012", date: "20220527" }],
    };
    const { url, parsed } = roundTrip(params);
    expect(url).toBe("schedule/SU0012-20220527");
    expect(parsed).toEqual(params);
  });

  it("round-trips multi-flight details", () => {
    const params: ScheduleParams = {
      type: "details",
      flights: [
        { carrier: "SU", flightNumber: "0012", date: "20220501" },
        { carrier: "SU", flightNumber: "0013", date: "20220507" },
      ],
    };
    const { url, parsed } = roundTrip(params);
    expect(url).toBe("schedule/SU0012-20220501/SU0013-20220507");
    expect(parsed).toEqual(params);
  });

  it("returns null for invalid URL", () => {
    expect(parseScheduleUrl("")).toBeNull();
    expect(parseScheduleUrl("not-a-schedule-url")).toBeNull();
    expect(parseScheduleUrl("schedule/route/")).toBeNull();
  });

  it("handles leading slash in parse", () => {
    const parsed = parseScheduleUrl("/schedule/route/SVO-LED-20250115-20250122");
    expect(parsed).toEqual({
      type: "route",
      outbound: {
        departure: "SVO",
        arrival: "LED",
        dateFrom: "20250115",
        dateTo: "20250122",
      },
    });
  });

  it("build then parse preserves all search variants", () => {
    const searchTypes: ScheduleParams[] = [
      { type: "start" },
      {
        type: "route",
        outbound: { departure: "SVO", arrival: "LED", dateFrom: "20250115", dateTo: "20250122" },
      },
      {
        type: "roundtrip",
        outbound: { departure: "SVO", arrival: "LED", dateFrom: "20250115", dateTo: "20250122" },
        inbound: { departure: "LED", arrival: "SVO", dateFrom: "20250125", dateTo: "20250131" },
      },
      {
        type: "details",
        flights: [{ carrier: "SU", flightNumber: "0100", date: "20250115" }],
      },
    ];

    for (const params of searchTypes) {
      const url = buildScheduleUrl(params);
      const parsed = parseScheduleUrl(url);
      expect(parsed).not.toBeNull();
      expect(parsed!.type).toBe(params.type);
    }
  });

  it("details with flight suffix round-trips correctly", () => {
    const params: ScheduleParams = {
      type: "details",
      flights: [{ carrier: "SU", flightNumber: "0100", suffix: "D", date: "20250115" }],
    };
    const { url, parsed } = roundTrip(params);
    expect(url).toBe("schedule/SU0100D-20250115");
    expect(parsed).toEqual(params);
  });
});
