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

test.describe('WFBO — Pusoy', () => {

    test.describe('Settings', () => {
        test.beforeEach(async ({ page }) => {
            await loginAs(page, ADMIN.mobile, ADMIN.password);
            await page.goto(`${BASE_URL}/wfbo/pusoy/settings`);
        });

        test('settings page loads with every section', async ({ page }) => {
            await expect(page.locator('[data-testid="pd-settings-form"]')).toBeVisible();
            for (const id of [
                'toggle-pd-enabled', 'toggle-pd-test-users-only', 'input-pd-stake-bronze', 'input-pd-stake-silver', 'input-pd-stake-gold',
                'input-pd-rake-percent', 'input-pd-private-rake-percent', 'input-pd-seats', 'toggle-pd-bot-fill', 'input-pd-bot-level',
                'input-pd-match-wait', 'input-pd-turn-seconds',
                'input-pd-daily-loss-cap',
            ]) {
                await expect(page.locator(`[data-testid="${id}"]`)).toBeVisible();
            }
            await expect(page.locator('[data-testid="pd-payout-example"]')).toContainText('the winner gets');
        });

        test('house fee, entries and bot strength save and persist', async ({ page }, testInfo) => {
            await page.fill('[data-testid="input-pd-rake-percent"]', '12');
            await page.fill('[data-testid="input-pd-private-rake-percent"]', '5');
            await page.fill('[data-testid="input-pd-stake-bronze"]', '150');
            await page.selectOption('[data-testid="input-pd-bot-level"]', 'hard');
            await page.fill('[data-testid="input-pd-daily-loss-cap"]', '3000');
            await page.click('[data-testid="btn-save-pd-settings"]');

            await expect(page.locator('[data-testid="flash-success"]')).toBeVisible();
            await expect(page.locator('[data-testid="input-pd-rake-percent"]')).toHaveValue('12');
            await expect(page.locator('[data-testid="input-pd-stake-bronze"]')).toHaveValue('150.00');
            await expect(page.locator('[data-testid="input-pd-bot-level"]')).toHaveValue('hard');
            await expect(page.locator('[data-testid="input-pd-daily-loss-cap"]')).toHaveValue('3000.00');
            // 4 x ₱150 = ₱600 pot, 12% fee = ₱72, winner gets ₱528.
            await expect(page.locator('[data-testid="pd-payout-example"]')).toContainText('₱528.00');
            // Private tables have their own fee: 4 x ₱150 = ₱600, 5% fee = ₱30, winner gets ₱570.
            await expect(page.locator('[data-testid="input-pd-private-rake-percent"]')).toHaveValue('5');
            await expect(page.locator('[data-testid="pd-private-payout-example"]')).toContainText('₱570.00');
            await page.locator('[data-testid="pd-private-payout-example"]').scrollIntoViewIfNeeded();
            await page.screenshot({ path: testInfo.outputPath('private-house-fee-setting.png') });

            // Put the defaults back for the front-end specs.
            await page.fill('[data-testid="input-pd-rake-percent"]', '10');
            await page.fill('[data-testid="input-pd-private-rake-percent"]', '10');
            await page.fill('[data-testid="input-pd-stake-bronze"]', '100');
            await page.selectOption('[data-testid="input-pd-bot-level"]', 'normal');
            await page.fill('[data-testid="input-pd-daily-loss-cap"]', '');
            await page.click('[data-testid="btn-save-pd-settings"]');
            await expect(page.locator('[data-testid="flash-success"]')).toBeVisible();
        });

        test('a house fee over 100% is rejected', async ({ page }) => {
            await page.fill('[data-testid="input-pd-rake-percent"]', '120');
            await page.click('[data-testid="btn-save-pd-settings"]');

            await expect(page.locator('[data-testid="flash-success"]')).toHaveCount(0);
            await expect(page.getByText('Must be between 0 and 100')).toBeVisible();
        });

    });

    test('Games menu links to the Pusoy hub', async ({ page }) => {
        await loginAs(page, ADMIN.mobile, ADMIN.password);
        await page.goto(`${BASE_URL}/wfbo/games/pusoy`);
        await expect(page.locator('[data-testid="wfbo-nav-game-hub-pd"]')).toHaveClass(/is-active/);
        await expect(page.locator('[data-testid="wfbo-page-title"]')).toContainText('Pusoy');
        await expect(page.locator('[data-testid="pd-hub-settings"]')).toBeVisible();
    });

    test('Results page loads for finance', async ({ page }) => {
        await loginAs(page, FINANCE.mobile, FINANCE.password);
        await page.goto(`${BASE_URL}/wfbo/pusoy/results`);
        await expect(page.locator('[data-testid="pd-results-table-card"]')).toBeVisible();
        await expect(page.getByText('Paid Hands', { exact: true })).toBeVisible();
    });

    test('Games Report lists Pusoy', async ({ page }) => {
        await loginAs(page, ADMIN.mobile, ADMIN.password);
        await page.goto(`${BASE_URL}/wfbo/games/report`);
        await expect(page.getByText('Pusoy').first()).toBeVisible();
    });
});
