import { test, expect } from '@playwright/test';
import {
    BASE_URL,
    USER,
    ADMIN,
    AUDITOR,
    loginAs,
    adjustWalletAsAuditor,
    ensureLiveRacingEnabled,
    createArena,
    createEventShowUrl,
    addRace,
    addHorses,
    offerSingleBetTypes,
    publishEvent,
    placeWizardBet,
    readWalletBalance,
} from '../helpers';

// The betting form has "Select Event" tabs (like the race / bet type tabs):
// today's event is the default, and every future published event can be
// picked for advance betting (it reloads the wizard for that event, keeping
// the scroll position). An event opened by URL is added to the tabs.
// See PlayController::liveRacingBetEvents() and _bet_event_tabs.html.twig.
// The fixtures publish an event for today; createEventShowUrl() dates events
// 2030+, so ours is a future event.

test.describe('Live Racing — event tabs in the betting form', () => {
    test.setTimeout(240_000);

    test('defaults to today, lists every future event, switches without scrolling to the top, and takes bets on a future event', async ({ page }) => {
        await loginAs(page, AUDITOR.mobile, AUDITOR.password);
        await adjustWalletAsAuditor(page, USER.mobile, '100', 'LR advance betting e2e');

        await loginAs(page, ADMIN.mobile, ADMIN.password);
        await ensureLiveRacingEnabled(page);
        const arena = await createArena(page, 'LRAdvance', { win: '10' });
        const { eventUrl, eventId } = await createEventShowUrl(page, arena);
        const race = await addRace(page, eventUrl, 1);
        const [h1] = await addHorses(page, race.raceUrl, 2);
        await offerSingleBetTypes(page, eventUrl, [{ key: 'win', raceId: race.raceId }]);
        await publishEvent(page, eventUrl);

        await loginAs(page, USER.mobile, USER.password);

        // Default: today's event.
        await page.goto(`${BASE_URL}/play/live-racing`);
        const tabs = page.locator('[data-testid="wizard-event-tabs"]');
        await expect(tabs).toBeVisible();
        await expect(tabs.locator('.wizard-event-tab.is-active')).toHaveText('Today');
        // "Select Event" heading, white like "Pick Your Bet".
        const heading = page.locator('[data-testid="betting-wizard-card"] .wizard-event-heading');
        await expect(heading).toHaveText('Select Event');
        await expect(heading).toHaveCSS('color', 'rgb(255, 255, 255)');
        // No "1 - 2" step indicator on the wizard any more.
        await expect(page.locator('[data-testid="wizard-step-indicator"]')).toHaveCount(0);

        // Every future published event is a tab - ours included, with an Advance badge.
        const ourTab = tabs.locator(`[data-testid="tab-event-${eventId}"]`);
        await expect(ourTab).toContainText('Advance');

        // Picking it reloads the wizard for that event without jumping back to the top.
        await page.evaluate(() => window.scrollTo(0, 400));
        await expect.poll(() => page.evaluate(() => window.scrollY)).toBeGreaterThan(300);
        await ourTab.click();
        await expect(page).toHaveURL(new RegExp(`/play/live-racing/${eventId}$`));
        await expect(ourTab).toHaveClass(/is-active/);
        await expect.poll(() => page.evaluate(() => window.scrollY)).toBeGreaterThan(300);
        await expect(tabs.locator('.wizard-event-tab', { hasText: 'Today' })).toHaveCount(1);
        const before = await readWalletBalance(page);

        await placeWizardBet(page, {
            eventId,
            raceId: race.raceId,
            cardLabel: /^Win$/,
            positions: { 1: [h1] },
            amount: '10',
        });

        await page.goto(`${BASE_URL}/play/live-racing/${eventId}`);
        expect(await readWalletBalance(page)).toBe(before - 10);

        // Back to today from its tab.
        await tabs.locator('.wizard-event-tab', { hasText: 'Today' }).click();
        await expect(page).toHaveURL(`${BASE_URL}/play/live-racing`);
    });
});
