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

const GAME_SLUGS = ['vk', 'pt', 'cg', 'mn', 'fk', 'ez', 'pk', 'h2h'];

test.describe('WFBO Games Report — rendering', () => {

    test.beforeEach(async ({ page }) => {
        await loginAs(page, ADMIN.mobile, ADMIN.password);
        await page.goto(`${BASE_URL}/wfbo/games/report`);
    });

    test('page heading reads "Games Report"', async ({ page }) => {
        await expect(page.locator('[data-testid="wfbo-page-title"]')).toHaveText('Games Report');
    });

    test('all eight aggregate summary cards render with definition sub-labels', async ({ page }) => {
        const cards = ['turnover', 'payout', 'commission', 'net-revenue',
                       'settled-bets', 'payout-rate', 'hold', 'active-players'];
        for (const c of cards) {
            await expect(page.locator(`[data-testid="games-report-card-${c}"]`)).toBeVisible();
            const def = page.locator(`[data-testid="games-report-card-${c}-def"]`);
            await expect(def).toBeVisible();
            expect((await def.innerText()).trim().length).toBeGreaterThan(10);
        }
        await expect(page.locator('[data-testid="games-report-card-turnover-def"]')).toContainText('staked');
        await expect(page.locator('[data-testid="games-report-card-hold-def"]')).toContainText('net revenue');
    });

    test('breakdown table renders one row per non-Live-Racing game plus a Total row', async ({ page }) => {
        await expect(page.locator('[data-testid="games-report-table"]')).toBeVisible();
        await expect(page.locator('[data-testid="games-report-legend"]')).toBeVisible();
        for (const slug of GAME_SLUGS) {
            await expect(page.locator(`[data-testid="games-report-row-${slug}"]`)).toBeVisible();
        }
        const totalRow = page.locator('[data-testid="games-report-total-row"]');
        await expect(totalRow).toContainText('Total');
        await expect(totalRow).toContainText('₱');
    });

    test('table has 11 analytics columns with hover-title definitions', async ({ page }) => {
        const heads = page.locator('[data-testid="games-report-table"] thead th');
        await expect(heads).toHaveCount(11);
        await expect(heads.nth(0)).toHaveText('Game');
        await expect(heads.nth(10)).toContainText('Hold');
        expect((await heads.nth(5).getAttribute('title')) ?? '').not.toEqual('');
    });

    test('the report does not list Live Racing', async ({ page }) => {
        await expect(page.locator('[data-testid="games-report-table"]')).not.toContainText('Live Racing');
    });

    test('period filter pills + custom month picker are present', async ({ page }) => {
        await expect(page.locator('[data-testid="period-this_month"]').first()).toBeVisible();
        await expect(page.locator('[data-testid="period-last_month"]').first()).toBeVisible();
        await expect(page.locator('input[type="month"]')).toBeVisible();
    });

    test('selecting a period pill updates the query string and re-renders', async ({ page }) => {
        await page.locator('[data-testid="period-last_month"]').first().click();
        await page.waitForLoadState('networkidle');
        expect(page.url()).toContain('period=last_month');
        await expect(page.locator('[data-testid="games-report-total-row"]')).toBeVisible();
    });

    test('footnote explains settled bets / reclassification / Live Racing exclusion', async ({ page }) => {
        const fn = page.locator('[data-testid="games-report-footnote"]');
        await expect(fn).toContainText('Settled bets exclude pending');
        await expect(fn).toContainText('House-agent commission');
        await expect(fn).toContainText('Live Racing is not included');
    });
});

test.describe('WFBO Games Report — role guard (admin only)', () => {

    test('ADMIN can access and sees the Report link in the Games group', async ({ page }) => {
        await loginAs(page, ADMIN.mobile, ADMIN.password);
        await page.goto(`${BASE_URL}/wfbo/games/report`);
        await expect(page.locator('[data-testid="games-report-table"]')).toBeVisible();
        await expect(page.locator('[data-testid="wfbo-nav-games-report"]')).toContainText('Report');
    });

    test('FINANCE user is blocked and has no nav link', async ({ page }) => {
        await loginAs(page, FINANCE.mobile, FINANCE.password);
        const res = await page.request.get(`${BASE_URL}/wfbo/games/report`);
        expect(res.status()).toBe(403);
        await page.goto(`${BASE_URL}/wfbo/deposits`);
        await expect(page.locator('[data-testid="wfbo-nav-games-report"]')).toHaveCount(0);
        await page.goto(`${BASE_URL}/wfbo/games/report`);
        await expect(page.locator('[data-testid="games-report-table"]')).not.toBeVisible();
    });

    test('OPERATIONS user is blocked', async ({ page }) => {
        await loginAs(page, OPERATIONS.mobile, OPERATIONS.password);
        const res = await page.request.get(`${BASE_URL}/wfbo/games/report`);
        expect(res.status()).toBe(403);
    });

    test('the seeded AUDITOR user (also ROLE_ADMIN) can access', async ({ page }) => {
        await loginAs(page, AUDITOR.mobile, AUDITOR.password);
        await page.goto(`${BASE_URL}/wfbo/games/report`);
        await expect(page.locator('[data-testid="games-report-table"]')).toBeVisible();
    });
});
