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-5 — one-way, 1 adult + 2 children under 5, ETB, WALLET. One free child per adult: the first
 * child is free, the second is charged a full adult fare. Total = 2 fares; 2 passengers are seated.
 */
test("UA-5: with 1 adult + 2 children, the second child pays full fare", async ({ page }) => {
  const r = await bookTrip(page, { adults: 1, children: 2, paymentMethod: "WALLET" });
  expect(r.confirmed).toBe(true);

  const fare = r.cardBaseFareMinor;
  // adult (paid) + first child (free) + second child (paid) = 2 fares.
  expect(r.reviewedTotalMinor).toBe(fare * 2);

  const booking = await prisma.booking.findUniqueOrThrow({ where: { id: r.bookingId } });
  expect(booking.totalMinor).toBe(fare * 2);

  // Only the free first-child is dropped → adult + paid second child are seated.
  const seats = await prisma.bookingSeat.findMany({ where: { bookingId: r.bookingId } });
  expect(seats.length).toBe(2);
});
