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

// Live Racing ▸ Report (ROLE_ADMIN): revenue, payouts, bet types and top
// winners across the events dated in a period. The fixture event is dated
// today with two still-pending Win bets (₱10 each), so it shows up with ₱0
// settled and ₱20 pending.

test.describe('WFBO Live Racing Report', () => {

    test('admin sees this month\'s events with their figures, charts and links', async ({ page }, testInfo) => {
        await loginAs(page, ADMIN.mobile, ADMIN.password);
        await page.goto(`${BASE_URL}/wfbo`);
        await page.locator('button[aria-controls="live-racing-group-body"]').click();
        await page.click('[data-testid="wfbo-nav-live-racing-report"]');
        await expect(page).toHaveURL(/\/wfbo\/live-racing\/report/);
        await expect(page.locator('[data-testid="wfbo-nav-live-racing-report"]')).toHaveClass(/is-active/);
        await expect(page.locator('[data-testid="wfbo-nav-live-racing-settings"]')).not.toHaveClass(/is-active/);

        await page.click('[data-testid="lrr-period-this_month"]');
        const row = page.locator('[data-testid^="lrr-event-"]').filter({ has: page.locator('[data-testid^="lrr-event-link-"]') }).first();
        await expect(row).toBeVisible();
        await expect(page.locator('[data-testid="lrr-revenue"]')).toHaveText('₱0.00');
        await expect(page.locator('[data-testid="lrr-pending"]')).toContainText('₱20.00');
        await expect(page.locator('[data-testid="lrr-chart-events"] canvas')).toBeVisible();
        await page.waitForTimeout(1200);
        await page.screenshot({ path: testInfo.outputPath('live-racing-report.png'), fullPage: true });

        // The event links to its own Report tab.
        const link = row.locator('[data-testid^="lrr-event-link-"]');
        const eventId = ((await link.getAttribute('data-testid')) ?? '').replace('lrr-event-link-', '');
        await expect(link).toHaveAttribute('href', `/wfbo/events/${eventId}?tab=report`);

        // A custom range with no events.
        await page.fill('[data-testid="lrr-from"]', '2026-01-01');
        await page.fill('[data-testid="lrr-to"]', '2026-01-31');
        await page.click('[data-testid="lrr-range-submit"]');
        await expect(page.locator('[data-testid="lrr-period-label"]')).toContainText('Jan 1 – Jan 31, 2026');
        await expect(page.locator('[data-testid="lrr-empty"]')).toBeVisible();
    });

    test('a finance user (not admin) cannot open it', async ({ page }) => {
        await loginAs(page, FINANCE.mobile, FINANCE.password);
        const res = await page.goto(`${BASE_URL}/wfbo/live-racing/report`);
        expect(res?.status()).toBe(403);
    });
});
