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

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

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

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

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

    test('referral code required checkbox is visible', async ({ page }) => {
        await page.goto(`${BASE_URL}/wfbo/settings`);
        await expect(page.locator('[data-testid="input-referral-code-required-wrapper"]')).toBeVisible();
    });

    test('player and agent withdrawal schedules are visible', async ({ page }) => {
        await page.goto(`${BASE_URL}/wfbo/settings`);
        await expect(page.locator('[data-testid="input-withdrawal-window-schedule"]')).toBeVisible();
        await expect(page.locator('[data-testid="input-agent-withdrawal-window-schedule"]')).toBeVisible();
        await expect(page.locator('[data-testid="input-withdrawal-day-1-start-hour"]')).toBeVisible();
    });

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

    test('changing agentCommissionRate persists after save', async ({ page }) => {
        await page.goto(`${BASE_URL}/wfbo/settings`);

        const rateInput = page.locator('input[id*="agentCommissionRate"], input[name*="agentCommissionRate"]').first();
        await rateInput.fill('5.50');
        await page.click('[data-testid="btn-save-settings"]');
        await expect(page.locator('[data-testid="flash-success"]')).toBeVisible();

        await page.reload();
        const savedValue = await rateInput.inputValue();
        expect(parseFloat(savedValue)).toBeCloseTo(5.5, 1);
    });

    test('changing siteName persists after save', async ({ page }) => {
        await page.goto(`${BASE_URL}/wfbo/settings`);

        const siteNameInput = page.locator('input[id*="siteName"], input[name*="siteName"]').first();
        const originalName = await siteNameInput.inputValue();
        const newName = `Test Site ${Date.now()}`.slice(0, 50);

        await siteNameInput.fill(newName);
        await page.click('[data-testid="btn-save-settings"]');
        await expect(page.locator('[data-testid="flash-success"]')).toBeVisible();

        await page.reload();
        const savedValue = await siteNameInput.inputValue();
        expect(savedValue).toBe(newName);

        // Restore original name
        await siteNameInput.fill(originalName || 'VK');
        await page.click('[data-testid="btn-save-settings"]');
        await expect(page.locator('[data-testid="flash-success"]')).toBeVisible();
    });

    test('changing raceIntervalMinutes persists after save', async ({ page }) => {
        await page.goto(`${BASE_URL}/wfbo/vk-settings`);

        const intervalInput = page.locator('input[id*="raceIntervalMinutes"], input[name*="raceIntervalMinutes"]').first();
        const original = await intervalInput.inputValue();

        await intervalInput.fill('10');
        await page.click('[data-testid="btn-save-vk-settings"]');
        await expect(page.locator('[data-testid="flash-success"]')).toBeVisible();

        await page.reload();
        const saved = await intervalInput.inputValue();
        expect(parseInt(saved)).toBe(10);

        // Restore
        await intervalInput.fill(original || '5');
        await page.click('[data-testid="btn-save-vk-settings"]');
        await expect(page.locator('[data-testid="flash-success"]')).toBeVisible();
    });

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

    test('WFBO sidebar shows settings nav link for admin', async ({ page }) => {
        await page.goto(`${BASE_URL}/wfbo`);
        await expect(page.locator('[data-testid="wfbo-sidebar"]')).toBeVisible();
    });

    test('max daily withdrawal amount field is visible', async ({ page }) => {
        await page.goto(`${BASE_URL}/wfbo/settings`);
        await expect(page.locator('[data-testid="input-max-daily-withdrawal-amount"]')).toBeVisible();
    });

    test('max daily withdrawal amount persists after save', async ({ page }) => {
        await page.goto(`${BASE_URL}/wfbo/settings`);

        const input = page.locator('[data-testid="input-max-daily-withdrawal-amount"]');
        const original = await input.inputValue();

        await input.fill('1000');
        await page.click('[data-testid="btn-save-settings"]');
        await expect(page.locator('[data-testid="flash-success"]')).toBeVisible();

        await page.reload();
        const saved = await input.inputValue();
        expect(parseFloat(saved)).toBeCloseTo(1000, 0);

        // Restore
        await input.fill(original || '');
        await page.click('[data-testid="btn-save-settings"]');
        await expect(page.locator('[data-testid="flash-success"]')).toBeVisible();
    });

    test('clearing max daily withdrawal amount (no limit) saves successfully', async ({ page }) => {
        await page.goto(`${BASE_URL}/wfbo/settings`);

        const input = page.locator('[data-testid="input-max-daily-withdrawal-amount"]');
        const original = await input.inputValue();

        await input.fill('');
        await page.click('[data-testid="btn-save-settings"]');
        await expect(page.locator('[data-testid="flash-success"]')).toBeVisible();

        await page.reload();
        expect(await input.inputValue()).toBe('');

        // Restore if there was a value
        if (original) {
            await input.fill(original);
            await page.click('[data-testid="btn-save-settings"]');
            await expect(page.locator('[data-testid="flash-success"]')).toBeVisible();
        }
    });

    test('player restrictions card is visible', async ({ page }) => {
        await page.goto(`${BASE_URL}/wfbo/settings`);
        await expect(page.locator('[data-testid="input-prevent-master-agent-playing-wrapper"]')).toBeVisible();
        await expect(page.locator('[data-testid="input-prevent-agent-playing-wrapper"]')).toBeVisible();
        await expect(page.locator('[data-testid="input-prevent-admin-playing-wrapper"]')).toBeVisible();
    });

    test('restrict agent playing checkbox persists after save', async ({ page }) => {
        await page.goto(`${BASE_URL}/wfbo/settings`);

        const checkbox = page.locator('[data-testid="input-prevent-agent-playing"]');
        const wasChecked = await checkbox.isChecked();

        // Toggle to unchecked state first, then check it
        if (wasChecked) await checkbox.uncheck();
        await checkbox.check();
        await page.click('[data-testid="btn-save-settings"]');
        await expect(page.locator('[data-testid="flash-success"]')).toBeVisible();

        await page.reload();
        await expect(page.locator('[data-testid="input-prevent-agent-playing"]')).toBeChecked();

        // Restore original state
        if (!wasChecked) {
            await page.locator('[data-testid="input-prevent-agent-playing"]').uncheck();
            await page.click('[data-testid="btn-save-settings"]');
            await expect(page.locator('[data-testid="flash-success"]')).toBeVisible();
        }
    });

    test('restrict master agent playing checkbox persists after save', async ({ page }) => {
        await page.goto(`${BASE_URL}/wfbo/settings`);

        const checkbox = page.locator('[data-testid="input-prevent-master-agent-playing"]');
        const wasChecked = await checkbox.isChecked();

        // Toggle to unchecked state first, then check it
        if (wasChecked) await checkbox.uncheck();
        await checkbox.check();
        await page.click('[data-testid="btn-save-settings"]');
        await expect(page.locator('[data-testid="flash-success"]')).toBeVisible();

        await page.reload();
        await expect(page.locator('[data-testid="input-prevent-master-agent-playing"]')).toBeChecked();

        // Restore original state
        if (!wasChecked) {
            await page.locator('[data-testid="input-prevent-master-agent-playing"]').uncheck();
            await page.click('[data-testid="btn-save-settings"]');
            await expect(page.locator('[data-testid="flash-success"]')).toBeVisible();
        }
    });

    test('restrict admin playing checkbox persists after save', async ({ page }) => {
        await page.goto(`${BASE_URL}/wfbo/settings`);

        const checkbox = page.locator('[data-testid="input-prevent-admin-playing"]');
        const wasChecked = await checkbox.isChecked();

        if (wasChecked) await checkbox.uncheck();
        await checkbox.check();
        await page.click('[data-testid="btn-save-settings"]');
        await expect(page.locator('[data-testid="flash-success"]')).toBeVisible();

        await page.reload();
        await expect(page.locator('[data-testid="input-prevent-admin-playing"]')).toBeChecked();

        // Restore original state
        if (!wasChecked) {
            await page.locator('[data-testid="input-prevent-admin-playing"]').uncheck();
            await page.click('[data-testid="btn-save-settings"]');
            await expect(page.locator('[data-testid="flash-success"]')).toBeVisible();
        }
    });

});
