import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;



/**
 * FrontendTests class that contains three JUnit tests to verify functionality of the frontend class.
 * Checks that HTML prompts correctly formatted and responses correctly
 * display data from Backend (placeholder version)
 */
public class FrontendTests {

    /**
     * First roleTest1 tests the generation of HTML prompts for user input.
     * Makes sure that generateShortestPathPromptHTML() and
     * generateTenClosestLocationsPromptHTML() return strings containing required input
     * IDs, such as "start", "end", "from".
     */
    @Test
    public void roleTest1() {
        //Initialize classes needed for test
        Graph_Placeholder graph = new Graph_Placeholder();
        Backend_Placeholder backend = new Backend_Placeholder(graph);
        Frontend frontend = new Frontend(backend);

        // Test Shortest Path prompt
        String pathPrompt = frontend.generateShortestPathPromptHTML();
        assertNotNull(pathPrompt, "Shortest path prompt should not be empty/null");
        assertTrue(pathPrompt.contains("id=\"start\""), "Should contain input with id='start'");
        assertTrue(pathPrompt.contains("id=\"end\""), "Should contain input with id='end'");
        assertTrue(pathPrompt.contains("Find Shortest Path"), "Button label should correctly display");

        // Test Ten Closest Locations Prompt
        String closestPrompt = frontend.generateTenClosestLocationsPromptHTML();
        assertNotNull(closestPrompt, "Closest Loc. prompt should not be empty/null");
        assertTrue(closestPrompt.contains("id=\"from\""), "Should contain input with id='from'");
        assertTrue(closestPrompt.contains("Ten Closest Locations"), "Button label should correctly display");

    }

    /**
     * roleTest2 Tsts the generation of the Shortest Path response HTML.
     * Verifies that the Frontend correctly gets data from backend like path list
     * and times, and properly formats them into an ordered list (<ol>).
     */
    @Test
    public void roleTest2() {
        // Initialize classes needed for tests
        Graph_Placeholder graph = new Graph_Placeholder();
        Backend_Placeholder backend = new Backend_Placeholder(graph);
        Frontend frontend = new Frontend(backend);

        String start = "Union South";
        String end = "Weeks Hall for Geological Sciences";
        String response = frontend.generateShortestPathResponseHTML(start, end);

        // Check HTML formatting
        assertTrue(response.contains("<ol>"), "Response should contain an ordered list for path");
        assertTrue(response.contains("<li>Union South</li>"), "Should list starting location in path");

        // Verify total tie calculation
        // Should be 1.0 + 2.0 + 3.0 = 6.0 according to Backend_Placeholder
        assertTrue(response.contains("6.0"), "Response should show the total travel time as 6.0");
    }

    /**
     * roleTest3 tests the generation of the ten closest locations response HTML.
     * Makes sure that locations returned by backend are formatted in an
     * unordered list (<ul>). Also checks that frontend includes a line identifying start location.
     */
    @Test
    public void roleTest3() {
        // Initialize necessary classes for testing
        Graph_Placeholder graph = new Graph_Placeholder();
        Backend_Placeholder backend = new Backend_Placeholder(graph);
        Frontend frontend = new Frontend(backend);

        String startLocation = "Union South";
        String response = frontend.generateTenClosestLocationsResponseHTML(startLocation);

        // Verify HTML's structure and content is correct
        assertTrue(response.contains("<ul>"), "Response should contain unorder list <ul>");
        assertTrue(response.contains("<li>Computer Sciences and Statistics</li>"), "Should list nearby locations");
        assertTrue(response.contains("Union South"), "Should mention start location in response");

        // Error check that method won't crash if no input
        String errorResponse = frontend.generateTenClosestLocationsResponseHTML(null);
        assertNotNull(errorResponse, "Frontend should return some error message even if input is null");
    }
}
