/**
 * The one UI-driven login spec for backoffice — every other spec uses the
 * programmatic cy.loginBackoffice() session.
 * Login form: apps/edr-freight-web/backoffice/src/pages/auth/LoginPage.tsx
 * (Mantine inputs, matched by placeholder).
 */
describe("backoffice: UI login", () => {
  it("redirects unauthenticated users to /auth", () => {
    cy.clearCookies();
    cy.visit("/dashboard/overview");
    cy.location("pathname").should("eq", "/auth");
  });

  it("logs in via the form and lands on the dashboard", () => {
    cy.clearCookies();
    cy.visit("/auth");
    cy.get('input[placeholder="name@company.com or 09XXXXXXXX"]').type("ceo@edr.local");
    cy.get('input[placeholder="Enter your password"]').type(
      Cypress.env("defaultPassword"),
      { log: false },
    );
    cy.get('button[type="submit"]').click();

    cy.location("pathname", { timeout: 20000 }).should("match", /^\/dashboard/);
    cy.getCookie("auth-token").should("exist");
    cy.getCookie("refresh-token").should("exist");
  });

  it("shows an error for wrong credentials and stays on /auth", () => {
    cy.clearCookies();
    cy.visit("/auth");
    cy.get('input[placeholder="name@company.com or 09XXXXXXXX"]').type("ceo@edr.local");
    cy.get('input[placeholder="Enter your password"]').type("definitely-wrong");
    cy.get('button[type="submit"]').click();

    cy.location("pathname").should("eq", "/auth");
    cy.getCookie("auth-token").should("not.exist");
  });
});

export {};
