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

const baseUrl = "http://127.0.0.1:3001";

async function run() {
  const browser = await chromium.launch({ headless: true });

  try {
    const page = await browser.newPage();

    await page.goto(`${baseUrl}/login`, { waitUntil: "networkidle" });
    await page.locator("#email").fill("devwebtik@gmail.com");
    await page.locator("#password").fill("Webtik2026");
    await page.getByRole("button", { name: "Вход" }).click();
    await page.waitForURL(`${baseUrl}/admin`);

    await page.goto(baseUrl, { waitUntil: "networkidle" });

    const headerText = await page.locator("header").first().textContent();
    if (!headerText?.includes("Здравейте")) {
      throw new Error("Admin greeting is not visible on the frontend header.");
    }

    await page.getByRole("button", { name: /Здравейте/ }).click();

    const menuText = await page.locator("[role='menu']").textContent();
    if (!menuText?.includes("Админ панел")) {
      throw new Error("Admin dropdown does not contain a link to the admin panel.");
    }

    console.log(JSON.stringify({ ok: true }));
  } finally {
    await browser.close();
  }
}

run().catch((error) => {
  console.error(error);
  process.exit(1);
});
