import { expect, test } from '@playwright/test';

import {
    expectCtaHierarchy,
    expectDisclosureLayer,
    expectFooterLinks,
    expectHomepageHeroCtaPair,
    expectHomepageHeroOrder,
    expectHomepageHeroRouteTargets,
    expectHomepageHeroStructure,
    expectHomepageHeroTrustSignals,
    expectHomepageHeroVisibleOnMobile,
    expectHomepageSectionOrder,
    expectMobileReadability,
    expectNavigationVsCtaDifferentiation,
    expectOnwardRouteReachable,
    expectPageFamily,
    expectProductNearVisual,
    expectPrimaryNavigation,
    expectShell,
    visitPage,
} from './smoke-helpers';

test('home uses the landing foundation to explain the product category with one clear action hierarchy', async ({
    page,
}) => {
    await visitPage(page, '/');
    await expectShell(page, /TenantAtlas/);
    await expectPageFamily(page, 'landing');
    await expectDisclosureLayer(page, '1');
    await expectDisclosureLayer(page, '2');
    await expectPrimaryNavigation(page);
    await expectNavigationVsCtaDifferentiation(page);
    await expectFooterLinks(page);
    await expectCtaHierarchy(page, 'Request a working session', 'See the product model');
    await expect(page.getByRole('main').getByRole('link', { name: 'Request a working session' }).first()).toBeVisible();

    const skipLink = page.getByRole('link', { name: 'Skip to content' });

    await page.keyboard.press('Tab');
    await expect(skipLink).toBeFocused();
});

test('homepage hero explains the product with a product-near visual and outcome framing', async ({
    page,
}) => {
    await visitPage(page, '/');
    await expectProductNearVisual(page);
    await expect(page.locator('[data-section="outcome"]')).toBeVisible();
    await expect(
        page.getByRole('heading', { name: /calmer operations|governance pain|operational risk/i }).first(),
    ).toBeVisible();
    await expectCtaHierarchy(page, 'Request a working session', 'See the product model');
});

test('homepage maintains required section order: outcome before capability before trust before progress before cta', async ({
    page,
}) => {
    await visitPage(page, '/');
    await expectHomepageSectionOrder(page, ['outcome', 'capability', 'trust', 'progress', 'cta']);
});

test('homepage shows grouped capability clusters instead of a feature wall', async ({
    page,
}) => {
    await visitPage(page, '/');
    await expect(page.locator('[data-section="capability"]')).toBeVisible();
    await expect(
        page.getByRole('heading', { name: /product model|what TenantAtlas covers/i }),
    ).toBeVisible();
});

test('homepage shows explicit trust signals before the final CTA', async ({
    page,
}) => {
    await visitPage(page, '/');
    await expect(page.locator('[data-section="trust"]')).toBeVisible();
    await expectOnwardRouteReachable(page, ['/trust']);
});

test('homepage hero makes the product category, text core, and one CTA pair explicit on first read', async ({
    page,
}) => {
    await visitPage(page, '/');
    await expectHomepageHeroStructure(page);
    await expect(page.locator('[data-homepage-hero="true"] [data-hero-eyebrow]')).toContainText(/microsoft tenant governance/i);
    await expect(
        page.locator('[data-homepage-hero="true"] [data-hero-heading]').getByRole('heading', {
            level: 1,
            name: /one operating record for change history, drift review, and restore planning/i,
        }),
    ).toBeVisible();
    await expect(page.locator('[data-homepage-hero="true"] [data-hero-supporting-copy]')).toContainText(
        /security, endpoint, and platform teams use TenantAtlas to see what changed, preview restores, and move reviews forward/i,
    );
    await expectHomepageHeroCtaPair(page, /working session/i, /product model/i);
});

test('homepage hero keeps product-near proof and bounded trust cues inside the hero itself', async ({
    page,
}) => {
    await visitPage(page, '/');
    await expectProductNearVisual(page, /change history, restore preview, and a review queue/i);
    await expectHomepageHeroTrustSignals(page);
    await expect(page.locator('[data-homepage-hero="true"] [data-hero-trust-signals]')).toContainText(
        /tenant-scoped boundaries/i,
    );
    await expect(page.locator('[data-homepage-hero="true"] [data-hero-trust-signals]')).toContainText(
        /reviewable change history/i,
    );
    await expect(page.locator('[data-homepage-hero="true"] [data-hero-trust-signals]')).toContainText(
        /preview before restore/i,
    );
});

test('homepage shows dated progress signals before the final CTA', async ({
    page,
}) => {
    await visitPage(page, '/');
    await expect(page.locator('[data-section="progress"]')).toBeVisible();
    await expectOnwardRouteReachable(page, ['/changelog']);
});

test('homepage routes into Product, Trust, Changelog, and Contact', async ({
    page,
}) => {
    await visitPage(page, '/');
    await expectOnwardRouteReachable(page, ['/product', '/trust', '/changelog', '/contact']);
});

test.describe('homepage mobile', () => {
    test.use({ viewport: { width: 390, height: 844 } });

    test('homepage remains readable on narrow screens', async ({ page }) => {
        await visitPage(page, '/');
        await expectMobileReadability(page);
        await expect(page.locator('[data-section="outcome"]')).toBeVisible();
        await expect(page.locator('[data-section="capability"]')).toBeVisible();
        await expect(page.locator('[data-section="trust"]')).toBeVisible();
    });

    test('homepage hero preserves meaning order and hero route intent on narrow screens', async ({ page }) => {
        await visitPage(page, '/');
        await expectHomepageHeroOrder(page, [
            'eyebrow',
            'headline',
            'supporting-copy',
            'cta-pair',
            'product-near-visual',
            'trust-subclaims',
        ]);
        await expectHomepageHeroVisibleOnMobile(page);
        await expectHomepageHeroRouteTargets(page, ['/contact', '/product']);
    });
});

test('product keeps the connected operating model readable without collapsing into a feature list', async ({
    page,
}) => {
    await visitPage(page, '/product');
    await expectShell(page, /operating model|restore posture|governance/i);
    await expectPageFamily(page, 'landing');
    await expectDisclosureLayer(page, '1');
    await expectDisclosureLayer(page, '2');
    await expectPrimaryNavigation(page);
    await expectNavigationVsCtaDifferentiation(page);
    await expectFooterLinks(page);
    await expect(
        page.getByRole('heading', { name: 'Explain what the product does before asking for buyer trust.' }),
    ).toBeVisible();
    await expectCtaHierarchy(page, 'Review the trust posture', 'Start the working session');
    await expect(page.getByRole('main').getByRole('link', { name: 'Read the changelog' }).first()).toBeVisible();
});
