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

test.describe('WFBO — Royale King 999', () => {

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

        test('settings page loads with every section', async ({ page }) => {
            await expect(page.locator('[data-testid="l9-settings-form"]')).toBeVisible();
            for (const id of [
                'toggle-l9-enabled', 'input-l9-max-bet', 'input-l9-chip-values', 'input-l9-house-win-rate',
                'input-l9-multiplier-small', 'input-l9-multiplier-jackpot', 'input-l9-weight-try-again', 'input-l9-weight-small',
                'input-l9-streak-threshold', 'input-l9-streak-step', 'input-l9-streak-floor',
            ]) {
                await expect(page.locator(`[data-testid="${id}"]`)).toBeVisible();
            }
            await expect(page.locator('[data-testid="l9-rtp-hint"]')).toContainText('%');
        });

        test('max bet, chips, multipliers and house win rate save and persist', async ({ page }) => {
            await page.fill('[data-testid="input-l9-max-bet"]', '250');
            await page.fill('[data-testid="input-l9-chip-values"]', '5, 10, 20, 50');
            await page.fill('[data-testid="input-l9-multiplier-jackpot"]', '25');
            await page.fill('[data-testid="input-l9-house-win-rate"]', '65');
            await page.fill('[data-testid="input-l9-streak-threshold"]', '5');
            await page.fill('[data-testid="input-l9-streak-floor"]', '30');
            await page.click('[data-testid="btn-save-l9-settings"]');

            await expect(page.locator('[data-testid="flash-success"]')).toBeVisible();
            await expect(page.locator('[data-testid="input-l9-max-bet"]')).toHaveValue('250.00');
            await expect(page.locator('[data-testid="input-l9-chip-values"]')).toHaveValue('5, 10, 20, 50');
            await expect(page.locator('[data-testid="input-l9-multiplier-jackpot"]')).toHaveValue('25.00');
            await expect(page.locator('[data-testid="input-l9-house-win-rate"]')).toHaveValue('65');
            await expect(page.locator('[data-testid="input-l9-streak-threshold"]')).toHaveValue('5');
            await expect(page.locator('[data-testid="input-l9-streak-floor"]')).toHaveValue('30');

            // Put the defaults back for the front-end specs.
            await page.fill('[data-testid="input-l9-max-bet"]', '99');
            await page.fill('[data-testid="input-l9-multiplier-jackpot"]', '20');
            await page.fill('[data-testid="input-l9-house-win-rate"]', '');
            await page.fill('[data-testid="input-l9-streak-threshold"]', '4');
            await page.fill('[data-testid="input-l9-streak-floor"]', '35');
            await page.click('[data-testid="btn-save-l9-settings"]');
            await expect(page.locator('[data-testid="flash-success"]')).toBeVisible();
        });

        test('a multiplier of x1 or less is rejected', async ({ page }) => {
            await page.fill('[data-testid="input-l9-multiplier-small"]', '1');
            await page.click('[data-testid="btn-save-l9-settings"]');

            await expect(page.locator('[data-testid="flash-success"]')).toHaveCount(0);
            await expect(page.getByText('Must be more than x1 (x1 is Balik Taya)')).toBeVisible();
        });

        test('an out-of-range house win rate is rejected', async ({ page }) => {
            await page.fill('[data-testid="input-l9-house-win-rate"]', '150');
            await page.click('[data-testid="btn-save-l9-settings"]');

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

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

    test('Results page loads for finance', async ({ page }) => {
        await loginAs(page, FINANCE.mobile, FINANCE.password);
        await page.goto(`${BASE_URL}/wfbo/lucky-999/results`);
        await expect(page.locator('[data-testid="l9-results-table-card"]')).toBeVisible();
        await expect(page.getByText('Total Spins')).toBeVisible();
    });
});
