// #region SettingsE2E [C:3] [TYPE Test] [SEMANTICS e2e, settings, environments, llm, git]
// @BRIEF E2E tests for Settings page — environments, LLM providers, Git config.
// @RELATION BINDS_TO -> [SettingsPage]
// @UX_STATE TabNavigated -> Active tab content is displayed.
// @UX_STATE EnvironmentAdded -> New Superset env appears in list.

import { test, expect } from '../fixtures/auth.fixture.js';
import { apiPost, apiDelete } from '../helpers/api.helper.js';

const FRONTEND_URL = process.env.FRONTEND_URL || 'http://127.0.0.1:8102';

test.describe('Settings — Environments', () => {
	test.beforeEach(async ({ authPage }) => {
		await authPage.goto(`${FRONTEND_URL}/settings#/settings/environments`);
		await authPage.waitForSelector('text=Окружения', { timeout: 10_000 });
	});

	test('should display environments tab with list', async ({ authPage }) => {
		// Tab button is active
		await expect(authPage.getByRole('button', { name: 'Окружения' })).toBeVisible();
		// At least one environment column header visible
		await expect(authPage.getByText(/НАЗВАНИЕ ПОДКЛЮЧЕНИЯ|URL|ИМЯ ПОЛЬЗОВАТЕЛЯ/)).toBeVisible();
	});

	test('should show add environment form', async ({ authPage }) => {
		await authPage.getByRole('button', { name: /Добавить окружение|Add/ }).click();
		await expect(authPage.getByText('ID')).toBeVisible();
		await expect(authPage.getByRole('button', { name: /Отмена|Cancel/ })).toBeVisible();
		await expect(authPage.getByRole('button', { name: /Добавить окружение|Save/ })).toBeVisible();
	});

	test('should add and delete environment via UI', async ({ authPage }) => {
		const envName = `e2e-test-${Date.now()}`;

		// Open form
		await authPage.getByRole('button', { name: /Добавить окружение|Add/ }).click();

		// Fill form (fields order depends on form layout)
		await authPage.getByLabel('ID').fill(envName);
		await authPage.getByLabel(/Название подключения|Name/).fill(envName);
		await authPage.getByLabel('URL').fill('https://e2e-test.example.com');
		await authPage.getByLabel(/Имя пользователя|Username/).fill('admin');
		await authPage.getByLabel(/Пароль|Password/).fill('test-pass');
		await authPage.getByRole('button', { name: /Добавить окружение|Save/ }).click();

		// Should appear in list
		await expect(authPage.getByText(envName)).toBeVisible({ timeout: 10_000 });
	});

	test('should navigate between all settings tabs', async ({ authPage }) => {
		const tabs = ['Окружения', 'Настройка логирования', 'Подключения', 'Интеграция Git', 'LLM',
			'Синхронизация миграции', 'Хранилище', 'Разделы проекта', 'Automation'];

		for (const tab of tabs) {
			await authPage.getByRole('button', { name: tab }).click();
			await expect(authPage.getByRole('button', { name: tab })).toHaveClass(/text-blue-600|border-blue-600/);
		}
	});
});

test.describe('Settings — LLM Provider', () => {
	test('should show LLM tab with provider list', async ({ authPage }) => {
		await authPage.goto(`${FRONTEND_URL}/settings#/settings/llm`);
		await authPage.waitForSelector('text=LLM', { timeout: 10_000 });
		// LLM provider config section visible
		await expect(authPage.getByText(/LLM/).first()).toBeVisible();
	});
});

test.describe('Settings — Git Integration', () => {
	test('should show Git tab with config list', async ({ authPage }) => {
		await authPage.goto(`${FRONTEND_URL}/settings#/settings/git`);
		await authPage.waitForSelector('text=Интеграция Git', { timeout: 10_000 });
		// Configured servers section
		await expect(authPage.getByText(/Настроенные серверы/)).toBeVisible();
	});
});
// #endregion SettingsE2E
