// Mocks the API via cy.intercept — this job doesn't run a live backend (see
// .github/workflows/ci.yml); apps/api's own Mocha suite covers real API
// behavior against a real database.

const authenticatedProfile = {
  id: 1,
  firstName: "Alice",
  lastName: "Martin",
  email: "alice@example.com",
  tokenVersion: 0,
  houseId: null,
  dietId: null,
};

describe("Sidebar — settings menu and account menu", () => {
  beforeEach(() => {
    cy.intercept("GET", "**/auth/me", { statusCode: 200, body: authenticatedProfile });
    // Not "**/planning*" — that glob also matches the Vite dev request for
    // planning-page.scss (see planning-page.cy.ts for the same gotcha).
    cy.intercept("GET", /\/planning\?/, { statusCode: 200, body: null });
  });

  it("no longer lists Foyer in the main nav", () => {
    cy.visit("/");
    cy.get(".app-sidebar__nav a").should("not.contain", "Foyer");
  });

  it("reveals the four settings pages behind the Paramètres toggle", () => {
    cy.visit("/");

    cy.contains("a", "Compte").should("not.exist");
    cy.contains("button", "Paramètres").click();

    cy.contains("a", "Compte").should("have.attr", "href", "/parametres/compte");
    cy.contains("a", "Préférences alimentaires").should(
      "have.attr",
      "href",
      "/parametres/preferences",
    );
    cy.contains("a", "Foyer").should("have.attr", "href", "/parametres/foyer");
    cy.contains("a", "Préférences utilisateur").should(
      "have.attr",
      "href",
      "/parametres/preferences-utilisateur",
    );
  });

  it("opens the account menu from the greeting and links to Mon compte", () => {
    cy.visit("/");

    cy.contains("a", "Mon compte").should("not.exist");
    cy.contains("button", "Bonjour Alice").click();
    cy.contains("a", "Mon compte").click();

    cy.url().should("include", "/parametres/compte");
  });

  it("redirects the old /foyer path to /parametres/foyer", () => {
    cy.intercept("GET", "**/house/current", { statusCode: 200, body: null });
    cy.visit("/foyer");
    cy.url().should("include", "/parametres/foyer");
  });

  it("collapses to an icon-only rail and back, persisting the choice across reloads", () => {
    cy.visit("/");

    cy.get(".app-sidebar").should("not.have.class", "collapsed");
    cy.contains("nav a", "Planning").should("be.visible");

    cy.get(".app-sidebar__collapse-toggle").click();
    cy.get(".app-sidebar").should("have.class", "collapsed");
    // The label text hides (not removed from the DOM — still there for the
    // `title` tooltip/accessibility), while the link itself stays visible,
    // icon-only.
    cy.contains("span.label", "Planning").should("exist").and("not.be.visible");
    cy.get("nav a[title='Planning']").should("be.visible");

    cy.reload();
    cy.get(".app-sidebar").should("have.class", "collapsed");

    cy.get(".app-sidebar__collapse-toggle").click();
    cy.get(".app-sidebar").should("not.have.class", "collapsed");
    cy.contains("nav a", "Planning").should("be.visible");
  });
});
