import { test, expect } from '@playwright/test';
import {
    BASE_URL,
    USER,
    ADMIN,
    loginAs,
    ensureLiveRacingEnabled,
    createArena,
    createEventShowUrl,
    addRace,
    addHorses,
    offerSingleBetTypes,
    offerMultiBetTypeCombos,
    publishEvent,
    closeBetting,
    settleAndMarkRace,
} from '../helpers';

// A closed/settled race still appears in the betting wizard. A *settled* leg
// shows the finishing order + dividends in place of the picker; the "Place Bet"
// block is hidden while such a leg (or a multi-combo with any ran leg) is
// active; and the "betting is now closed" warning only shows while the *active*
// leg is a closed race with no result — it must not linger on a multi-combo's
// still-open legs, and must clear the instant you tab back to an open race
// (not after the 5s poll). See event.html.twig `updateClosedRaceState`.

test.describe('Live Racing wizard — closed/settled race behaviour', () => {
    test.setTimeout(180_000);

    test('settled leg shows result+dividends and hides Place Bet', async ({ page }) => {
        await loginAs(page, ADMIN.mobile, ADMIN.password);
        await ensureLiveRacingEnabled(page);

        const arena = await createArena(page, 'LRSettledLeg', { win: '10' });
        const { eventUrl, eventId } = await createEventShowUrl(page, arena);
        const r1 = await addRace(page, eventUrl, 1);
        const r2 = await addRace(page, eventUrl, 2);
        const h1 = await addHorses(page, r1.raceUrl, 4);
        await addHorses(page, r2.raceUrl, 4);
        await offerSingleBetTypes(page, eventUrl, [
            { key: 'win', raceId: r1.raceId },
            { key: 'win', raceId: r2.raceId },
        ]);
        await publishEvent(page, eventUrl);

        await closeBetting(page, r1.raceUrl);
        await page.goto(`${BASE_URL}${r1.raceUrl}`);
        await page.selectOption('[data-testid="select-finisher-1"]', h1[0]);
        await page.selectOption('[data-testid="select-finisher-2"]', h1[1]);
        await page.locator('[data-testid^="input-dividend-"]').first().fill('25.00');
        page.once('dialog', d => d.accept());
        await page.click('[data-testid="btn-save-result"]');
        await expect(page.locator('[data-testid="flash-success"]')).toBeVisible();
        await settleAndMarkRace(page, r1.raceId);

        await loginAs(page, USER.mobile, USER.password);
        await page.goto(`${BASE_URL}/play/live-racing/${eventId}`);
        await page.click('[data-testid="tab-bet-key-win"]');
        const winPanel = page.locator('[data-testid="key-panel-win"]');
        const placeBet = page.locator('[data-testid="wizard-place-bet"]');
        const warning = page.locator('[data-testid="wizard-race-closed-warning"]');
        const activeLeg = winPanel.locator('.wizard-single-leg:not(.d-none)');

        // Defaults to the open race 2: picker + Place Bet, no warning.
        await expect(activeLeg.locator('.horse-row').first()).toBeVisible();
        await expect(placeBet).toBeVisible();
        await expect(warning).toBeHidden();

        // Tab to the settled race 1: result block, no picker, Place Bet hidden,
        // no warning (deliberately viewing a past result).
        await winPanel.locator(`[data-testid^="tab-race-${r1.raceId}-bet-"]`).click();
        await expect(winPanel.locator(`[data-testid="wizard-leg-finish-${r1.raceId}"]`)).toHaveText('1/2');
        await expect(winPanel.locator(`[data-testid="wizard-leg-dividends-${r1.raceId}"]`)).toContainText('₱25.00');
        await expect(activeLeg.locator('.horse-row')).toHaveCount(0);
        await expect(placeBet).toBeHidden();
        await expect(warning).toBeHidden();

        // Back to race 2: Place Bet returns, still no warning.
        await winPanel.locator(`[data-testid^="tab-race-${r2.raceId}-bet-"]`).click();
        await expect(placeBet).toBeVisible();
        await expect(warning).toBeHidden();
    });

    test('multi-race combo: warning follows the active leg, not "any leg ran"', async ({ page }) => {
        await loginAs(page, ADMIN.mobile, ADMIN.password);
        await ensureLiveRacingEnabled(page);

        const arena = await createArena(page, 'LRMultiLeg', { pick_4: '10' });
        const { eventUrl, eventId } = await createEventShowUrl(page, arena);
        const races = [];
        for (let n = 1; n <= 4; n++) {
            const r = await addRace(page, eventUrl, n);
            // 6 (not 4) horses so a race row's horse-count cell can't collide
            // with the race-number text addRace() filters on.
            await addHorses(page, r.raceUrl, 6);
            races.push(r);
        }
        await offerMultiBetTypeCombos(page, eventUrl, 'pick_4', [races.map(r => r.raceId)]);
        await publishEvent(page, eventUrl);

        // Race 1 (first Pick 4 leg): close + result + settle. Races 2-4 open.
        // Pick 4's dividend is only entered on its last leg, so race 1's result
        // needs just a finisher.
        await closeBetting(page, races[0].raceUrl);
        await page.goto(`${BASE_URL}${races[0].raceUrl}`);
        await page.selectOption('[data-testid="select-finisher-1"]', await firstHorseId(page));
        page.once('dialog', d => d.accept());
        await page.click('[data-testid="btn-save-result"]');
        await expect(page.locator('[data-testid="flash-success"]')).toBeVisible();
        await settleAndMarkRace(page, races[0].raceId);

        await loginAs(page, USER.mobile, USER.password);
        await page.goto(`${BASE_URL}/play/live-racing/${eventId}`);
        await page.click('[data-testid="tab-bet-key-pick_4"]');
        const panel = page.locator('[data-testid^="key-panel-pick_4"]');
        const placeBet = page.locator('[data-testid="wizard-place-bet"]');
        const warning = page.locator('[data-testid="wizard-race-closed-warning"]');

        // Defaults to race 2 (open): Place Bet hidden (race 1 already ran) but
        // NO warning — the active leg is open.
        await expect(placeBet).toBeHidden();
        await expect(warning).toBeHidden();

        // Tab to the settled race 1 leg: its result shows, still no warning.
        await panel.locator(`[data-testid^="tab-race-${races[0].raceId}-bet-"]`).click();
        await expect(panel.locator(`[data-testid="wizard-leg-result-${races[0].raceId}"]`)).toBeVisible();
        await expect(warning).toBeHidden();

        // Tab to still-open race 3: no warning (this was the bug — it used to
        // linger because "any leg ran").
        await panel.locator(`[data-testid^="tab-race-${races[2].raceId}-bet-"]`).click();
        await expect(warning).toBeHidden();
        await expect(placeBet).toBeHidden();
    });
});

async function firstHorseId(page: import('@playwright/test').Page): Promise<string> {
    const opt = page.locator('[data-testid="select-finisher-1"] option').nth(1);
    return (await opt.getAttribute('value')) ?? '';
}
