import { test, expect } from '@playwright/test';
import { BASE_URL, USER, loginAs, createYoutubeVideoEvent } from '../helpers';

// The site logo sits over the race video, bottom left, like a TV channel logo -
// never in the way of a tap (the sound button is bottom right).
test.describe('Live Racing — site logo on the race video', () => {
    test.setTimeout(120_000);

    test('shows the logo in the bottom-left corner of the video on phone and desktop', async ({ page }, testInfo) => {
        const eventId = await createYoutubeVideoEvent(page, 'LRVideoLogo');
        await loginAs(page, USER.mobile, USER.password);

        for (const [label, size] of [['phone', { width: 390, height: 844 }], ['desktop', { width: 1366, height: 900 }]] as const) {
            await page.setViewportSize(size);
            await page.goto(`${BASE_URL}/play/live-racing/${eventId}`);
            const container = page.locator('#yt-player-container');
            const logo = page.locator('[data-testid="lr-video-logo"]');
            await expect(container).toHaveAttribute('data-player-ready', 'true', { timeout: 30_000 });
            await expect(logo).toBeVisible();
            await expect(logo).toHaveCSS('pointer-events', 'none');
            expect(await logo.evaluate((img: HTMLImageElement) => img.complete && img.naturalWidth > 0)).toBe(true);

            const v = (await container.boundingBox())!;
            const l = (await logo.boundingBox())!;
            expect(l.x - v.x).toBeGreaterThanOrEqual(8);
            expect(l.x - v.x).toBeLessThan(14);
            expect(v.y + v.height - (l.y + l.height)).toBeGreaterThanOrEqual(8);
            expect(v.y + v.height - (l.y + l.height)).toBeLessThan(14);
            expect(l.width).toBeLessThan(v.width / 4);

            await page.waitForTimeout(1500);
            await container.screenshot({ path: testInfo.outputPath(`lr-video-logo-${label}.png`) });
            await page.screenshot({ path: testInfo.outputPath(`lr-video-logo-page-${label}.png`) });
        }
    });
});
