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

type ContentApiEntry = {
    id?: string
    translationKey?: string
    lang?: string
    path?: string
    active?: boolean
    blocks?: Array<{ type?: string }>
}

async function getContentEntries(api: { get: Function }, filter: Record<string, unknown>): Promise<ContentApiEntry[]> {
    const res = await api.get(`${API_BASE}/content`, {
        params: {
            filter: JSON.stringify(filter),
        },
    })

    expect(res.ok()).toBeTruthy()

    const body = (await res.json()) as unknown
    return Array.isArray(body) ? (body as ContentApiEntry[]) : []
}

test.describe("Seeded Content API", () => {
    test("returns the seeded localized page variants through the public content endpoint", async ({ api }) => {
        const deEntries = await getContentEntries(api, {
            lang: "de",
            path: SEEDED_TEST_CONTENT.home.path,
            active: true,
        })
        const enEntries = await getContentEntries(api, {
            lang: "en",
            path: SEEDED_TEST_CONTENT.home.path,
            active: true,
        })

        expect(deEntries).toHaveLength(1)
        expect(enEntries).toHaveLength(1)
        expect(deEntries[0].translationKey).toBe(SEEDED_TEST_CONTENT.home.translationKey)
        expect(enEntries[0].translationKey).toBe(SEEDED_TEST_CONTENT.home.translationKey)
        expect(deEntries[0].blocks?.map((block) => block.type)).toEqual(["hero", "features", "richtext", "accordion"])
        expect(enEntries[0].blocks?.map((block) => block.type)).toEqual(["hero", "features", "richtext", "accordion"])
    })

    test("returns the seeded contact page and an empty result for missing active routes", async ({ api }) => {
        const contactEntries = await getContentEntries(api, {
            lang: "de",
            path: SEEDED_TEST_CONTENT.contact.path,
            active: true,
        })
        const inactiveEntries = await getContentEntries(api, {
            lang: "de",
            path: SEEDED_TEST_CONTENT.inactive.path,
            active: true,
        })
        const missingEntries = await getContentEntries(api, {
            lang: "de",
            path: "/playwright-e2e-missing",
            active: true,
        })

        expect(contactEntries).toHaveLength(1)
        expect(contactEntries[0].blocks?.map((block) => block.type)).toEqual(["hero", "contact-form"])
        expect(inactiveEntries).toHaveLength(0)
        expect(missingEntries).toHaveLength(0)
    })

    test("allows the admin API to inspect the inactive seeded entry", async ({ adminApi }) => {
        const res = await adminApi.get(`${API_BASE}/content`, {
            params: {
                filter: JSON.stringify({
                    translationKey: SEEDED_TEST_CONTENT.inactive.translationKey,
                    lang: "de",
                }),
            },
        })

        expect(res.ok()).toBeTruthy()
        const body = (await res.json()) as ContentApiEntry[]
        expect(body).toHaveLength(1)
        expect(body[0].active).toBe(false)
        expect(body[0].path).toBe(SEEDED_TEST_CONTENT.inactive.path)
    })
})
