import type { Page, TestInfo } from "@playwright/test";

function safeName(name: string): string {
  return name
    .trim()
    .toLowerCase()
    .replace(/[^a-z0-9-_]+/g, "-")
    .replace(/-+/g, "-")
    .replace(/^-|-$/g, "");
}

export async function captureEvidence(
  page: Page,
  testInfo: TestInfo,
  name: string,
) {
  const filename = `${safeName(name)}.png`;
  const fullPath = testInfo.outputPath(filename);
  await page.screenshot({ path: fullPath, fullPage: true });
  await testInfo.attach(name, {
    path: fullPath,
    contentType: "image/png",
  });
}
