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

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

/**
 * UA-1 — one-way, 1 adult, ETB, WALLET. The full real-browser booking flow, asserting the money
 * chain: card fare > 0, and reviewedTotalMinor == Booking.totalMinor == displayTotalMinor ==
 * PaymentIntent.amountMinor == wallet DEBIT, booking CONFIRMED.
 */
test("UA-1: one-way WALLET booking, price cross-check holds end to end", async ({ page }) => {
  const r = await bookOneAdult(page, { nationality: "Ethiopian", paymentMethod: "WALLET" });
  expect(r.confirmed).toBe(true);
  expect([200, 201]).toContain(r.initiateStatus);

  const booking = await prisma.booking.findUniqueOrThrow({ where: { id: r.bookingId } });
  const intent = await prisma.paymentIntent.findUniqueOrThrow({ where: { bookingId: r.bookingId } });
  const debit = await prisma.walletLedgerEntry.findFirst({
    where: { relatedBookingId: r.bookingId, type: "DEBIT" },
  });

  expect(booking.totalMinor).toBe(r.reviewedTotalMinor);
  expect(booking.displayTotalMinor).toBe(r.reviewedTotalMinor);
  expect(intent.amountMinor).toBe(r.reviewedTotalMinor);
  expect(debit?.amountMinor).toBe(r.reviewedTotalMinor);
  expect(booking.status).toBe("CONFIRMED");
});
