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-2 ✅ — full one-way USD booking (Other nationality, INTERNATIONAL class, WALLET). The money chain
 * is now COHERENT: the passenger sees and agrees to a USD amount (displayCurrency/displayTotalMinor),
 * while the stored charge basis is honestly labeled ETB (currency/totalMinor). The two are the same
 * fare at the USD→ETB rate — no longer a mislabeled 100× divergence.
 */
test("UA-2: USD booking — passenger amount in USD, charge basis stored coherently in ETB", async ({ page }) => {
  const r = await bookTrip(page, { nationality: "Other", paymentMethod: "WALLET" });
  expect(r.displayCurrency).toBe("USD");
  expect(r.confirmed).toBe(true);

  const booking = await prisma.booking.findUniqueOrThrow({ where: { id: r.bookingId } });
  const intent = await prisma.paymentIntent.findUniqueOrThrow({ where: { bookingId: r.bookingId } });

  // Passenger-facing: the USD amount they saw and agreed to (what the browser reviewed).
  expect(booking.displayCurrency).toBe("USD");
  expect(booking.displayTotalMinor).toBe(r.reviewedTotalMinor);
  expect(r.reviewedTotalMinor).toBe(r.cardDisplayMinor);

  // Stored charge basis: ETB, coherently labeled (no more USD mislabel).
  expect(booking.currency).toBe("ETB");
  expect(booking.totalMinor).toBe(r.cardBaseFareMinor); // the ETB fare
  expect(booking.totalMinor).toBe(r.reviewedTotalMinor * 100); // ETB == USD display × rate

  // The charge/intent moves the ETB amount; booking is confirmed.
  expect(intent.amountMinor).toBe(booking.totalMinor);
  expect(booking.status).toBe("CONFIRMED");
});
