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

// User Report (ROLE_ADMIN): registered, logged-in and active players per
// day / week / month with KPI cards. A player visiting the site is recorded in
// user_daily_presence, so after USER logs in today counts as logged in.

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

    test('a player visit shows up as logged in; admin switches daily / weekly / monthly', async ({ browser, page }, testInfo) => {
        const playerCtx = await browser.newContext({ ignoreHTTPSErrors: true });
        const player = await playerCtx.newPage();
        await loginAs(player, USER.mobile, USER.password);
        await playerCtx.close();

        await loginAs(page, ADMIN.mobile, ADMIN.password);
        await page.goto(`${BASE_URL}/wfbo`);
        await page.click('[data-testid="wfbo-nav-user-report"]');
        await expect(page).toHaveURL(/\/wfbo\/users\/report/);
        await expect(page.locator('[data-testid="wfbo-nav-user-report"]')).toHaveClass(/is-active/);
        await expect(page.locator('[data-testid="wfbo-nav-users"]')).not.toHaveClass(/is-active/);

        // Daily by default: KPIs, chart and today's row (newest first).
        await expect(page.locator('[data-testid="ur-range-daily"]')).toHaveClass(/is-active/);
        for (const kpi of ['total', 'registered', 'logged-in', 'active', 'ftd', 'activation', 'conversion', 'dau']) {
            await expect(page.locator(`[data-testid="ur-kpi-${kpi}"]`)).toBeVisible();
        }
        expect(Number((await page.locator('[data-testid="ur-kpi-logged-in-value"]').innerText()).replace(/,/g, ''))).toBeGreaterThanOrEqual(1);
        const today = page.locator('[data-testid="ur-table"] tbody tr').first();
        expect(Number((await today.locator('td').nth(2).innerText()).replace(/,/g, ''))).toBeGreaterThanOrEqual(1);
        await expect(page.locator('[data-testid="ur-table"] tbody tr')).toHaveCount(30);
        await expect(page.locator('[data-testid="ur-chart"] canvas')).toBeVisible();
        await page.waitForTimeout(800);
        await page.screenshot({ path: testInfo.outputPath('user-report-daily.png'), fullPage: true });

        await page.click('[data-testid="ur-range-weekly"]');
        await expect(page).toHaveURL(/range=weekly/);
        await expect(page.locator('[data-testid="ur-range-weekly"]')).toHaveClass(/is-active/);
        await expect(page.locator('[data-testid="ur-table"] tbody tr')).toHaveCount(12);
        await expect(page.locator('[data-testid="ur-chart"] canvas')).toBeVisible();
        await page.waitForTimeout(800);
        await page.screenshot({ path: testInfo.outputPath('user-report-weekly.png'), fullPage: true });

        await page.click('[data-testid="ur-range-monthly"]');
        await expect(page).toHaveURL(/range=monthly/);
        await expect(page.locator('[data-testid="ur-range-monthly"]')).toHaveClass(/is-active/);
        await expect(page.locator('[data-testid="ur-table"] tbody tr')).toHaveCount(12);
        await page.waitForTimeout(800);
        await page.screenshot({ path: testInfo.outputPath('user-report-monthly.png'), fullPage: true });
    });

    test('a finance user (not admin) has no link and cannot open it', async ({ page }) => {
        await loginAs(page, FINANCE.mobile, FINANCE.password);
        await page.goto(`${BASE_URL}/wfbo/deposits`);
        await expect(page.locator('[data-testid="wfbo-nav-user-report"]')).toHaveCount(0);
        const res = await page.goto(`${BASE_URL}/wfbo/users/report`);
        expect(res?.status()).toBe(403);
    });
});
