/* WITNESS · W-077 owner-menu — "owner creates a dish via the owner webapp; patron verifies it
 *                                appears on the live menu" — cross-app menu propagation.
 *
 * TWO STALE THINGS CORRECTED BEFORE THIS WALK:
 *   1. plan.json/fixmap point this journey at "Canary Owner Menu" (cdaf8ce3) - but the
 *      journey's OWN step 2 is "OWNER :: Verify Canary Kitchen", and a prior FALSE_GREEN was
 *      retracted for exactly this mix-up (runs/wave0/fixture-misassign.json confirms it:
 *      owner-menu -> should be "Canary Kitchen", not "Canary Owner Menu"). This walk targets
 *      Canary Kitchen (99e9d621) directly.
 *   2. The row's deferred_why ("no owner row - canary-bootstrap deletes it every run") is now
 *      contradicted by this run's own evidence below: canaryOwner signs in and lands on a full
 *      Canary Kitchen owner dashboard, not a 403/no-access screen.
 *
 * WHAT I WALK: steps 1-3 (Login, Verify Canary Kitchen, Navigate to Menu Mgmt) - all reads.
 * WHAT I DO NOT DO: steps 4-10 (Create CANARY-MENU/ITEM/ADDON/SIDE, attach, publish) are
 * MUTATIONS on Canary Kitchen, a fixture dozens of OTHER canary journeys in this ledger run
 * against hourly (boost, bar-tender, table-transfer, service-call-guard, ...). Creating a
 * persistent menu/item/addon/side on it mid-cycle is exactly the class of action this ledger's
 * own precedent refuses elsewhere (see modifier-link.spec.mjs, and the D3 rows throughout
 * ledger.json) - a false green for me is a corrupted fixture for every other journey. Steps
 * 11-14 (patron verifies the created item) are contingent on steps 4-10 and therefore also not
 * walkable - there is nothing created yet to look for.
 */
import { test } from "@playwright/test";
import fs from "node:fs";
import { journal, look, account, DWELL, OWNER, deskContext, closeRole, signInOwner } from "../lib-witness.mjs";

const RID = "99e9d621-6941-4acc-8d85-50f15f3a2f2b"; // Canary Kitchen - dev (corrected)

test("owner-menu", async ({ browser }) => {
  const jr = journal("owner-menu");
  jr.target({ app: "owner", baseUrl: OWNER, restaurantId: RID });
  jr.note("**Corrected fixture:** this journey targets Canary Kitchen (99e9d621), not "
        + '"Canary Owner Menu" (cdaf8ce3) - `runs/wave0/fixture-misassign.json` confirms the '
        + "prior mapping was wrong; a FALSE_GREEN was already retracted for it.");
  jr.note("_I walk the reads (login, verify restaurant, reach Menu Mgmt) and stop before any "
        + "create/publish step — those mutate Canary Kitchen, a fixture other canary journeys "
        + "in this ledger run against hourly. Steps 4-14 are therefore UNPROVEN, not failed._");

  const cO = await deskContext(browser, jr, "owner");
  const o = await cO.newPage();
  const ev = {};
  let verdict = "INCOMPLETE", why = "";

  try {
    await jr.step("OWNER", "I sign in (Cognito)", async id => {
      const a = account("canaryOwner");
      await signInOwner(o, a.email, a.password, RID);
      return (await look(o, jr, "owner", id, "signed in")).slice(0, 200);
    });

    await jr.step("OWNER", "I verify I am looking at Canary Kitchen, not some other restaurant",
      async id => {
        const t = await look(o, jr, "owner", id, "dashboard");
        ev.dashboardText = t.slice(0, 300);
        const named = /Canary Kitchen/i.test(t);
        if (!named)
          throw new Error(`Signed in, but the dashboard does not name "Canary Kitchen" - it `
                         + `reads: ${t.slice(0, 260)}`);
        return `Dashboard names Canary Kitchen. Reads: ${t.slice(0, 220)}`;
      });

    await jr.step("OWNER", "I navigate to Menu Management", async id => {
      await o.goto(`${OWNER}/owner/menu/?restaurantId=${RID}`, { waitUntil: "domcontentloaded" });
      await o.waitForTimeout(DWELL + 2000);
      const t = await look(o, jr, "owner", id, "menu mgmt");
      const menuish = /menu/i.test(t);
      ev.menuScreenText = t.slice(0, 300);
      if (!menuish)
        throw new Error(`Menu Mgmt did not render anything recognisable as a menu screen: `
                       + `${t.slice(0, 260)}`);
      return `Menu Mgmt screen reads: ${t.slice(0, 220)}`;
    });

    verdict = "PARTIAL_GREEN";
    why = "reached login, verified restaurant, and Menu Mgmt cleanly; stopped before any "
        + "create/publish mutation on a shared hourly canary fixture (steps 4-14 unproven)";
  } catch (e) {
    verdict = "STOPPED"; why = String(e.message || e).split("\n")[0].slice(0, 500);
  } finally {
    fs.writeFileSync(`${jr.dir}/evidence.json`, JSON.stringify(ev, null, 1));
    jr.finish(verdict, why);
    await closeRole(cO, jr, "owner");
  }
});
