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

// The YouTube race video has no YouTube controls - our own LIVE badge, round
// sound button and a tap layer over the video. It starts WITH sound.
// The launch flag allows autoplay with sound (like a player who has already
// used the site), so the result doesn't depend on the headless default.
// Blocked-autoplay fallback: live_racing_video_sound_blocked.spec.ts.

test.use({ launchOptions: { args: ['--autoplay-policy=no-user-gesture-required'] } });

test.describe('Live Racing — video sound control', () => {
    test.setTimeout(150_000);

    test('starts with sound, no YouTube controls, our button and a tap on the video toggle it', async ({ page }, testInfo) => {
        const eventId = await createYoutubeVideoEvent(page, 'LRVideoSound');
        await loginAs(page, USER.mobile, USER.password);
        await page.goto(`${BASE_URL}/play/live-racing/${eventId}`);

        const container = page.locator('#yt-player-container');
        const toggle = page.locator('[data-testid="lr-video-sound-toggle"]');
        const hint = page.locator('[data-testid="lr-video-sound-hint"]');
        const iframe = page.locator('iframe#yt-player');
        await expect(container).toHaveAttribute('data-player-ready', 'true', { timeout: 30_000 });

        expect(await iframe.getAttribute('src')).toContain('controls=0');
        await expect(iframe).toHaveCSS('pointer-events', 'none');
        await expect(page.locator('[data-testid="lr-video-live"]')).toBeVisible();

        // On load: sound on (and still on after the blocked-autoplay check).
        await page.waitForTimeout(2500);
        await expect(container).toHaveAttribute('data-muted', 'false');
        await expect(toggle).toHaveAttribute('aria-label', 'Mute stream');
        await expect(hint).toBeHidden();
        await page.screenshot({ path: testInfo.outputPath('lr-video-sound-on-desktop.png') });

        // Our button mutes, then unmutes.
        await toggle.click();
        await expect(container).toHaveAttribute('data-muted', 'true');
        await expect(toggle).toHaveAttribute('aria-label', 'Unmute stream');
        await expect(hint).toBeVisible();
        await page.screenshot({ path: testInfo.outputPath('lr-video-muted-desktop.png') });
        await toggle.click();
        await expect(container).toHaveAttribute('data-muted', 'false');

        // A tap anywhere on the video toggles too.
        await page.locator('[data-testid="lr-video-shield"]').click();
        await expect(container).toHaveAttribute('data-muted', 'true');
        await page.locator('[data-testid="lr-video-shield"]').click();
        await expect(container).toHaveAttribute('data-muted', 'false');

        await page.setViewportSize({ width: 390, height: 844 });
        await page.waitForTimeout(1000);
        await page.screenshot({ path: testInfo.outputPath('lr-video-sound-on-phone.png') });
    });
});
