import { test, expect } from "#tests/e2e/fixtures/real-backend"

// These tests run against the real backend (localhost:8000).
// Each test gets a fresh user with real JWT tokens.
// Created projects are cleaned up automatically after each test.

test.describe("Create Project (Integration)", () => {
	test("should create project and see it in the list", async ({
		realProjectsPage,
	}) => {
		const { page, modal } = realProjectsPage

		// Empty state
		await expect(page.getByText("У вас пока нет проектов")).toBeVisible()

		await realProjectsPage.openCreateModal()
		await modal.locator("#project_name").fill("Интеграционный тест")
		await modal.locator("button", { hasText: "Создать" }).click()

		await expect(modal).toBeHidden()
		await expect(page.getByText("Интеграционный тест")).toBeVisible()
	})

	test("should create project with description", async ({
		realProjectsPage,
	}) => {
		const { page, modal } = realProjectsPage

		await realProjectsPage.openCreateModal()
		await modal.locator("#project_name").fill("Проект с описанием")
		await modal
			.locator("#project_description")
			.fill("Описание для интеграционного теста")
		await modal.locator("button", { hasText: "Создать" }).click()

		await expect(modal).toBeHidden()
		await expect(page.getByText("Проект с описанием")).toBeVisible()

		// Verify via API that description was saved
		const projects = await realProjectsPage.getProjects()
		const created = projects.find(
			(p: { name: string }) => p.name === "Проект с описанием",
		)
		expect(created).toBeTruthy()
	})

	test("should create project with Russian language", async ({
		realProjectsPage,
	}) => {
		const { page, modal } = realProjectsPage

		await realProjectsPage.openCreateModal()
		await modal.locator("#project_name").fill("Русский проект")

		await modal.locator("button").filter({ hasText: "Авто" }).click()
		await page.locator("[role=option]", { hasText: "Русский" }).click()

		await modal.locator("button", { hasText: "Создать" }).click()
		await expect(modal).toBeHidden()

		// Verify language via API
		const projects = await realProjectsPage.getProjects()
		const created = projects.find(
			(p: { name: string }) => p.name === "Русский проект",
		) as { language: string } | undefined
		expect(created?.language).toBe("ru")
	})

	test("should create multiple projects", async ({ realProjectsPage }) => {
		const { page, modal } = realProjectsPage

		// Create first project
		await realProjectsPage.openCreateModal()
		await modal.locator("#project_name").fill("Первый проект")
		await modal.locator("button", { hasText: "Создать" }).click()
		await expect(modal).toBeHidden()
		await expect(page.getByText("Первый проект")).toBeVisible()

		// Create second project
		await realProjectsPage.openCreateModal()
		await modal.locator("#project_name").fill("Второй проект")
		await modal.locator("button", { hasText: "Создать" }).click()
		await expect(modal).toBeHidden()
		await expect(page.getByText("Второй проект")).toBeVisible()

		// Both visible
		await expect(page.getByText("Первый проект")).toBeVisible()
		await expect(page.getByText("Второй проект")).toBeVisible()

		// Verify via API
		const projects = await realProjectsPage.getProjects()
		expect(projects.length).toBe(2)
	})

	test("should persist project after page reload", async ({
		realProjectsPage,
	}) => {
		const { page, modal } = realProjectsPage

		await realProjectsPage.openCreateModal()
		await modal.locator("#project_name").fill("Персистентный проект")
		await modal.locator("button", { hasText: "Создать" }).click()
		await expect(modal).toBeHidden()
		await expect(page.getByText("Персистентный проект")).toBeVisible()

		// Reload and verify project is still there
		await page.reload()
		await page.getByRole("heading", { name: "Мои проекты" }).waitFor()
		await expect(page.getByText("Персистентный проект")).toBeVisible()
	})

	test("should show validation error for empty name (client-side)", async ({
		realProjectsPage,
	}) => {
		const { modal } = realProjectsPage

		await realProjectsPage.openCreateModal()
		await modal.locator("button", { hasText: "Создать" }).click()

		await expect(
			modal.getByText("Введите название проекта"),
		).toBeVisible()
		await expect(modal).toBeVisible()
	})
})
