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

test.describe('WFBO Agent & Role Management', () => {

    test.beforeEach(async ({ page }) => {
        await loginAs(page, ADMIN.mobile, ADMIN.password);
        await page.goto(`${BASE_URL}/wfbo/users`);
    });

    test('admin can promote a user to ROLE_FINANCE', async ({ page }) => {
        const promoteForm = page.locator('[data-testid^="form-promote-finance-"]').first();
        if (await promoteForm.count() === 0) {
            test.skip();
            return;
        }
        const formTestId = await promoteForm.getAttribute('data-testid');
        const userId = formTestId?.replace('form-promote-finance-', '');
        await page.locator(`[data-testid="btn-actions-${userId}"]`).click();
        await page.locator(`[data-testid="btn-promote-finance-${userId}"]`).click();
        await page.locator('[data-testid="btn-confirm-action"]').click();
        await expect(page.locator('[data-testid="flash-success"]')).toBeVisible();
    });

    test('admin can demote a ROLE_FINANCE user', async ({ page }) => {
        const demoteForm = page.locator('[data-testid^="form-demote-finance-"]').first();
        if (await demoteForm.count() === 0) {
            test.skip();
            return;
        }
        const formTestId = await demoteForm.getAttribute('data-testid');
        const userId = formTestId?.replace('form-demote-finance-', '');
        await page.locator(`[data-testid="btn-actions-${userId}"]`).click();
        await page.locator(`[data-testid="btn-demote-finance-${userId}"]`).click();
        await expect(page.locator('[data-testid="flash-success"]')).toBeVisible();
    });

    test('admin can promote a user to ROLE_OPERATIONS', async ({ page }) => {
        const promoteForm = page.locator('[data-testid^="form-promote-operations-"]').first();
        if (await promoteForm.count() === 0) {
            test.skip();
            return;
        }
        const formTestId = await promoteForm.getAttribute('data-testid');
        const userId = formTestId?.replace('form-promote-operations-', '');
        await page.locator(`[data-testid="btn-actions-${userId}"]`).click();
        await page.locator(`[data-testid="btn-promote-operations-${userId}"]`).click();
        await page.locator('[data-testid="btn-confirm-action"]').click();
        await expect(page.locator('[data-testid="flash-success"]')).toBeVisible();
    });

    test('admin can demote a ROLE_OPERATIONS user', async ({ page }) => {
        const demoteForm = page.locator('[data-testid^="form-demote-operations-"]').first();
        if (await demoteForm.count() === 0) {
            test.skip();
            return;
        }
        const formTestId = await demoteForm.getAttribute('data-testid');
        const userId = formTestId?.replace('form-demote-operations-', '');
        await page.locator(`[data-testid="btn-actions-${userId}"]`).click();
        await page.locator(`[data-testid="btn-demote-operations-${userId}"]`).click();
        await expect(page.locator('[data-testid="flash-success"]')).toBeVisible();
    });

    test('admin can promote a user to ROLE_ADMIN', async ({ page }) => {
        // Only promote a non-admin user (never the fixture ADMIN)
        const promoteForm = page.locator('[data-testid^="form-promote-admin-"]').first();
        if (await promoteForm.count() === 0) {
            test.skip();
            return;
        }
        const formTestId = await promoteForm.getAttribute('data-testid');
        const userId = formTestId?.replace('form-promote-admin-', '');
        await page.locator(`[data-testid="btn-actions-${userId}"]`).click();
        await page.locator(`[data-testid="btn-promote-admin-${userId}"]`).click();
        await page.locator('[data-testid="btn-confirm-action"]').click();
        await expect(page.locator('[data-testid="flash-success"]')).toBeVisible();
    });


});

test.describe('Agent Commission — Agent Portal Side', () => {

    test('master agent can promote a referred user to agent via modal', async ({ page }) => {
        await loginAs(page, AGENT.mobile, AGENT.password);
        await page.goto(`${BASE_URL}/agent/users`);

        // Only ROLE_MASTER_AGENT sees the promote-agent button (opens a modal)
        const promoteBtn = page.locator('[data-testid^="btn-promote-agent-"]').first();
        if (await promoteBtn.count() === 0) {
            test.skip();
            return;
        }
        await promoteBtn.click();

        // Fill in commission rate in the modal and confirm
        const modal = page.locator('[data-testid="promote-agent-modal"]');
        await expect(modal).toHaveCSS('display', 'flex');
        await page.fill('[data-testid="input-promote-commission-rate"]', '30');
        await page.click('[data-testid="btn-confirm-promote"]');
        await expect(page.locator('[data-testid="flash-success"]')).toBeVisible();
    });

});
