from pathlib import Path


ROOT = Path(__file__).resolve().parents[1]


def test_active_monitoring_prometheus_scrapes_momo_app_metrics():
    config = (ROOT / "monitoring/prometheus.yml").read_text(encoding="utf-8")

    assert "job_name: 'momo-app'" in config
    assert "metrics_path: /metrics" in config
    assert "targets: ['momo-pro-system:80']" in config


def test_monitoring_stack_connects_prometheus_to_momo_network():
    compose = (ROOT / "monitoring/docker-compose.yml").read_text(encoding="utf-8")

    assert "momo-network:" in compose
    assert "external: true" in compose
    assert "name: momo-network" in compose


def test_monitoring_cadvisor_uses_internal_network_only():
    compose = (ROOT / "monitoring/docker-compose.yml").read_text(encoding="utf-8")
    cadvisor_block = compose.split("  # Prometheus - 時序資料庫", 1)[0]

    assert "container_name: cadvisor" in cadvisor_block
    assert "ports:" not in cadvisor_block
    assert "cadvisor:8080" in (ROOT / "monitoring/prometheus.yml").read_text(encoding="utf-8")


def test_monitoring_stack_declares_blackbox_exporter():
    compose = (ROOT / "monitoring/docker-compose.yml").read_text(encoding="utf-8")

    assert "blackbox-exporter:" in compose
    assert "container_name: blackbox-exporter" in compose
    assert "blackbox-exporter:9115" in (ROOT / "monitoring/prometheus.yml").read_text(encoding="utf-8")


def test_active_blackbox_targets_only_include_live_uat_momo_entrypoint():
    config = (ROOT / "monitoring/prometheus.yml").read_text(encoding="utf-8")
    blackbox_block = config.split("job_name: 'blackbox-http'", 1)[1].split(
        "  # Blackbox - TCP",
        1,
    )[0]

    assert "- https://mo.wooo.work/health" in blackbox_block
    assert "- http://momo-pro-system:80/health" in blackbox_block
    assert "- https://mo.wooo.work\n" not in blackbox_block
    assert "192.168.0.110:5000" not in blackbox_block
    assert "https://momo.wooo.work" not in blackbox_block
    assert "https://wooo.work" not in blackbox_block


def test_blackbox_http_targets_use_lightweight_health_endpoint():
    config = (ROOT / "monitoring/prometheus.yml").read_text(encoding="utf-8")
    blackbox_block = config.split("job_name: 'blackbox-http'", 1)[1].split(
        "  # Blackbox - TCP",
        1,
    )[0]

    assert "/health" in blackbox_block
    assert "mo.wooo.work\n" not in blackbox_block


def test_compose_prometheus_uses_container_dns_for_momo_app():
    config = (ROOT / "docker/prometheus/prometheus.yml").read_text(encoding="utf-8")

    assert "job_name: 'momo-app'" in config
    assert "targets: ['momo-pro-system:80']" in config


def test_compose_prometheus_blackbox_targets_health_only():
    config = (ROOT / "docker/prometheus/prometheus.yml").read_text(encoding="utf-8")
    uat_block = config.split("job_name: 'blackbox-http-uat'", 1)[1].split(
        "job_name: 'blackbox-http-prod'",
        1,
    )[0]
    prod_block = config.split("job_name: 'blackbox-http-prod'", 1)[1].split(
        "job_name: 'blackbox-tcp'",
        1,
    )[0]

    assert "- https://mo.wooo.work/health" in uat_block
    assert "- http://momo-pro-system:80/health" in uat_block
    assert "- https://mo.wooo.work\n" not in uat_block
    assert "- http://192.168.0.110:5001\n" not in uat_block
    assert "- https://momo.wooo.work/health" in prod_block
    assert "- https://momo.wooo.work\n" not in prod_block
