/** Shared constants mirroring apps/edr-passenger-api/test/fixtures/{seed-core,seed-ui}.ts. */
export const STATIONS = {
  A: "00000000-0000-4000-8000-000000000020", // Alpha / AAA
  B: "00000000-0000-4000-8000-000000000021", // Bravo / BBB
  C: "00000000-0000-4000-8000-000000000022", // Charlie / CCC
} as const;

export const SCHEDULE_ID = "00000000-0000-4000-8000-000000000101";
export const RETURN_SCHEDULE_ID = "00000000-0000-4000-8000-000000000201";
export const SEAT_CLASS_LOCAL = "00000000-0000-4000-8000-000000000010";
export const SEAT_CLASS_INTL = "00000000-0000-4000-8000-000000000011";
export const COACH_TYPE_ID = "00000000-0000-4000-8000-000000000001";
export const ROUTE_ID = "00000000-0000-4000-8000-000000000030";
export const PROMO_VALID = "PROMO10";
export const PROMO_EXPIRED = "EXPIRED50";

export const API_URL = process.env.API_URL ?? "http://localhost:4000";

/** Friendly nationality name → the enum the portal/search expects. */
export const NATIONALITY_ENUM = {
  Ethiopian: "ETHIOPIAN",
  Djiboutian: "DJIBOUTIAN",
  Other: "OTHER",
} as const;
export type Nationality = keyof typeof NATIONALITY_ENUM;

/** Display currency the search returns per nationality (asserted by the currency specs). */
export const CURRENCY_BY_NATIONALITY = {
  Ethiopian: "ETB",
  Djiboutian: "DJF",
  Other: "USD",
} as const;

function tokenFrom(file: string): string {
  const fs = require("node:fs") as typeof import("node:fs");
  const path = require("node:path") as typeof import("node:path");
  const raw = JSON.parse(fs.readFileSync(path.join(__dirname, "storage", file), "utf8"));
  for (const origin of raw.origins ?? []) {
    for (const item of origin.localStorage ?? []) {
      if (item.name === "auth_token") return item.value as string;
    }
  }
  throw new Error(`auth_token not found in ${file} — did global-setup run?`);
}

/** Staff/admin auth token minted by global-setup (backoffice storageState). */
export function staffToken(): string {
  return tokenFrom("staff.json");
}

/** Regular passenger (non-admin) auth token minted by global-setup (portal storageState). */
export function passengerToken(): string {
  return tokenFrom("passenger.json");
}

/** Must match seed-ui.sampleDepartAt(): now + 2 days at 06:00Z. */
export function sampleDepartDate(): string {
  const d = new Date();
  d.setUTCDate(d.getUTCDate() + 2);
  d.setUTCHours(6, 0, 0, 0);
  return d.toISOString().slice(0, 10);
}

/**
 * Deep-link to the results page (bypasses the search form, which cannot emit tripType/returnDate).
 * `nationality` accepts the friendly name ("Ethiopian"|"Djiboutian"|"Other") and is emitted as the
 * enum the results page expects. For a round trip, pass tripType "ROUND_TRIP" — returnDate defaults
 * to the same calendar day (the seeded return leg departs 8h after the outbound).
 */
export function resultsUrl(opts?: {
  adults?: number;
  children?: number;
  nationality?: string;
  tripType?: "ONE_WAY" | "ROUND_TRIP";
  returnDate?: string;
}) {
  const nat = opts?.nationality ?? "Ethiopian";
  const enumNat = (NATIONALITY_ENUM as Record<string, string>)[nat] ?? nat.toUpperCase();
  const p = new URLSearchParams({
    origin: STATIONS.A,
    destination: STATIONS.C,
    date: sampleDepartDate(),
    tripType: opts?.tripType ?? "ONE_WAY",
    adults: String(opts?.adults ?? 1),
    children: String(opts?.children ?? 0),
    nationality: enumNat,
  });
  if ((opts?.tripType ?? "ONE_WAY") === "ROUND_TRIP") {
    p.set("returnDate", opts?.returnDate ?? sampleDepartDate());
  }
  return `/booking/results?${p.toString()}`;
}
