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

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

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

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

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

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

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

    test('round interval accepts 0.25 minute (15 seconds) and must be longer than the bet closing time', async ({ page }, testInfo) => {
        await page.goto(`${BASE_URL}/wfbo/pataasan/settings`);
        const interval = page.locator('[data-testid="input-hl-interval-minutes"]');
        const closing  = page.locator('[data-testid="input-hl-bet-closing-seconds"]');
        const before   = { interval: await interval.inputValue(), closing: await closing.inputValue() };

        // Closing time as long as the round: betting would never open.
        await interval.fill('0.25');
        await closing.fill('15');
        await page.click('[data-testid="btn-save-hl-settings"]');
        await expect(page.locator('[data-testid="hl-settings-form"]')).toContainText('Bet closing time must be shorter than the round interval (15 seconds).');

        // Below the 0.1-minute floor.
        await page.locator('[data-testid="input-hl-interval-minutes"]').fill('0.05');
        await page.locator('[data-testid="input-hl-bet-closing-seconds"]').fill('3');
        await page.locator('[data-testid="input-hl-interval-minutes"]').evaluate((el) => el.removeAttribute('min'));   // bypass the browser check
        await page.click('[data-testid="btn-save-hl-settings"]');
        await expect(page.locator('[data-testid="hl-settings-form"]')).toContainText('Must be at least 0.1 minute (6 seconds)');

        // 0.25 with a 3-second close saves and is kept exactly (not rounded to 0.3).
        await page.locator('[data-testid="input-hl-interval-minutes"]').fill('0.25');
        await page.locator('[data-testid="input-hl-bet-closing-seconds"]').fill('3');
        await page.click('[data-testid="btn-save-hl-settings"]');
        await expect(page.locator('[data-testid="flash-success"]').first()).toBeVisible();
        await page.goto(`${BASE_URL}/wfbo/pataasan/settings`);
        await expect(page.locator('[data-testid="input-hl-interval-minutes"]')).toHaveValue('0.25');
        await page.screenshot({ path: testInfo.outputPath('pataasan-interval.png'), fullPage: true });

        // Put the original values back for other specs.
        await page.locator('[data-testid="input-hl-interval-minutes"]').fill(before.interval);
        await page.locator('[data-testid="input-hl-bet-closing-seconds"]').fill(before.closing);
        await page.click('[data-testid="btn-save-hl-settings"]');
        await expect(page.locator('[data-testid="flash-success"]').first()).toBeVisible();
    });

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

});

test.describe('WFBO — Pataasan Rank Payouts', () => {

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

    test('hl-rank-payouts page loads', async ({ page }) => {
        await page.goto(`${BASE_URL}/wfbo/pataasan/rank-payouts`);
        await expect(page).toHaveURL(`${BASE_URL}/wfbo/pataasan/rank-payouts`);
    });

    test('rank payouts save button is present', async ({ page }) => {
        await page.goto(`${BASE_URL}/wfbo/pataasan/rank-payouts`);
        await expect(page.locator('button[type="submit"]')).toBeVisible();
    });

    test('submitting rank payouts with unchanged values shows flash-success', async ({ page }) => {
        await page.goto(`${BASE_URL}/wfbo/pataasan/rank-payouts`);
        await page.click('button[type="submit"]');
        await expect(page.locator('[data-testid="flash-success"]')).toBeVisible();
    });

    test('FINANCE user is blocked from hl-rank-payouts (ROLE_ADMIN required)', async ({ page }) => {
        await loginAs(page, FINANCE.mobile, FINANCE.password);
        await page.goto(`${BASE_URL}/wfbo/pataasan/rank-payouts`);
        // 403 — no submit button accessible
        await expect(page.locator('[data-testid="wfbo-sidebar"]')).not.toBeVisible();
    });

});
