package de.avatic.lcc.e2e.tests;

import de.avatic.lcc.e2e.pages.AssistantPage;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

import java.util.logging.Logger;

import static org.junit.jupiter.api.Assertions.assertTrue;

/**
 * Smoke tests to verify basic application functionality.
 * These tests run quickly and verify that the application is accessible.
 *
 * <p>The backend with integrated frontend is started automatically via @SpringBootTest.
 *
 * <p>Run with: {@code mvn test -Dtest=SmokeE2ETest -Dspring.profiles.active=test,mysql}
 */
@Tag("smoke")
@DisplayName("Smoke E2E Tests")
class SmokeE2ETest extends AbstractE2ETest {

    private static final Logger logger = Logger.getLogger(SmokeE2ETest.class.getName());

    @Test
    @DisplayName("Application is accessible")
    void testApplicationIsAccessible() {
        page.navigate(getBaseUrl());
        String title = page.title();

        logger.info(() -> "Page title: " + title);
        assertTrue(title != null && !title.isEmpty(), "Page should have a title");
    }

    @Test
    @DisplayName("Login was successful")
    void testLoginWasSuccessful() {
        // Login happens in @BeforeEach via AbstractE2ETest
        // After login, we navigate away from dev page (done in AbstractE2ETest)
        String currentUrl = page.url();
        assertTrue(!currentUrl.contains("/dev"), "Should not be on dev page after login");
    }

    @Test
    @DisplayName("Navigate to assistant page")
    void testNavigateToAssistant() {
        // Navigate to assistant
        AssistantPage assistant = new AssistantPage(page);
        assistant.navigate(getBaseUrl());

        String currentUrl = page.url();
        assertTrue(currentUrl.contains("/assistant"), "Should be on assistant page");
    }

    @Test
    @DisplayName("Part number search is functional")
    void testPartNumberSearchFunctional() {
        // Navigate to assistant
        AssistantPage assistant = new AssistantPage(page);
        assistant.navigate(getBaseUrl());

        // Take screenshot to debug
        takeScreenshot("assistant_page");

        // Verify the part number modal is shown with textarea
        boolean textAreaVisible = page.locator("textarea").isVisible();
        logger.info(() -> "Text area visible: " + textAreaVisible);

        // Verify analyze button is present (text: "Analyze input")
        boolean analyzeButtonVisible = page.getByText("Analyze input").isVisible();
        logger.info(() -> "Analyze button visible: " + analyzeButtonVisible);

        assertTrue(textAreaVisible, "Text area for part numbers should be visible");
        assertTrue(analyzeButtonVisible, "Analyze input button should be visible");
    }

    @Test
    @DisplayName("Test materials exist in database")
    void testMaterialsExistInDatabase() {
        // Check if our test materials are in the database
        Integer count = jdbcTemplate.queryForObject(
            "SELECT COUNT(*) FROM material WHERE normalized_part_number IN ('004222640104', '003064540201')",
            Integer.class
        );
        logger.info(() -> "Found " + count + " test materials in database");

        // List all materials for debugging
        var materials = jdbcTemplate.queryForList(
            "SELECT part_number, normalized_part_number, name FROM material LIMIT 20"
        );
        logger.info(() -> "Materials in DB: " + materials);

        // Test the exact SQL that the API uses
        var searchResult = jdbcTemplate.queryForList(
            "SELECT * FROM material WHERE part_number IN (?) OR normalized_part_number IN (?)",
            "003064540201", "003064540201"
        );
        logger.info(() -> "Search result for '003064540201': " + searchResult);

        // Also test with the original part number
        var searchResult2 = jdbcTemplate.queryForList(
            "SELECT * FROM material WHERE part_number IN (?) OR normalized_part_number IN (?)",
            "3064540201", "3064540201"
        );
        logger.info(() -> "Search result for '3064540201': " + searchResult2);

        assertTrue(count != null && count >= 2, "At least 2 test materials should exist. Found: " + count);
    }
}
