"""
Map frontend smoke tests — HTML structure, key controls, pin/geofence rendering logic.
Verifies the map UI elements are present in the served HTML.
"""
import os
import subprocess
import pytest

BASE_URL = "https://canteen.ourpad.casa"


def _fetch():
    """Fetch the homepage and return body text."""
    r = subprocess.run(
        ["curl", "-sk", BASE_URL],
        capture_output=True, text=True, timeout=10
    )
    return r.stdout


BODY = None


def body():
    global BODY
    if BODY is None:
        BODY = _fetch()
    return BODY


class TestMapInitialization:
    """Map loads with Leaflet and correct tiles."""

    def test_leaflet_loaded(self):
        """Leaflet JS library is included."""
        assert "leaflet.js" in body() or "leaflet" in body()

    def test_leaflet_css_loaded(self):
        """Leaflet CSS is included."""
        assert "leaflet.css" in body()

    def test_leaflet_draw_loaded(self):
        """Leaflet Draw plugin for geofence drawing."""
        assert "leaflet.draw.js" in body()

    def test_leaflet_heat_loaded(self):
        """Leaflet Heat plugin for heatmap."""
        assert "leaflet-heat.js" in body()

    def test_osm_tiles_configured(self):
        """OpenStreetMap tile URL template used."""
        assert "tile.openstreetmap.org" in body()

    def test_init_map_function_exists(self):
        """JavaScript initMap() function is defined."""
        assert "function initMap" in body()

    def test_map_container_exists(self):
        """HTML element #mapContainer for the map."""
        assert 'id="mapContainer"' in body() or 'id="map-container"' in body()


class TestAssetPins:
    """Asset pin markers on the map."""

    def test_pin_toggle_function(self):
        """togglePins() function exists."""
        assert "function togglePins" in body()

    def test_load_asset_pins_function(self):
        """loadAssetPins() function exists."""
        assert "function loadAssetPins" in body() or "loadAssetPins" in body()

    def test_add_asset_marker_function(self):
        """addAssetMarker() function exists."""
        assert "function addAssetMarker" in body() or "addAssetMarker" in body()

    def test_marker_uses_leaflet_marker(self):
        """Markers created via L.marker()."""
        assert "L.marker" in body()

    def test_pin_filter_null_coords(self):
        """Pins only created for assets with non-null lat/lng."""
        assert "a.latitude != null" in body() or "latitude != null" in body()

    def test_directions_link_in_popup(self):
        """Popup includes Google Maps directions link."""
        assert "google.com/maps/dir" in body()

    def test_details_button_in_popup(self):
        """Popup includes Details button to switch to asset view."""
        assert "viewAsset" in body()


class TestGeofenceUI:
    """Geofence drawing and display."""

    def test_geofence_toggle_function(self):
        """toggleGeofenceDraw() function exists."""
        assert "function toggleGeofenceDraw" in body()

    def test_geofence_save_function(self):
        """saveDrawnGeofence() function exists."""
        assert "function saveDrawnGeofence" in body()

    def test_geofence_cancel_function(self):
        """cancelGeofenceDraw() function exists."""
        assert "function cancelGeofenceDraw" in body()

    def test_load_geofences_function(self):
        """loadGeofences() function exists."""
        assert "function loadGeofences" in body()

    def test_geofence_popup_with_edit_delete(self):
        """Geofence popup includes Edit and Delete buttons."""
        content = body()
        assert "editGeofence" in content
        assert "deleteGeofence" in content

    def test_geofence_color_picker(self):
        """Geofence color picker input exists."""
        assert "geofenceColor" in body()

    def test_geofence_chip_ui(self):
        """Geofence toggle chip UI element."""
        assert "chipGeo" in body() or "Add Geofence" in body()


class TestGPSControls:
    """GPS centering and user location."""

    def test_center_on_gps_function(self):
        """centerOnGPS() function exists."""
        assert "function centerOnGPS" in body()

    def test_gps_blue_dot_marker(self):
        """User location shown as blue circle marker."""
        assert "circleMarker" in body()

    def test_gps_toast_on_missing(self):
        """Toast shown when GPS unavailable."""
        assert "GPS location not available" in body()

    def test_pins_chip_ui(self):
        """Pin toggle chip exists."""
        assert "chipPins" in body()


class TestHeatmap:
    """Heatmap layer controls."""

    def test_heatmap_toggle_function(self):
        """toggleHeatmap() function exists."""
        assert "function toggleHeatmap" in body() or "toggleHeatmap" in body()

    def test_heatmap_data_function(self):
        """loadHeatmapData() function exists."""
        assert "function loadHeatmapData" in body() or "loadHeatmapData" in body()


class TestMapRefresh:
    """Map lifecycle and data refresh."""

    def test_map_invalidate_on_tab_switch(self):
        """invalidateSize() called when tab becomes visible."""
        assert "invalidateSize" in body()

    def test_pins_refresh_on_data_load(self):
        """clearAssetMarkers() exists for refreshing pins."""
        assert "function clearAssetMarkers" in body()

    def test_map_returns_200(self):
        """Homepage serves successfully."""
        assert "Canteen Asset Tracker" in body()
