import { test, expect } from '@playwright/test';
import {
  loginAsAdmin,
  navigateToSettings,
  waitForPageStable,
  waitForSuccessSweetAlert
} from './helpers/accountingcore';

/**
 * AccountingCore Settings Tests
 *
 * Tests the AccountingCore Settings page functionality including:
 * - Displaying settings page with all sections
 * - Transaction settings (prefix, start number, future dates, attachments)
 * - Category settings (custom categories, hierarchy levels)
 * - Integration settings (auto-sync sales/purchase orders)
 * - Saving settings successfully
 */

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

  test('should display settings page', async ({ page }) => {
    await test.step('Navigate to settings page', async () => {
      await navigateToSettings(page);
    });

    await test.step('Verify page loaded', async () => {
      // Wait for page to fully load
      await page.waitForTimeout(1000);
      // Verify the page has content (card or form)
      const hasCard = await page.locator('.card').first().isVisible();
      const hasForm = await page.locator('form').first().isVisible();
      expect(hasCard || hasForm).toBeTruthy();
    });
  });

  test('should display allow future dates setting', async ({ page }) => {
    await test.step('Navigate to settings', async () => {
      await navigateToSettings(page);
      await waitForPageStable(page);
    });

    await test.step('Verify allow future dates switch exists', async () => {
      const futureDatesSwitch = page.locator('#allow_future_dates');
      await expect(futureDatesSwitch).toBeAttached();
    });
  });

  test('should display require attachments setting', async ({ page }) => {
    await test.step('Navigate to settings', async () => {
      await navigateToSettings(page);
      await waitForPageStable(page);
    });

    await test.step('Verify require attachments switch exists', async () => {
      const attachmentsSwitch = page.locator('#require_attachments');
      await expect(attachmentsSwitch).toBeAttached();
    });
  });

  test('should display allow custom categories setting', async ({ page }) => {
    await test.step('Navigate to settings', async () => {
      await navigateToSettings(page);
      await waitForPageStable(page);
    });

    await test.step('Verify allow custom categories switch exists', async () => {
      const customCategoriesSwitch = page.locator('#allow_custom_categories');
      await expect(customCategoriesSwitch).toBeAttached();
    });
  });

  test('should display category hierarchy levels setting', async ({ page }) => {
    await test.step('Navigate to settings', async () => {
      await navigateToSettings(page);
      await waitForPageStable(page);
    });

    await test.step('Verify category hierarchy levels field exists', async () => {
      const hierarchyField = page.locator('#category_hierarchy_levels');
      await expect(hierarchyField).toBeVisible();
    });
  });

  test('should have save button', async ({ page }) => {
    await test.step('Navigate to settings', async () => {
      await navigateToSettings(page);
      await waitForPageStable(page);
    });

    await test.step('Verify save button exists', async () => {
      const saveButton = page.locator('button[type="submit"]').first();
      await expect(saveButton).toBeVisible();
    });
  });

  test('should save settings successfully', async ({ page }) => {
    await test.step('Navigate to settings', async () => {
      await navigateToSettings(page);
      await waitForPageStable(page);
    });

    await test.step('Toggle a setting', async () => {
      const futureDatesSwitch = page.locator('#allow_future_dates');
      const initialState = await futureDatesSwitch.isChecked();

      if (initialState) {
        await futureDatesSwitch.uncheck();
      } else {
        await futureDatesSwitch.check();
      }
      await page.waitForTimeout(300);
    });

    await test.step('Click save button', async () => {
      const saveButton = page.locator('button[type="submit"]').first();
      await saveButton.click();
      await waitForSuccessSweetAlert(page);
    });
  });

  test('should display settings with current values', async ({ page }) => {
    await test.step('Navigate to settings', async () => {
      await navigateToSettings(page);
      await waitForPageStable(page);
    });

    await test.step('Verify form has values', async () => {
      const hierarchyLevels = await page.locator('#category_hierarchy_levels').inputValue();
      expect(hierarchyLevels).toBeTruthy();
    });
  });
});
