import { test, expect } from "@playwright/test";
import { PrismaClient } from "@prisma/client";
import { bookTrip } from "../../fixtures/booking-flow";

const prisma = new PrismaClient();
test.afterAll(async () => {
  await prisma.$disconnect();
});

/**
 * UA-6 ✅ — round-trip books BOTH legs. The return leg (C→A) traverses the seeded route high→low; the
 * fare engine now prices the reverse direction by absolute distance (previously it threw "origin must
 * come before destination" and dropped every class, leaving the inbound leg with seats but no priced
 * coach — unbookable). The full two-leg wizard now completes: outbound + return seat, and a total of
 * 2× the one-way fare.
 */
test("UA-6: round-trip books both legs — return leg priced, one seat per leg, total = 2× one-way fare", async ({
  page,
}) => {
  const r = await bookTrip(page, { tripType: "ROUND_TRIP", paymentMethod: "WALLET" });
  expect(r.confirmed).toBe(true);

  const booking = await prisma.booking.findUniqueOrThrow({ where: { id: r.bookingId } });
  expect(booking.bookingType).toBe("ROUND_TRIP");
  expect(booking.status).toBe("CONFIRMED");

  // One seat per leg (leg 1 outbound + leg 2 return) for a single passenger.
  const seats = await prisma.bookingSeat.findMany({ where: { bookingId: r.bookingId } });
  expect(seats.length).toBe(2);
  expect(new Set(seats.map((s) => s.leg)).size).toBe(2);

  // Both legs cover the same A↔C distance, so the round-trip total is 2× the one-way base fare (ETB).
  expect(r.cardBaseFareMinor).toBeGreaterThan(0);
  expect(booking.totalMinor).toBe(r.cardBaseFareMinor * 2);
});
