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

// Steps shared across admin feature specs — navigation and generic UI
// assertions. Anything specific to one feature (its own API mocks, its own
// DOM structure) lives in that feature's own `<name>.ts` step file.
//
// Every admin API call is mocked via `cy.intercept` — the Cypress suite
// never runs a live backend; apps/api's own Mocha suite covers real
// `/admin/*` behaviour.

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

Given("I am signed in as admin {string}", (name: string) => {
  cy.intercept("GET", "**/admin/auth/me", {
    statusCode: 200,
    body: {
      id: 1,
      email: `${name.toLowerCase()}@example.com`,
      name,
      createdAt: "2026-08-01T00:00:00.000Z",
      lastLoginAt: "2026-08-28T09:00:00.000Z",
    },
  });
});

When("I visit {string}", (path: string) => {
  cy.visit(path);
});

When("I fill in the {string} field with {string}", (fieldId: string, value: string) => {
  cy.get(`#${fieldId}`).clear().type(value);
});

When("I click the button {string}", (text: string) => {
  cy.contains("button", text).click();
});

Then("the URL should include {string}", (fragment: string) => {
  cy.url().should("include", fragment);
});

Then("the URL should not include {string}", (fragment: string) => {
  cy.url().should("not.include", fragment);
});

Then("I should see {string}", (text: string) => {
  cy.contains(text).should("be.visible");
});

Then("I should see the heading {string}", (text: string) => {
  cy.contains("h1", text).should("be.visible");
});
