import test from "node:test";
import assert from "node:assert/strict";
import { readFile } from "node:fs/promises";
import ts from "typescript";
const read = (path) => readFile(new URL(`../${path}`, import.meta.url), "utf8");

test("traveler picker opens with existing clients, focused search, empty guidance and creation action", async () => {
  const picker = await read("src/components/travelers/AddTravelerDialog.tsx");
  assert.match(picker, /CommandInput autoFocus/);
  assert.match(picker, /Clients disponibles/);
  assert.match(picker, /Aucun client trouvé/);
  assert.match(picker, /Créer un nouveau voyageur/);
  assert.match(picker, /name\.toLowerCase\(\)\.includes/);
  assert.match(picker, /email\.toLowerCase\(\)\.includes/);
});

test("existing profiles use the secured assignment RPC with the current trip", async () => {
  const [picker, actions] = await Promise.all([read("src/components/travelers/AddTravelerDialog.tsx"), read("src/services/travelerActions.ts")]);
  assert.match(picker, /assignExistingTravelerToTrip\(tripId, traveler\.id\)/);
  assert.match(actions, /rpc\("assign_existing_traveler"/);
  assert.match(actions, /p_trip_id: tripId/);
});

test("unknown people reuse invite-user and never show success before confirmation", async () => {
  const picker = await read("src/components/travelers/AddTravelerDialog.tsx");
  assert.match(picker, /await inviteTraveler\(\{ tripId/);
  assert.match(picker, /result\?\.associated/);
  assert.match(picker, /catch \(cause\).*setError/s);
  assert.ok(picker.indexOf("await inviteTraveler") < picker.indexOf("result?.associated"));
});

test("email duplicate checks are case-insensitive", async () => {
  const source = await read("src/services/travelerActions.ts");
  const compiled = ts.transpileModule(source.replace('import { supabase } from "@/integrations/supabase/client";\n', 'const supabase = {};\n'), { compilerOptions: { module: ts.ModuleKind.ES2022, target: ts.ScriptTarget.ES2022 } }).outputText;
  const module = await import(`data:text/javascript;base64,${Buffer.from(compiled).toString("base64")}`);
  assert.equal(module.isTravelerAlreadyLinked(" Sophie@Example.com ", ["sophie@example.com"]), true);
  assert.equal(module.isTravelerAlreadyLinked("paul@example.com", ["sophie@example.com"]), false);
});

test("workspace list distinguishes active members from pending invitations and computes real validations", async () => {
  const page = await read("src/pages/planner/workspace/TravelersPage.tsx");
  assert.match(page, /TravelerStatusBadge status="active"/);
  assert.match(page, /validation\.travelerId === member\.userId/);
  assert.match(page, /invitation\.status === "pending"/);
  assert.match(page, /trip\.id/);
});

test("Clients page only derives people and trip counts from already authorized trips", async () => {
  const clients = await read("src/pages/planner/ClientsPage.tsx");
  assert.match(clients, /getUserTrips\(\)/);
  assert.match(clients, /trip\.members/);
  assert.match(clients, /trip\.invitations/);
  assert.doesNotMatch(clients, /supabase/);
});
