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

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

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

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

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

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

    test('draw time input has default value 20:00', async ({ page }) => {
        await page.goto(`${BASE_URL}/wfbo/ezdos/settings`);
        await expect(page.locator('[data-testid="input-ez-draw-time"]')).toHaveValue('20:00');
    });

    test('straight payout input has default value 5000', async ({ page }) => {
        await page.goto(`${BASE_URL}/wfbo/ezdos/settings`);
        await expect(page.locator('[data-testid="input-ez-straight-payout"]')).toHaveValue('5000.00');
    });

    test('rambolito payout input has default value 3000', async ({ page }) => {
        await page.goto(`${BASE_URL}/wfbo/ezdos/settings`);
        await expect(page.locator('[data-testid="input-ez-rambolito-payout"]')).toHaveValue('3000.00');
    });

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

    test('updating draw time and payout saves successfully', async ({ page }) => {
        await page.goto(`${BASE_URL}/wfbo/ezdos/settings`);
        await page.fill('[data-testid="input-ez-draw-time"]', '21:30');
        await page.fill('[data-testid="input-ez-straight-payout"]', '6000');
        await page.fill('[data-testid="input-ez-rambolito-payout"]', '3500');
        await page.click('[data-testid="btn-save-ez-settings"]');
        await expect(page.locator('[data-testid="flash-success"]')).toBeVisible();
        await expect(page.locator('[data-testid="input-ez-draw-time"]')).toHaveValue('21:30');
        await expect(page.locator('[data-testid="input-ez-straight-payout"]')).toHaveValue('6000.00');
        await expect(page.locator('[data-testid="input-ez-rambolito-payout"]')).toHaveValue('3500.00');
    });

    test('updating bet closing time saves successfully', async ({ page }) => {
        await page.goto(`${BASE_URL}/wfbo/ezdos/settings`);
        await page.fill('[data-testid="input-ez-bet-closing-time"]', '19:45');
        await page.click('[data-testid="btn-save-ez-settings"]');
        await expect(page.locator('[data-testid="flash-success"]')).toBeVisible();
        await expect(page.locator('[data-testid="input-ez-bet-closing-time"]')).toHaveValue('19:45');
    });

    test('toggling ez-enabled and saving persists the change', async ({ page }) => {
        await page.goto(`${BASE_URL}/wfbo/ezdos/settings`);
        const toggle = page.locator('[data-testid="toggle-ez-enabled"]');
        const wasChecked = await toggle.isChecked();
        await toggle.click();
        await page.click('[data-testid="btn-save-ez-settings"]');
        await expect(page.locator('[data-testid="flash-success"]')).toBeVisible();
        await expect(toggle).toBeChecked({ checked: !wasChecked });
    });

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

});
