import { expect, test } from '@playwright/test';
import { getAdminToken, installAdminToken } from './helpers/admin-auth';

const username = process.env.E2E_ADMIN_USERNAME ?? 'admin';
const currentPassword = process.env.E2E_ADMIN_CURRENT_PASSWORD;
const newPassword = process.env.E2E_ADMIN_NEW_PASSWORD;
const baseUrl = (process.env.E2E_BASE_URL ?? 'https://www.taxbaik.com').replace(/\/$/, '');

test.describe('admin password change', () => {
  test('changes password through the real admin UI', async ({ page, request }) => {
    test.skip(!currentPassword || !newPassword, 'E2E_ADMIN_CURRENT_PASSWORD and E2E_ADMIN_NEW_PASSWORD are required.');

    const token = await getAdminToken(request, baseUrl, username, currentPassword);
    await installAdminToken(page, token);
    await page.goto(`${baseUrl}/admin/settings`);
    await expect(page.getByRole('heading', { name: /사이트 설정|설정/ })).toBeVisible();

    await page.getByRole('textbox', { name: '현재 비밀번호' }).fill(currentPassword);
    await page.getByRole('textbox', { name: '새 비밀번호' }).fill(newPassword);
    await page.getByRole('textbox', { name: '새 비밀번호 확인' }).fill(newPassword);
    await page.getByRole('button', { name: '비밀번호 변경' }).click();

    await expect(page.getByText(/비밀번호가 변경되었습니다|비밀번호 변경/)).toBeVisible({ timeout: 20_000 });
  });
});
