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

test.describe('WFBO — Fruit King Settings', () => {

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

    test('fk-settings page loads with form', async ({ page }) => {
        await page.goto(`${BASE_URL}/wfbo/fruit-king/settings`);
        await expect(page.locator('[data-testid="fk-settings-form"]')).toBeVisible();
    });

    test('save button is present', async ({ page }) => {
        await page.goto(`${BASE_URL}/wfbo/fruit-king/settings`);
        await expect(page.locator('[data-testid="btn-save-fk-settings"]')).toBeVisible();
    });

    test('fk-enabled toggle wrapper is visible', async ({ page }) => {
        await page.goto(`${BASE_URL}/wfbo/fruit-king/settings`);
        await expect(page.locator('[data-testid="input-fk-enabled-wrapper"]')).toBeVisible();
    });

    test('submitting with unchanged values shows flash-success', async ({ page }) => {
        await page.goto(`${BASE_URL}/wfbo/fruit-king/settings`);
        await page.click('[data-testid="btn-save-fk-settings"]');
        await expect(page.locator('[data-testid="flash-success"]')).toBeVisible();
    });

    test('updating house win rate saves and persists', async ({ page }) => {
        await page.goto(`${BASE_URL}/wfbo/fruit-king/settings`);
        await page.fill('[data-testid="input-fk-house-win-rate"]', '10');
        await page.click('[data-testid="btn-save-fk-settings"]');
        await expect(page.locator('[data-testid="flash-success"]')).toBeVisible();
        await expect(page.locator('[data-testid="input-fk-house-win-rate"]')).toHaveValue('10');
    });

    test('max bet amount can be updated and persists', async ({ page }) => {
        await page.goto(`${BASE_URL}/wfbo/fruit-king/settings`);
        await expect(page.locator('[data-testid="input-fk-max-bet-amount"]')).toBeVisible();
        await page.fill('[data-testid="input-fk-max-bet-amount"]', '150');
        await page.click('[data-testid="btn-save-fk-settings"]');
        await expect(page.locator('[data-testid="flash-success"]')).toBeVisible();
        await expect(page.locator('[data-testid="input-fk-max-bet-amount"]')).toHaveValue('150.00');
    });

    test('max symbols per spin can be updated and persists', async ({ page }) => {
        await page.goto(`${BASE_URL}/wfbo/fruit-king/settings`);
        await expect(page.locator('[data-testid="input-fk-max-symbols-per-spin"]')).toBeVisible();
        await page.fill('[data-testid="input-fk-max-symbols-per-spin"]', '3');
        await page.click('[data-testid="btn-save-fk-settings"]');
        await expect(page.locator('[data-testid="flash-success"]')).toBeVisible();
        await expect(page.locator('[data-testid="input-fk-max-symbols-per-spin"]')).toHaveValue('3');
    });

    test('smallest-bet-wins toggle can be toggled off and persists', async ({ page }) => {
        await page.goto(`${BASE_URL}/wfbo/fruit-king/settings`);
        const toggle = page.locator('[data-testid="toggle-fk-smallest-bet-wins"]');
        await expect(toggle).toBeVisible();

        if (await toggle.isChecked()) {
            await toggle.uncheck();
        }
        await page.click('[data-testid="btn-save-fk-settings"]');
        await expect(page.locator('[data-testid="flash-success"]')).toBeVisible();
        await expect(toggle).not.toBeChecked();

        // Restore default ON so other specs run against the intended behavior.
        await toggle.check();
        await page.click('[data-testid="btn-save-fk-settings"]');
        await expect(page.locator('[data-testid="flash-success"]')).toBeVisible();
        await expect(toggle).toBeChecked();
    });

    test('FINANCE user is blocked from fk-settings (ROLE_ADMIN required)', async ({ page }) => {
        await loginAs(page, FINANCE.mobile, FINANCE.password);
        await page.goto(`${BASE_URL}/wfbo/fruit-king/settings`);
        await expect(page.locator('[data-testid="fk-settings-form"]')).not.toBeVisible();
    });

});
