import { test, expect, type Page } from '@playwright/test'

async function expectLocalePrefixed(page: Page, selector: string, locale: 'en' | 'nl') {
  const link = page.locator(selector).first()
  if (await link.count() === 0) return
  const href = await link.getAttribute('href')
  if (!href) return
  if (href.startsWith('http')) return // external
  expect(href.startsWith(`/${locale}`) || href === `/${locale}`).toBeTruthy()
}

test.describe('locale-aware internal links', () => {
  for (const locale of ['en', 'nl'] as const) {
    test(`header, breadcrumbs, menus are ${locale}-prefixed`, async ({ page }) => {
      await page.goto(`/${locale}/dashboard`)
      await page.waitForLoadState('networkidle')

      // Header brand
      await expectLocalePrefixed(page, 'header a[href]', locale)

      // Breadcrumb first item (Dashboard)
      await expectLocalePrefixed(page, '[data-part="breadcrumb"] a[href]', locale)

      // Account quick links rendered in layout
      await expectLocalePrefixed(page, 'a[href*="/dashboard/account"]', locale)

      // Sidebar menus (two UNavigationMenu blocks)
      const sidebarLinks = page.locator('nav a[href]').all()
      for (const linkPromise of await sidebarLinks) {
        const href = await linkPromise.getAttribute('href')
        if (!href || href.startsWith('http')) continue
        expect(href.startsWith(`/${locale}`) || href === `/${locale}`).toBeTruthy()
      }
    })
  }
})
