import { test, expect, clickSpaLink, waitForSpaReady } from "./fixtures"
import { SEEDED_TEST_CONTENT } from "../fixtures/test-constants"

test.describe("Seeded Home Page", () => {
    test("renders the seeded German page with the expected block content", async ({ page }) => {
        await page.goto(`/de${SEEDED_TEST_CONTENT.home.path}`)
        const lang = await waitForSpaReady(page)

        expect(lang).toBe("de")
        await expect(page.getByRole("heading", { level: 1, name: "Playwright Seed Startseite" })).toBeVisible()
        await expect(page.locator("[data-block='features'] .feature-card")).toHaveCount(3)
        await expect(page.locator("[data-block='richtext']")).toContainText(
            "Dieser Richtext-Block prueft, dass formatierter HTML-Inhalt im SPA gerendert wird."
        )
        await expect(page.locator("[data-block='accordion'] button[aria-expanded]").first()).toHaveAttribute(
            "aria-expanded",
            "true"
        )
    })

    test("keeps the route stable when switching the language", async ({ page }) => {
        await page.goto(`/de${SEEDED_TEST_CONTENT.home.path}`)
        await waitForSpaReady(page)

        const header = page.locator("header")
        await expect(header).toBeVisible()
        await header.getByRole("link", { name: "en", exact: true }).click()
        await waitForSpaReady(page)

        await expect(page).toHaveURL(new RegExp(`/en${SEEDED_TEST_CONTENT.home.path}$`))
        await expect(page.getByRole("heading", { level: 1, name: "Playwright Seed Home" })).toBeVisible()
    })

    test("navigates to the seeded contact page via SPA CTA and keeps the form usable", async ({ page }) => {
        await page.goto(`/de${SEEDED_TEST_CONTENT.home.path}`)
        await waitForSpaReady(page)

        await clickSpaLink(page, "[data-block='hero'] a")

        await expect(page).toHaveURL(new RegExp(`/de${SEEDED_TEST_CONTENT.contact.path}$`))
        await expect(page.locator("[data-block='contact-form']")).toBeVisible()
        await expect(page.getByLabel("Name")).toBeVisible()
        await expect(page.getByLabel("E-Mail")).toBeVisible()
        await expect(page.locator("textarea[name='message']")).toBeVisible()
    })

    test("moves focus to the main content via the skip link", async ({ page }) => {
        await page.goto(`/de${SEEDED_TEST_CONTENT.home.path}`)
        await waitForSpaReady(page)

        await page.keyboard.press("Tab")

        const skipLink = page.getByRole("button", { name: "Zum Hauptinhalt springen" })
        await expect(skipLink).toBeFocused()

        await page.keyboard.press("Enter")

        const mainContent = page.locator("main#main-content")
        await expect(mainContent).toBeFocused()
    })
})
