import { chromium } from "@playwright/test";

const siteUrl = process.env.SITE_URL ?? "http://127.0.0.1:3000";
const publicRoutes = [
  "/",
  "/sell-your-house-fast.html",
  "/how-it-works.html",
  "/situations.html",
  "/locations.html",
  "/about.html",
  "/contact.html",
  "/privacy.html",
  "/terms.html",
  "/sell-as-is.html",
  "/foreclosure-help.html",
  "/sell-inherited-property.html",
  "/sell-during-divorce.html",
  "/seller-insights.html",
  "/compare-selling-options.html",
  "/blog.html",
  "/cash-sale-benefits.html",
  "/areas-we-serve/maryland/baltimore/",
];

const footerDestinations = [
  "/sell-your-house-fast.html",
  "/how-it-works.html",
  "/situations.html",
  "/locations.html",
  "/compare-selling-options.html",
  "/blog.html",
  "/cash-sale-benefits.html",
  "/blog.html#selling-without-agent",
  "/seller-insights.html",
  "/sell-as-is.html",
  "/foreclosure-help.html",
  "/sell-inherited-property.html",
  "/sell-during-divorce.html",
  "/about.html",
  "/contact.html",
  "/privacy.html",
  "/terms.html",
];

function assert(condition, message) {
  if (!condition) throw new Error(message);
}

async function dismissOfferDialog(page) {
  const closeButton = page.getByRole("button", { name: "Close cash offer form" });
  if (await closeButton.isVisible().catch(() => false)) {
    await closeButton.click();
  }
}

const browser = await chromium.launch({
  headless: true,
  executablePath: "/usr/bin/chromium",
});

try {
  const desktop = await browser.newPage({ viewport: { width: 1280, height: 720 } });

  for (const route of publicRoutes) {
    const response = await desktop.goto(`${siteUrl}${route}`, { waitUntil: "networkidle" });
    assert(response?.ok(), `Expected ${route} to return a successful page response.`);
    await desktop.locator("h1").waitFor({ state: "visible" });
    const heading = (await desktop.locator("h1").first().textContent())?.trim();
    assert(Boolean(heading), `Expected ${route} to render a visible page heading.`);
    assert(
      (await desktop.locator('a[href="/blog.html"]').count()) > 0,
      `Expected ${route} to expose the Seller Education blog from visible navigation.`,
    );
  }

  await desktop.goto(`${siteUrl}/blog.html`, { waitUntil: "networkidle" });
  await dismissOfferDialog(desktop);
  for (const href of footerDestinations) {
    assert(
      (await desktop.locator(`.site-footer a[href="${href}"]`).count()) > 0,
      `Expected a footer discovery link for ${href}.`,
    );
  }

  await desktop.goto(`${siteUrl}/blog.html#cash-sale-benefits`, { waitUntil: "networkidle" });
  await dismissOfferDialog(desktop);
  assert(
    await desktop.locator("#cash-sale-benefits").isVisible(),
    "Expected the cash-sale-benefits article to remain visible on the Seller Education blog.",
  );

  await desktop.goto(`${siteUrl}/blog.html`, { waitUntil: "networkidle" });
  await dismissOfferDialog(desktop);
  await desktop.locator('a[href="/cash-sale-benefits.html"]').first().click();
  await desktop.waitForURL(/\/cash-sale-benefits\.html$/);
  assert(
    await desktop.getByRole("heading", { name: "Cash Sale Benefits and Tradeoffs" }).isVisible(),
    "Expected the standalone cash-sale guide to be reachable from the existing blog article.",
  );

  const mobile = await browser.newPage({ viewport: { width: 375, height: 812 } });
  await mobile.goto(`${siteUrl}/`, { waitUntil: "networkidle" });
  await dismissOfferDialog(mobile);
  await mobile.getByRole("button", { name: "Toggle navigation" }).click();
  const mobileBlogLink = mobile.locator('.main-nav--open a[href="/blog.html"]');
  assert(await mobileBlogLink.isVisible(), "Expected Seller Education to remain visible in the open mobile menu.");
  await mobileBlogLink.click();
  await mobile.waitForURL(/\/blog\.html$/);
  assert(await mobile.locator("#cash-sale-benefits").isVisible(), "Expected the seller education article to render on mobile.");

  console.log(`Verified ${publicRoutes.length} public routes, footer resource links, article anchors, and mobile Seller Education navigation at ${siteUrl}.`);
} finally {
  await browser.close();
}
