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

test.describe('Change Password', () => {

    test('change password page loads with form fields', async ({ page }) => {
        await loginAs(page, USER.mobile, USER.password);
        await page.goto(`${BASE_URL}/profile/change-password`);

        await expect(page).toHaveTitle(/Change Password/i);
        await expect(page.locator('[data-testid="change-password-form"]')).toBeVisible();
        await expect(page.locator('[data-testid="input-current-password"]')).toBeVisible();
        await expect(page.locator('[data-testid="input-new-password"]')).toBeVisible();
        await expect(page.locator('[data-testid="input-new-password-confirm"]')).toBeVisible();
    });

    test('navbar shows change password link', async ({ page }) => {
        await loginAs(page, USER.mobile, USER.password);
        await page.click('[data-testid="navbar-user-menu"]');
        await expect(page.locator('[data-testid="nav-link-change-password"]')).toBeVisible();
    });

    test('wrong current password shows error flash', async ({ page }) => {
        await loginAs(page, USER.mobile, USER.password);
        await page.goto(`${BASE_URL}/profile/change-password`);

        await page.fill('[data-testid="input-current-password"]',    'WrongPassword!');
        await page.fill('[data-testid="input-new-password"]',        'NewPassword2!');
        await page.fill('[data-testid="input-new-password-confirm"]', 'NewPassword2!');
        await page.click('[data-testid="btn-change-password"]');

        await expect(page.locator('[data-testid="flash-error"]')).toContainText('Current password is incorrect');
    });

    test('mismatched new passwords shows validation error', async ({ page }) => {
        await loginAs(page, USER.mobile, USER.password);
        await page.goto(`${BASE_URL}/profile/change-password`);

        await page.fill('[data-testid="input-current-password"]',    USER.password);
        await page.fill('[data-testid="input-new-password"]',        'NewPassword2!');
        await page.fill('[data-testid="input-new-password-confirm"]', 'Different3!');
        await page.click('[data-testid="btn-change-password"]');

        await expect(page).toHaveURL(`${BASE_URL}/profile/change-password`);
        await expect(page.locator('body')).toContainText('password fields must match');
    });

    test('successful password change shows success flash', async ({ page }) => {
        await loginAs(page, USER.mobile, USER.password);
        await page.goto(`${BASE_URL}/profile/change-password`);

        await page.fill('[data-testid="input-current-password"]',    USER.password);
        await page.fill('[data-testid="input-new-password"]',        'Changed1!New');
        await page.fill('[data-testid="input-new-password-confirm"]', 'Changed1!New');
        await page.click('[data-testid="btn-change-password"]');

        await expect(page.locator('[data-testid="flash-success"]')).toContainText('Password changed successfully');

        // Restore the fixture password so subsequent tests are not affected
        await page.fill('[data-testid="input-current-password"]',    'Changed1!New');
        await page.fill('[data-testid="input-new-password"]',        USER.password);
        await page.fill('[data-testid="input-new-password-confirm"]', USER.password);
        await page.click('[data-testid="btn-change-password"]');
        await page.waitForURL(`${BASE_URL}/profile/change-password`);
    });

    test('back to dashboard link returns to home', async ({ page }) => {
        await loginAs(page, USER.mobile, USER.password);
        await page.goto(`${BASE_URL}/profile/change-password`);
        await page.click('[data-testid="link-back-home"]');
        await expect(page).toHaveURL(`${BASE_URL}/`);
    });

});
