const { test, expect } = require('@playwright/test');
const { openController, openDisplay, resetGame } = require('./helpers.cjs');

test.describe('Basic Flow: Controller ↔ Display', () => {

    test('dovrebbe caricare Display e Controller con i titoli corretti', async ({ context }) => {
        const displayPage = await openDisplay(context);
        const controllerPage = await openController(context);

        await expect(displayPage).toHaveTitle(/Segnapunti/);
        await expect(controllerPage).toHaveTitle(/Controller/);
    });

    test('il punteggio iniziale dovrebbe essere 0-0', async ({ context }) => {
        const controllerPage = await openController(context);
        await resetGame(controllerPage);

        const homeScore = controllerPage.locator('.team-score.home-bg .team-pts');
        const guestScore = controllerPage.locator('.team-score.guest-bg .team-pts');
        await expect(homeScore).toHaveText('0');
        await expect(guestScore).toHaveText('0');
    });

    test('click +1 Home sul Controller dovrebbe aggiornare il Display', async ({ context }) => {
        const displayPage = await openDisplay(context);
        const controllerPage = await openController(context);

        await resetGame(controllerPage);

        // Click +1 Home
        await controllerPage.locator('.team-score.home-bg').click();
        await controllerPage.waitForTimeout(200);

        // Verifica Controller mostra 1
        await expect(controllerPage.locator('.team-score.home-bg .team-pts')).toHaveText('1');

        // Verifica Display mostra 1 (il punteggio grande)
        await expect(displayPage.locator('.punt.home')).toHaveText('1');
    });

    test('click +1 Guest sul Controller dovrebbe aggiornare il Display', async ({ context }) => {
        const displayPage = await openDisplay(context);
        const controllerPage = await openController(context);

        await resetGame(controllerPage);

        // Click +1 Guest
        await controllerPage.locator('.team-score.guest-bg').click();
        await controllerPage.waitForTimeout(200);

        // Verifica Controller
        await expect(controllerPage.locator('.team-score.guest-bg .team-pts')).toHaveText('1');

        // Verifica Display
        await expect(displayPage.locator('.punt.guest')).toHaveText('1');
    });

    test('la sincronizzazione dovrebbe funzionare con punti alternati', async ({ context }) => {
        const displayPage = await openDisplay(context);
        const controllerPage = await openController(context);

        await resetGame(controllerPage);

        // Home +1, Guest +1, Home +1
        await controllerPage.locator('.team-score.home-bg').click();
        await controllerPage.waitForTimeout(100);
        await controllerPage.locator('.team-score.guest-bg').click();
        await controllerPage.waitForTimeout(100);
        await controllerPage.locator('.team-score.home-bg').click();
        await controllerPage.waitForTimeout(200);

        // Controller: Home 2, Guest 1
        await expect(controllerPage.locator('.team-score.home-bg .team-pts')).toHaveText('2');
        await expect(controllerPage.locator('.team-score.guest-bg .team-pts')).toHaveText('1');

        // Display: Home 2, Guest 1
        await expect(displayPage.locator('.punt.home')).toHaveText('2');
        await expect(displayPage.locator('.punt.guest')).toHaveText('1');
    });
});
