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

const baseUrl = "http://127.0.0.1:3001";
const regionName = "Delete Test Region";

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

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

    page.on("dialog", async (dialog) => {
      await dialog.accept();
    });

    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}/admin/regions`, { waitUntil: "networkidle" });

    const row = page.locator("tr", { hasText: regionName }).first();
    await row.getByRole("button").nth(1).click();
    await page.waitForLoadState("networkidle");

    const stillVisible = await page.locator("text=Delete Test Region").count();
    if (stillVisible > 0) {
      throw new Error("Region is still visible after delete.");
    }

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

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