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

/**
 * Settings Page Tests
 *
 * Tests the Settings page functionality including:
 * - Navigation between settings sections
 * - General settings display and form fields
 * - Branding settings with logo upload
 * - Company settings form fields
 * - Employee settings
 * - Maps settings
 * - Mail settings
 */

// Helper function to login as admin
async function loginAsAdmin(page) {
  await page.goto('/auth/login', { waitUntil: 'networkidle' });
  await page.locator('#email').fill('admin@demo.com');
  await page.locator('#password').fill('password123');
  await page.locator('button[type="submit"]').click();
  await page.waitForURL('**/dashboard', { timeout: 15000 });
}

test.describe('Settings Page', () => {
  test.beforeEach(async ({ page }) => {
    await loginAsAdmin(page);
  });

  test('should display settings page with navigation', async ({ page }) => {
    // Navigate to settings page
    await page.goto('/settings', { waitUntil: 'networkidle' });

    // Verify navigation sidebar is visible
    await expect(page.locator('#settingsNav')).toBeVisible();

    // Verify navigation items exist
    await expect(page.locator('#settingsNav a[data-section="general"]')).toBeVisible();
    await expect(page.locator('#settingsNav a[data-section="branding"]')).toBeVisible();
    await expect(page.locator('#settingsNav a[data-section="company"]')).toBeVisible();
    await expect(page.locator('#settingsNav a[data-section="employee"]')).toBeVisible();
    await expect(page.locator('#settingsNav a[data-section="maps"]')).toBeVisible();
    await expect(page.locator('#settingsNav a[data-section="mail"]')).toBeVisible();
  });

  test('should display general settings section by default', async ({ page }) => {
    await page.goto('/settings', { waitUntil: 'networkidle' });

    // Verify general settings section is visible by default
    await expect(page.locator('#section-general')).toBeVisible();
    await expect(page.locator('#form-general')).toBeVisible();

    // Verify general form fields
    await expect(page.locator('#appName')).toBeVisible();
    await expect(page.locator('#country')).toBeVisible();
    await expect(page.locator('#currency')).toBeVisible();
    await expect(page.locator('#currencySymbol')).toBeVisible();
  });

  test('should navigate to branding settings section', async ({ page }) => {
    await page.goto('/settings', { waitUntil: 'networkidle' });

    // Click branding navigation item
    await page.click('#settingsNav a[data-section="branding"]');
    await page.waitForTimeout(500);

    // Verify branding section is visible
    await expect(page.locator('#section-branding')).toBeVisible();
    await expect(page.locator('#form-branding')).toBeVisible();

    // Verify branding form elements
    await expect(page.locator('#appLogo')).toBeAttached(); // Hidden file input
    await expect(page.locator('#appFavicon')).toBeAttached(); // Hidden file input
  });

  test('should navigate to company settings section', async ({ page }) => {
    await page.goto('/settings', { waitUntil: 'networkidle' });

    // Click company navigation item
    await page.click('#settingsNav a[data-section="company"]');
    await page.waitForTimeout(500);

    // Verify company section is visible
    await expect(page.locator('#section-company')).toBeVisible();
    await expect(page.locator('#form-company')).toBeVisible();

    // Verify company form fields
    await expect(page.locator('#companyName')).toBeVisible();
    await expect(page.locator('#companyPhone')).toBeVisible();
    await expect(page.locator('#companyEmail')).toBeVisible();
    await expect(page.locator('#companyAddress')).toBeVisible();
  });

  test('should navigate to employee settings section', async ({ page }) => {
    await page.goto('/settings', { waitUntil: 'networkidle' });

    // Click employee navigation item
    await page.click('#settingsNav a[data-section="employee"]');
    await page.waitForTimeout(500);

    // Verify employee section is visible
    await expect(page.locator('#section-employee')).toBeVisible();
    await expect(page.locator('#form-employee')).toBeVisible();

    // Verify employee form fields
    await expect(page.locator('#defaultPassword')).toBeVisible();
  });

  test('should navigate to maps settings section', async ({ page }) => {
    await page.goto('/settings', { waitUntil: 'networkidle' });

    // Click maps navigation item
    await page.click('#settingsNav a[data-section="maps"]');
    await page.waitForTimeout(500);

    // Verify maps section is visible
    await expect(page.locator('#section-maps')).toBeVisible();
    await expect(page.locator('#form-maps')).toBeVisible();

    // Verify maps form fields
    await expect(page.locator('#mapProvider')).toBeVisible();
    await expect(page.locator('#mapZoomLevel')).toBeVisible();
    await expect(page.locator('#centerLatitude')).toBeVisible();
    await expect(page.locator('#centerLongitude')).toBeVisible();
    await expect(page.locator('#mapApiKey')).toBeVisible();
  });

  test('should navigate to mail settings section', async ({ page }) => {
    await page.goto('/settings', { waitUntil: 'networkidle' });

    // Click mail navigation item
    await page.click('#settingsNav a[data-section="mail"]');
    await page.waitForTimeout(500);

    // Verify mail section is visible
    await expect(page.locator('#section-mail')).toBeVisible();
    await expect(page.locator('#form-mail')).toBeVisible();

    // Verify mail form fields
    await expect(page.locator('#mail_host')).toBeVisible();
    await expect(page.locator('#mail_port')).toBeVisible();
    await expect(page.locator('#mail_username')).toBeVisible();
    await expect(page.locator('#mail_from_address')).toBeVisible();
  });

  test('should highlight active navigation item', async ({ page }) => {
    await page.goto('/settings', { waitUntil: 'networkidle' });

    // General should be active by default
    await expect(page.locator('#settingsNav a[data-section="general"]')).toHaveClass(/active/);

    // Click company settings
    await page.click('#settingsNav a[data-section="company"]');
    await page.waitForTimeout(500);

    // Company should now be active
    await expect(page.locator('#settingsNav a[data-section="company"]')).toHaveClass(/active/);

    // General should no longer be active
    await expect(page.locator('#settingsNav a[data-section="general"]')).not.toHaveClass(/active/);
  });

  test('should have save button in each settings section', async ({ page }) => {
    await page.goto('/settings', { waitUntil: 'networkidle' });

    // General section - check save button
    await expect(page.locator('#section-general button[type="submit"]')).toBeVisible();

    // Navigate to company section and check save button
    await page.click('#settingsNav a[data-section="company"]');
    await page.waitForTimeout(500);
    await expect(page.locator('#section-company button[type="submit"]')).toBeVisible();

    // Navigate to mail section and check save button
    await page.click('#settingsNav a[data-section="mail"]');
    await page.waitForTimeout(500);
    await expect(page.locator('#section-mail button[type="submit"]')).toBeVisible();
  });

  test('should navigate to code prefix settings section', async ({ page }) => {
    await page.goto('/settings', { waitUntil: 'networkidle' });

    // Click code-prefix navigation item
    await page.click('#settingsNav a[data-section="code-prefix"]');
    await page.waitForTimeout(500);

    // Verify code-prefix section is visible
    await expect(page.locator('#section-code-prefix')).toBeVisible();
    await expect(page.locator('#form-code-prefix')).toBeVisible();

    // Verify code prefix form fields
    await expect(page.locator('#employeeCodePrefix')).toBeVisible();
  });

  test('should have test email button in mail settings', async ({ page }) => {
    await page.goto('/settings', { waitUntil: 'networkidle' });

    // Navigate to mail section
    await page.click('#settingsNav a[data-section="mail"]');
    await page.waitForTimeout(500);

    // Verify test email input and button exist
    await expect(page.locator('#test_email')).toBeVisible();
    await expect(page.locator('#sendTestEmailBtn')).toBeVisible();
  });
});
