import { test, expect } from '@playwright/test';

/**
 * E2E tests for user profile management:
 *   - Update name/email via PUT /auth/me
 *   - Delete (deactivate) account via DELETE /auth/me
 */

test.describe('User Profile Management', () => {
  const email = `test-${Date.now()}@example.com`;
  const password = 'TestPass123!';

  test.beforeAll(async ({ page }) => {
    // Register a new user first
    await page.goto('/register');
    await page.fill('input[placeholder="Full name"]', 'John Doe');
    await page.fill('input[placeholder="name@example.com"]', email);
    await page.fill('input[placeholder="Password"]', password);
    await page.click('button:has-text("Sign up")');
    await expect(page).toHaveURL('/')
  });

  test('update profile information', async ({ page }) => {
    // Open settings profile page
    await page.goto('/settings/profile');
    await page.fill('input[name="first_name"]', 'Jane');
    await page.fill('input[name="last_name"]', 'Smith');
    await page.fill('input[name="email"]', `jane.${Date.now()}@example.com`);
    await page.click('button:has-text("Save Changes")');
    await expect(page.locator('p')).toContainText('Jane Smith');
  });

  test('delete account (soft‑delete)', async ({ page }) => {
    await page.goto('/settings/account');
    await page.click('button:has-text("Delete Account")');
    // Confirm modal (Playwright handles native confirm)
    page.on('dialog', dialog => dialog.accept());
    await expect(page).toHaveURL('/login');
    // Verify login fails after deletion
    await page.fill('input[placeholder="name@example.com"]', email);
    await page.fill('input[placeholder="Password"]', password);
    await page.click('button:has-text("Sign in")');
    await expect(page.locator('div[role="alert"]')).toContainText('Invalid email or password');
  });
});
