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

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

    test('super admin creates, edits and cancels a bonus; creator is recorded', async ({ page }) => {
        const player = await registerAndLogin(page);
        await loginAs(page, SUPER_ADMIN.mobile, SUPER_ADMIN.password);

        await page.goto(`${BASE_URL}/wfbo`);
        await page.locator('button[aria-controls="campaigns-group-body"]').click();
        await page.locator('#campaigns-group-body [data-testid="wfbo-nav-bonuses"]').click();
        await expect(page).toHaveURL(/\/wfbo\/bonuses/);

        const id = await createBonus(page, player.mobile, '150', 'Birthday bonus');
        const row = page.locator(`[data-testid="bonus-row-${id}"]`);
        await expect(page.locator('[data-testid="flash-success"]')).toContainText('₱150.00');
        await expect(row.locator(`[data-testid="bonus-status-${id}"]`)).toHaveText(/active/i);
        await expect(row).toContainText('Super');   // created-by column (fixture super admin)

        // Active filter lists it
        await page.click('[data-testid="bonus-filter-active"]');
        await expect(page.locator(`[data-testid="bonus-row-${id}"]`)).toBeVisible();

        // Edit the amount
        await page.click(`[data-testid="btn-edit-bonus-${id}"]`);
        await expect(page.locator('[data-testid="input-bonus-mobile"]')).toHaveValue(player.mobile);
        await page.fill('[data-testid="input-bonus-amount"]', '175.50');
        await page.click('[data-testid="btn-submit-bonus"]');
        await expect(page.locator(`[data-testid="bonus-amount-${id}"]`)).toHaveText('₱175.50');
        await expect(page.locator(`[data-testid="bonus-row-${id}"]`)).toContainText('edited by');

        // Cancel it (native confirm)
        page.once('dialog', (d) => d.accept());
        await page.click(`[data-testid="btn-cancel-bonus-${id}"]`);
        await expect(page.locator(`[data-testid="bonus-status-${id}"]`)).toHaveText(/cancelled/i);
        await expect(page.locator(`[data-testid="btn-edit-bonus-${id}"]`)).toHaveCount(0);
    });

    test('invalid form shows validation errors', async ({ page }) => {
        await loginAs(page, SUPER_ADMIN.mobile, SUPER_ADMIN.password);
        await page.goto(`${BASE_URL}/wfbo/bonuses/new`);
        await page.fill('[data-testid="input-bonus-mobile"]', '+19999999999');
        await page.fill('[data-testid="input-bonus-amount"]', '0');
        await page.click('[data-testid="btn-submit-bonus"]');

        // Each validation error is its own flash toast.
        const errors = page.locator('[data-testid="flash-error"]');
        for (const message of [
            'No user found with mobile number +19999999999',
            'Amount must be greater than zero',
            'Claim period start date & time is required',
            'Bonus image is required',
        ]) {
            await expect(errors.filter({ hasText: message })).toHaveCount(1);
        }
        await expect(page.locator('[data-testid="input-bonus-mobile"]')).toHaveValue('+19999999999');
    });

    test('a regular admin cannot see or open bonuses', async ({ page }) => {
        await loginAs(page, ADMIN.mobile, ADMIN.password);
        await page.goto(`${BASE_URL}/wfbo`);
        await expect(page.locator('[data-testid="wfbo-nav-bonuses"]')).toHaveCount(0);

        const response = await page.goto(`${BASE_URL}/wfbo/bonuses`);
        expect(response?.status()).toBe(403);
    });

});
