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

test.describe('WFBO Admin Reset Password', () => {

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

    test('Reset Password button is visible in actions dropdown', async ({ page }) => {
        const firstActionBtn = page.locator('[data-testid^="btn-actions-"]').first();
        const testId = await firstActionBtn.getAttribute('data-testid');
        const uid = testId?.replace('btn-actions-', '');

        await firstActionBtn.click();
        await expect(page.locator(`[data-testid="btn-reset-password-${uid}"]`)).toBeVisible();
    });

    test('clicking Reset Password shows generated password modal', async ({ page }) => {
        const firstActionBtn = page.locator('[data-testid^="btn-actions-"]').first();
        const testId = await firstActionBtn.getAttribute('data-testid');
        const uid = testId?.replace('btn-actions-', '');

        await firstActionBtn.click();
        await page.locator(`[data-testid="btn-reset-password-${uid}"]`).click();
        await page.locator('[data-testid="btn-confirm-action"]').click();

        await expect(page.locator('[data-testid="reset-result-password"]')).toBeVisible();
        const pwd = await page.locator('[data-testid="reset-result-password"]').textContent();
        expect(pwd?.trim().length).toBeGreaterThanOrEqual(10);
    });

    test('modal does not reappear after closing', async ({ page }) => {
        const firstActionBtn = page.locator('[data-testid^="btn-actions-"]').first();
        const testId = await firstActionBtn.getAttribute('data-testid');
        const uid = testId?.replace('btn-actions-', '');

        await firstActionBtn.click();
        await page.locator(`[data-testid="btn-reset-password-${uid}"]`).click();
        await page.locator('[data-testid="btn-confirm-action"]').click();

        await expect(page.locator('[data-testid="reset-result-done"]')).toBeVisible();
        await page.locator('[data-testid="reset-result-done"]').click();

        // Reload the page — flash is consumed, modal should not reappear
        await page.reload();
        await expect(page.locator('#resetPasswordResultModal')).toHaveCount(0);
    });

});
