/**
 * Cross-app flow: same business objects seen from both directions —
 * customer (portal, port 5373) and staff (backoffice, port 5383 = baseUrl).
 * Different ports = different origins, so portal steps inside a test that
 * also touches backoffice run inside cy.origin().
 *
 * Cookie caveat: cookies ignore ports, so both apps share the localhost
 * cookie jar. Always call the matching login command immediately before
 * switching apps — cy.session restores the right cookie snapshot.
 *
 * This spec is the template for full journeys (booking → approval →
 * scheduling → billing). It verifies both sides of the fence against
 * seeded data via UI + API cross-checks.
 */
const portal = () => Cypress.env("portalUrl") as string;
const api = () => Cypress.env("apiUrl") as string;

describe("flow: customer and staff see the same world", () => {
  it("staff views booking requests, customer views bookings", () => {
    // Staff side on the primary origin (baseUrl) first — the first origin a
    // test visits becomes primary; every other origin needs cy.origin().
    cy.loginBackoffice("ceo@edr.local");
    cy.visit("/dashboard/booking-requests");
    cy.location("pathname").should("eq", "/dashboard/booking-requests");
    cy.get("#root").should("not.be.empty");

    // Customer side — switch session first, then enter the portal origin.
    cy.loginPortal("user@gmail.com");
    cy.origin(portal(), () => {
      cy.visit("/bookings");
      cy.location("pathname").should("not.eq", "/login");
      cy.get("#root").should("not.be.empty");
    });
  });

  it("staff and customer both resolve their own /api/me identity", () => {
    cy.apiLogin("ceo@edr.local").then(({ token }) => {
      cy.request({
        url: `${api()}/api/me`,
        headers: { Authorization: `Bearer ${token}` },
      })
        .its("status")
        .should("eq", 200);
    });
    cy.apiLogin("user@gmail.com", Cypress.env("demoPassword")).then(({ token }) => {
      cy.request({
        url: `${api()}/api/me`,
        headers: { Authorization: `Bearer ${token}` },
      })
        .its("status")
        .should("eq", 200);
    });
  });

  it("cy.origin: staff dashboard then portal in a single test", () => {
    cy.loginBackoffice("ceo@edr.local");
    cy.visit("/dashboard/overview");
    cy.get("#root").should("not.be.empty");

    cy.loginPortal("user@gmail.com");
    cy.origin(Cypress.env("portalUrl") as string, () => {
      cy.visit("/portal");
      cy.location("pathname").should("not.eq", "/login");
      cy.get("#root").should("not.be.empty");
    });
  });
});

export {};
