from __future__ import annotations

import json
from http import HTTPStatus

from osm_lead_source_service import __version__
from osm_lead_source_service.http_service import route_response, service_status


def test_service_defaults_to_shadow_read_only_mode() -> None:
    status = service_status({})

    assert status == {
        "dashboard_snapshot_configured": False,
        "environment": "local",
        "legacy_writer_disabled": False,
        "mcp_enabled": False,
        "mode": "shadow-read-only",
        "noco_write_enabled": False,
        "phase": "2B",
        "read_only": True,
        "release_sha": "unknown",
        "scheduler_enabled": False,
        "service": "osm-lead-source-service",
        "sidecar_configured": False,
        "stale_deletion_enabled": False,
        "version": __version__,
        "write_runtime_armed": False,
        "writer_enabled": False,
    }


def test_status_arms_only_when_both_writer_gates_are_true() -> None:
    writer_only = service_status({"OSM_LEAD_SOURCE_WRITER_ENABLED": "true"})
    armed = service_status(
        {
            "OSM_LEAD_SOURCE_WRITER_ENABLED": "true",
            "OSM_LEAD_SOURCE_LEGACY_WRITER_DISABLED": "true",
            "OSM_LEAD_SOURCE_NOCO_API_TOKEN": "must-not-appear",
            "OSM_LEAD_SOURCE_SIDECAR_DATABASE_URL": "must-not-appear",
        }
    )

    assert writer_only["write_runtime_armed"] is False
    assert armed["write_runtime_armed"] is True
    assert armed["mode"] == "controlled-writer"
    assert armed["sidecar_configured"] is True
    rendered = repr(dict(armed))
    assert "must-not-appear" not in rendered


def test_scheduler_status_requires_separate_approval() -> None:
    requested = service_status({"OSM_LEAD_SOURCE_SCHEDULER_ENABLED": "true"})
    approved_only = service_status({"OSM_LEAD_SOURCE_SCHEDULER_APPROVED": "true"})
    effective = service_status(
        {
            "OSM_LEAD_SOURCE_SCHEDULER_ENABLED": "true",
            "OSM_LEAD_SOURCE_SCHEDULER_APPROVED": "true",
        }
    )

    assert requested["scheduler_enabled"] is False
    assert approved_only["scheduler_enabled"] is False
    assert effective["scheduler_enabled"] is True


def test_root_returns_dashboard_html() -> None:
    response = route_response("/", {"OSM_LEAD_SOURCE_ENVIRONMENT": "production"})

    assert response.status is HTTPStatus.OK
    assert response.content_type == "text/html; charset=utf-8"
    assert b"OpenStreetMap Lead Generator" in response.body
    assert b"shadow-read-only" not in response.body


def test_status_and_dashboard_api_remain_machine_readable() -> None:
    status_response = route_response("/status", {})
    dashboard_response = route_response("/api/dashboard", {})

    status_payload = json.loads(status_response.body)
    dashboard_payload = json.loads(dashboard_response.body)
    assert status_response.status is HTTPStatus.OK
    assert status_payload["mode"] == "shadow-read-only"
    assert dashboard_payload["snapshot"]["counts"]["active"] == 980
    assert dashboard_payload["service"]["write_runtime_armed"] is False


def test_health_readiness_favicon_and_unknown_routes() -> None:
    health = route_response("/healthz", {})
    ready = route_response("/readyz?probe=1", {})
    favicon = route_response("/favicon.ico", {})
    missing = route_response("/missing", {})

    assert json.loads(health.body) == {"ok": True}
    assert json.loads(ready.body) == {"mode": "shadow-read-only", "ok": True}
    assert favicon.status is HTTPStatus.NO_CONTENT
    assert favicon.body == b""
    assert missing.status is HTTPStatus.NOT_FOUND
    assert json.loads(missing.body) == {"error": "not_found"}
