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

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

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

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

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

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

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

    test('updating house win rate and a tier field saves successfully', async ({ page }) => {
        await page.goto(`${BASE_URL}/wfbo/mines/settings`);
        await page.fill('[data-testid="input-mg-house-win-rate"]', '10');
        await page.fill('[data-testid="input-mg-green-mines"]', '4');
        await page.click('[data-testid="btn-save-mg-settings"]');
        await expect(page.locator('[data-testid="flash-success"]')).toBeVisible();
        await expect(page.locator('[data-testid="input-mg-house-win-rate"]')).toHaveValue('10');
        await expect(page.locator('[data-testid="input-mg-green-mines"]')).toHaveValue('4');
    });

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

});
