
import { test, expect } from './fixtures';

test.describe('VI. User Interface & Experience', () => {

    test('6.1. A. Adaptive GUI - Mobile Navigation (Width < 768px)', async ({ page, createUniqueUser }) => {
        const user = await createUniqueUser();
        await page.goto('/');
        await page.getByLabel('Email').fill(user.email);
        await page.getByLabel('Password').fill(user.password);
        await page.getByRole('button', { name: 'Login' }).click();

        try {
            await expect(page.getByRole('heading', { name: /Change Password/i }).or(page.getByText('Free Workout'))).toBeVisible({ timeout: 5000 });
            if (await page.getByRole('heading', { name: /Change Password/i }).isVisible()) {
                await page.getByLabel('New Password').fill('StrongNewPass123!');
                await page.getByRole('button', { name: /Save|Change/i }).click();
            }
        } catch (e) { }
        await expect(page.getByText('Free Workout')).toBeVisible();

        await page.setViewportSize({ width: 375, height: 667 });

        await expect(page.getByText('Free Workout')).toBeVisible();

        await expect(page.getByRole('navigation', { name: /Bottom|Mobile/i })).toBeVisible();
        await expect(page.getByRole('button', { name: /Tracker/i })).toBeVisible();

        await expect(page.getByRole('navigation', { name: /Desktop|Side/i })).toBeHidden();
    });

    test('6.2. A. Adaptive GUI - Desktop Navigation (Width >= 768px)', async ({ page, createUniqueUser }) => {
        const user = await createUniqueUser();
        await page.goto('/');
        await page.getByLabel('Email').fill(user.email);
        await page.getByLabel('Password').fill(user.password);
        await page.getByRole('button', { name: 'Login' }).click();

        try {
            await expect(page.getByRole('heading', { name: /Change Password/i }).or(page.getByText('Free Workout'))).toBeVisible({ timeout: 5000 });
            if (await page.getByRole('heading', { name: /Change Password/i }).isVisible()) {
                await page.getByLabel('New Password').fill('StrongNewPass123!');
                await page.getByRole('button', { name: /Save|Change/i }).click();
            }
        } catch (e) { }
        await expect(page.getByText('Free Workout')).toBeVisible();

        await page.setViewportSize({ width: 1280, height: 800 });

        await expect(page.getByRole('navigation', { name: /Desktop|Side/i })).toBeVisible();
        await expect(page.getByRole('button', { name: 'Tracker' })).toBeVisible();

        await expect(page.getByRole('navigation', { name: /Bottom|Mobile/i })).toBeHidden();
    });

    test('6.3. A. Adaptive GUI - Responsive Charts in Stats', async ({ page, createUniqueUser }) => {
        const user = await createUniqueUser();
        await page.goto('/');
        await page.getByLabel('Email').fill(user.email);
        await page.getByLabel('Password').fill(user.password);
        await page.getByRole('button', { name: 'Login' }).click();

        try {
            await expect(page.getByRole('heading', { name: /Change Password/i }).or(page.getByText('Free Workout'))).toBeVisible({ timeout: 5000 });
            if (await page.getByRole('heading', { name: /Change Password/i }).isVisible()) {
                await page.getByLabel('New Password').fill('StrongNewPass123!');
                await page.getByRole('button', { name: /Save|Change/i }).click();
            }
        } catch (e) { }
        await expect(page.getByText('Free Workout')).toBeVisible();

        const checkNoHorizontalScroll = async () => {
            const scrollWidth = await page.evaluate(() => document.documentElement.scrollWidth);
            const clientWidth = await page.evaluate(() => document.documentElement.clientWidth);
            expect(scrollWidth).toBeLessThanOrEqual(clientWidth);
        };

        await page.getByRole('button', { name: 'Stats' }).click();

        const widths = [1280, 1024, 768, 600, 480, 375];
        const heights = [800, 768, 667];

        for (const width of widths) {
            for (const height of heights) {
                await page.setViewportSize({ width, height });
                await page.waitForTimeout(200);

                await checkNoHorizontalScroll();

                const noData = await page.getByText(/Not enough data/i).isVisible();
                if (noData) {
                    await expect(page.getByText(/Not enough data/i)).toBeVisible();
                } else {
                    await expect(page.getByRole('heading', { name: /Total Volume/i }).or(page.getByText('Total Volume'))).toBeVisible();
                    await expect(page.getByRole('heading', { name: /Set Count/i }).or(page.getByText('Set Count'))).toBeVisible();
                    await expect(page.getByRole('heading', { name: /Body Weight/i }).or(page.getByText('Body Weight'))).toBeVisible();
                    await expect(page.locator('svg').first()).toBeVisible();
                }
            }
        }
    });

    test('6.4. A. Adaptive GUI - Fluid Layout Responsiveness', async ({ page, createUniqueUser }) => {
        const user = await createUniqueUser();
        await page.goto('/');
        await page.getByLabel('Email').fill(user.email);
        await page.getByLabel('Password').fill(user.password);
        await page.getByRole('button', { name: 'Login' }).click();

        try {
            await expect(page.getByRole('heading', { name: /Change Password/i }).or(page.getByText('Free Workout'))).toBeVisible({ timeout: 5000 });
            if (await page.getByRole('heading', { name: /Change Password/i }).isVisible()) {
                await page.getByLabel('New Password').fill('StrongNewPass123!');
                await page.getByRole('button', { name: /Save|Change/i }).click();
            }
        } catch (e) { }
        await expect(page.getByText('Free Workout')).toBeVisible();

        const checkNoHorizontalScroll = async () => {
            const scrollWidth = await page.evaluate(() => document.documentElement.scrollWidth);
            const clientWidth = await page.evaluate(() => document.documentElement.clientWidth);
            expect(scrollWidth).toBeLessThanOrEqual(clientWidth);
        };

        const widths = [1280, 1024, 768, 600, 480, 375];
        const heights = [800, 768, 667];

        for (const width of widths) {
            for (const height of heights) {
                await page.setViewportSize({ width, height });
                await checkNoHorizontalScroll();

                await page.getByRole('button', { name: 'Plans' }).click();
                await checkNoHorizontalScroll();
                await page.getByRole('button', { name: 'Profile' }).click();
                await checkNoHorizontalScroll();
                await page.getByRole('button', { name: 'History' }).click();
                await checkNoHorizontalScroll();
                await page.getByRole('button', { name: 'Stats' }).click();
                await checkNoHorizontalScroll();
                await page.getByRole('button', { name: 'AI Coach' }).click();
                await checkNoHorizontalScroll();
                await page.getByRole('button', { name: 'Tracker' }).click();
                await checkNoHorizontalScroll();
            }
        }
    });
});
