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

async function ensurePachinkoEnabled(page: Page) {
    await loginAs(page, ADMIN.mobile, ADMIN.password);
    await page.goto(`${BASE_URL}/wfbo/pachinko/settings`, { waitUntil: 'domcontentloaded' });
    const toggle = page.locator('[data-testid="toggle-pk-enabled"]');
    if (!(await toggle.isChecked())) {
        await toggle.check();
        await page.click('[data-testid="btn-save-pk-settings"]');
        await expect(page.locator('[data-testid="flash-success"]')).toBeVisible();
    }
}

test.describe('Pachinko — Game Page', () => {

    test.beforeEach(async ({ page }) => {
        await ensurePachinkoEnabled(page);
        await loginAs(page, USER.mobile, USER.password);
    });

    test('game page loads at /play/pachinko', async ({ page }) => {
        await page.goto(`${BASE_URL}/play/pachinko`, { waitUntil: 'domcontentloaded' });
        await expect(page.locator('[data-testid="pk-page"]')).toBeVisible();
        await expect(page.locator('[data-testid="pk-auto-btn"]')).toHaveText('AUTO DROP');
    });

    test('how-to overlay reflects the rebalanced multipliers', async ({ page }) => {
        await page.goto(`${BASE_URL}/play/pachinko`, { waitUntil: 'domcontentloaded' });
        await page.click('#howto-link');
        const overlay = page.locator('#howto-overlay');
        await expect(overlay).toHaveText(/1\.5x/);
        await expect(overlay).toHaveText(/0\.5x returns half/);
        await expect(overlay).toHaveText(/0x loses the full bet/);
        await expect(overlay).not.toHaveText(/0\.2x/);
        await expect(overlay).not.toHaveText(/0\.8x/);
        await expect(overlay).not.toHaveText(/1\.8x/);
    });

    test('unauthenticated access redirects to /login', async ({ page }) => {
        await page.context().clearCookies();
        await page.goto(`${BASE_URL}/play/pachinko`);
        expect(page.url()).toContain('/login');
    });

});

test.describe('Pachinko — Bet Amount Display', () => {

    test.beforeEach(async ({ page }) => {
        await ensurePachinkoEnabled(page);
        await loginAs(page, USER.mobile, USER.password);
        await page.goto(`${BASE_URL}/play/pachinko`, { waitUntil: 'domcontentloaded' });
    });

    test('bet amount is shown with a peso sign prefix', async ({ page }) => {
        const betValue = page.locator('[data-testid="pk-bet-value"]');
        await expect(betValue).toContainText('₱');
    });

    test('bet amount is labeled as the price per ball', async ({ page }) => {
        await expect(page.locator('[data-testid="pk-bet-sub"]')).toHaveText('Per Ball');
    });

    test('+/- buttons step the bet in 5s and keep the peso prefix', async ({ page }) => {
        const betValue = page.locator('[data-testid="pk-bet-value"]');
        await expect(betValue).toHaveText('₱ 50');

        await page.click('[data-testid="pk-bet-plus"]');
        await expect(betValue).toHaveText('₱ 55');

        await page.click('[data-testid="pk-bet-plus"]');
        await expect(betValue).toHaveText('₱ 60');

        await page.click('[data-testid="pk-bet-minus"]');
        await expect(betValue).toHaveText('₱ 55');
    });

});
