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

test.describe('WFBO — Live Racing Payout Bonus/Cap Settings', () => {

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

    test('payout settings table lists every bet type with bonus and max payout inputs', async ({ page }) => {
        await page.goto(`${BASE_URL}/wfbo/live-racing/settings`);

        await expect(page.locator('[data-testid="live-racing-payout-settings-table"]')).toBeVisible();
        await expect(page.locator('[data-testid="input-bonus-win"]')).toBeVisible();
        await expect(page.locator('[data-testid="input-max-payout-win"]')).toBeVisible();
        await expect(page.locator('[data-testid="input-bonus-pick_6"]')).toBeVisible();
    });

    test('saving a bonus % and max payout persists and pre-fills on reload', async ({ page }) => {
        await page.goto(`${BASE_URL}/wfbo/live-racing/settings`);

        await page.fill('[data-testid="input-bonus-win"]', '15');
        await page.fill('[data-testid="input-max-payout-win"]', '500');
        await page.click('[data-testid="btn-save-live-racing-payout-settings"]');

        await expect(page.locator('[data-testid="flash-success"]')).toBeVisible();

        await page.goto(`${BASE_URL}/wfbo/live-racing/settings`);
        await expect(page.locator('[data-testid="input-bonus-win"]')).toHaveValue('15.00');
        await expect(page.locator('[data-testid="input-max-payout-win"]')).toHaveValue('500.00');

        // Setting is a shared singleton across the whole suite - leaving a
        // "win" bonus/cap in place would silently change payout math for
        // every other spec that settles a "win" bet. Clean up.
        await page.fill('[data-testid="input-bonus-win"]', '');
        await page.fill('[data-testid="input-max-payout-win"]', '');
        await page.click('[data-testid="btn-save-live-racing-payout-settings"]');
        await expect(page.locator('[data-testid="flash-success"]')).toBeVisible();
    });

    test('a negative bonus percentage is rejected', async ({ page }) => {
        await page.goto(`${BASE_URL}/wfbo/live-racing/settings`);

        await page.fill('[data-testid="input-bonus-win"]', '-5');
        await page.click('[data-testid="btn-save-live-racing-payout-settings"]');

        await expect(page.locator('[data-testid="flash-error"]').first()).toContainText('non-negative number');
    });

    test('leaving both fields blank clears any previously saved override', async ({ page }) => {
        await page.goto(`${BASE_URL}/wfbo/live-racing/settings`);
        await page.fill('[data-testid="input-bonus-forecast"]', '10');
        await page.click('[data-testid="btn-save-live-racing-payout-settings"]');
        await expect(page.locator('[data-testid="flash-success"]')).toBeVisible();

        await page.goto(`${BASE_URL}/wfbo/live-racing/settings`);
        await page.fill('[data-testid="input-bonus-forecast"]', '');
        await page.click('[data-testid="btn-save-live-racing-payout-settings"]');
        await expect(page.locator('[data-testid="flash-success"]')).toBeVisible();

        await page.goto(`${BASE_URL}/wfbo/live-racing/settings`);
        await expect(page.locator('[data-testid="input-bonus-forecast"]')).toHaveValue('');
    });

});
