import { Given } from "@badeball/cypress-cucumber-preprocessor";

// Admin-specific steps for `admin-login.feature`. Generic navigation / form
// steps ("I visit", "I fill in the … field", "I click the button", "I
// should see …") are reused from `support/step_definitions/common.steps.ts`;
// only the `/admin/*` API mocks live here. Every admin call is stubbed via
// `cy.intercept` — this suite never runs a live backend (apps/api's Mocha
// suite covers real `/admin/*` behaviour).

const adminBody = {
  id: 1,
  email: "ops@example.com",
  name: "Ops",
  createdAt: "2026-08-01T00:00:00.000Z",
  lastLoginAt: "2026-08-28T09:00:00.000Z",
};

Given("the admin session check returns unauthenticated", () => {
  cy.intercept("GET", "**/admin/auth/me", { statusCode: 401, body: { code: 4011, message: "no" } });
});

Given("admin login fails with invalid credentials", () => {
  cy.intercept("POST", "**/admin/auth/login", {
    statusCode: 401,
    body: { code: 4010, message: "Invalid email or password" },
  });
});

Given("admin login succeeds as {string}", (name: string) => {
  const body = { ...adminBody, name, email: `${name.toLowerCase()}@example.com` };
  cy.intercept("POST", "**/admin/auth/login", { statusCode: 200, body });
  // After navigate("/admin"), RequireAdmin re-checks the session — from now
  // on it must report authenticated (last matching intercept wins).
  cy.intercept("GET", "**/admin/auth/me", { statusCode: 200, body });
});
