/// <reference types="cypress" />
export {};

Cypress.env("AUTO_LOGIN", false);

describe("sign-in theme persistence", () => {
  after(() => {
    Cypress.env("AUTO_LOGIN", true);
  });

  beforeEach(() => {
    cy.clearCookies();
    cy.clearLocalStorage();

    cy.intercept("POST", "**/api/proxy/profile/login*", (req) => {
      req.reply({
        delay: 2500,
        statusCode: 200,
        body: {
          success: true,
          message: "ok",
          data: {
            access_token: "e2e-access-token",
            refresh_token: "e2e-refresh-token",
            expires_at: Date.now() + 60_000,
            user: {
              id: "507f1f77bcf86cd799439011",
              username: "tester",
              email: "tester@example.com",
              role: "admin",
            },
          },
        },
      });
    }).as("loginRequest");

    cy.visit("/auth/sign-in");
    cy.location("pathname").should("eq", "/auth/sign-in");
  });

  it("keeps selected theme after user logs in and lands on dashboard", () => {
    cy.contains("button", "Switch to dark mode").click();
    cy.get("html").should("have.class", "dark");

    cy.get('input[name="username"]').type("tester");
    cy.get('input[name="password"]').type("Secret123!");
    cy.contains('button[type="submit"]', "Sign in").click();

    cy.wait("@loginRequest");
    cy.location("pathname").should("eq", "/");
    cy.get("html").should("have.class", "dark");
  });
});
