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

// LiveRacingEventFixtures seeds two equal-stake pending Win bets placed today
// (players +10000000101 / +10000000102). Pending bets count toward Total Bet
// but not Wins, so Total Bet lists both while Win Count leaves them out.
test.describe('WFBO Live Racing Leaderboard', () => {

    test.beforeEach(async ({ page }) => {
        await loginAs(page, ADMIN.mobile, ADMIN.password);
    });

    test('nav link opens the leaderboard, defaulting to Forecast ranked by Win Count', async ({ page }) => {
        await page.goto(`${BASE_URL}/wfbo`);
        // The Live Racing sidebar group starts collapsed outside its own pages.
        await page.locator('button[aria-controls="live-racing-group-body"]').click();
        await page.locator('[data-testid="wfbo-nav-live-racing-leaderboard"]').click();

        await expect(page).toHaveURL(/\/wfbo\/live-racing\/leaderboard/);
        await expect(page.locator('[data-testid="wfbo-nav-live-racing-leaderboard"]')).toHaveClass(/is-active/);
        await expect(page.locator('[data-testid="lr-lb-bet-type"]')).toHaveValue('forecast');
        await expect(page.locator('[data-testid="lr-lb-sort"]')).toHaveValue('win_count');
        await expect(page.locator('[data-testid="lr-lb-info"]')).toContainText('Forecast');
        await expect(page.locator('[data-testid="lr-lb-info"]')).toContainText('ranked by Win Count');
    });

    test('bet type + custom date range + sort filter the board and podium rows are styled', async ({ page }) => {
        const today = new Intl.DateTimeFormat('en-CA', { timeZone: 'Asia/Manila' }).format(new Date());

        await page.goto(`${BASE_URL}/wfbo/live-racing/leaderboard`);
        await page.locator('[data-testid="lr-lb-bet-type"]').selectOption('win');
        await page.locator('[data-testid="lr-lb-date-from"]').fill(today);
        await page.locator('[data-testid="lr-lb-date-to"]').fill(today);
        await page.locator('[data-testid="lr-lb-apply"]').click();

        await expect(page).toHaveURL(new RegExp(`bet_type=win.*date_from=${today}.*date_to=${today}`));
        await expect(page.locator('[data-testid="lr-lb-info"]')).toContainText('Win');

        // Win Count only ranks players with at least one win (other specs may
        // have settled some today; the seeded pending bets never appear here).
        const winCells = page.locator('[data-testid^="lr-lb-wins-"]');
        for (const text of await winCells.allTextContents()) {
            expect(Number(text.trim())).toBeGreaterThan(0);
        }

        await page.locator('[data-testid="lr-lb-sort"]').selectOption('total_bet');
        await page.locator('[data-testid="lr-lb-apply"]').click();

        await expect(page.locator('[data-testid="lr-lb-info"]')).toContainText('ranked by Total Bet');
        // Both seeded bettors are listed even though their bets are still pending.
        const table = page.locator('[data-testid="lr-lb-table"]');
        await expect(table).toContainText('+10000000101');
        await expect(table).toContainText('+10000000102');

        const rows = page.locator('[data-testid^="lr-lb-row-"]');
        await expect(rows.nth(0)).toHaveClass(/lb-row--gold/);
        await expect(rows.nth(1)).toHaveClass(/lb-row--silver/);
        if (await rows.count() >= 4) {
            await expect(rows.nth(3)).not.toHaveClass(/lb-row--/);
        }

        // Period pills drop the custom range but keep bet type + sort.
        await page.locator('[data-testid="lr-lb-period-today"]').click();
        await expect(page).toHaveURL(/period=today/);
        await expect(page.locator('[data-testid="lr-lb-date-from"]')).toHaveValue('');
        await expect(page.locator('[data-testid="lr-lb-bet-type"]')).toHaveValue('win');
        await expect(page.locator('[data-testid="lr-lb-sort"]')).toHaveValue('total_bet');
    });

});
