/**
 * Shared helpers for the staging closed-issue verification suite.
 *
 * Credentials mirror prisma/seed-test-users.ts (seeded into pelagia_test) and the
 * dev-seed users in tests/e2e/helpers/login.ts.
 */
import { type Page, expect } from "@playwright/test";

export interface Credentials {
  email: string;
  password: string;
}

export const USERS = {
  TECH:      { email: "tech@pelagia.local",      password: "tech1234"     },
  MANNING:   { email: "manning@pelagia.local",   password: "manning1234"  },
  ACCOUNTS:  { email: "accounts@pelagia.local",  password: "accounts1234" },
  MANAGER:   { email: "manager@pelagia.local",   password: "manager1234"  },
  SUPERUSER: { email: "superuser@pelagia.local", password: "super1234"    },
  AUDITOR:   { email: "auditor@pelagia.local",   password: "audit1234"    },
  ADMIN:     { email: "admin@pelagia.local",     password: "admin1234"    },
  SITE:      { email: "site@pelagia.local",      password: "site1234"     },
} satisfies Record<string, Credentials>;

/** Log in via the credentials provider and wait until off the /login page. */
export async function login(page: Page, creds: Credentials): Promise<void> {
  await page.goto("/login");
  await page.getByLabel(/email address/i).fill(creds.email);
  await page.getByLabel(/password/i).fill(creds.password);
  // The staging login page has two buttons: "Sign in with Microsoft" (SSO) and the
  // credentials "Sign in" submit. Target the exact credentials submit.
  await page.getByRole("button", { name: "Sign in", exact: true }).click();
  await expect(page).not.toHaveURL(/\/login/, { timeout: 30_000 });
}

/** Log out via the header control (best-effort; ignores if already logged out). */
export async function logout(page: Page): Promise<void> {
  await page.context().clearCookies();
}
