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-3w ✅ — Djiboutian/DJF, WALLET. The money chain is now COHERENT: the passenger sees and agrees
 * to a DJF amount (displayCurrency/displayTotalMinor), while the stored charge basis is honestly
 * labeled ETB (currency/totalMinor). Same fare, two correctly-labeled currencies — no mislabel.
 */
test("UA-3w: DJF WALLET booking — passenger amount in DJF, charge basis stored coherently in ETB", async ({ page }) => {
  const r = await bookTrip(page, { nationality: "Djiboutian", paymentMethod: "WALLET" });
  expect(r.displayCurrency).toBe("DJF");
  expect(r.confirmed).toBe(true);

  const booking = await prisma.booking.findUniqueOrThrow({ where: { id: r.bookingId } });
  // Passenger-facing: the DJF amount they saw and agreed to.
  expect(booking.displayCurrency).toBe("DJF");
  expect(booking.displayTotalMinor).toBe(r.reviewedTotalMinor);
  expect(r.reviewedTotalMinor).toBe(r.cardDisplayMinor);
  // Stored charge basis: ETB, coherently labeled (no more DJF mislabel).
  expect(booking.currency).toBe("ETB");
  expect(booking.totalMinor).toBe(r.cardBaseFareMinor);
  expect(booking.status).toBe("CONFIRMED");
});

/**
 * UA-3 — Djiboutian/DJF paid via a forged gateway settlement. The real telebirr gateway is
 * unreachable in the test env, so (per the matrix's settlement-injection plan) the booking is created
 * through the real browser flow and settled by forging the payment.succeeded event. Proves the DJF
 * booking reaches a CONFIRMED, ticketed state through the gateway (non-WALLET) path.
 */
test("UA-3: DJF booking settles through a forged gateway payment", async ({ page }) => {
  const r = await bookTrip(page, { nationality: "Djiboutian", paymentMethod: "TELEBIRR" });
  expect(r.displayCurrency).toBe("DJF");
  expect(r.confirmed).toBe(true);

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