#!/usr/bin/env python3
from __future__ import annotations

import json
import os
import sys
import urllib.request
import xml.etree.ElementTree as ET


def _escape_label(value: str) -> str:
    return value.replace("\\", "\\\\").replace("\n", "\\n").replace('"', '\\"')


def _label_str(labels: dict[str, str]) -> str:
    parts = [f'{key}="{_escape_label(val)}"' for key, val in labels.items() if val]
    return "{" + ",".join(parts) + "}" if parts else ""


def _load_coverage(path: str) -> float:
    with open(path, "r", encoding="utf-8") as handle:
        payload = json.load(handle)
    summary = payload.get("summary") or {}
    percent = summary.get("percent_covered")
    if isinstance(percent, (int, float)):
        return float(percent)
    raise RuntimeError("coverage summary missing percent_covered")


def _load_junit(path: str) -> dict[str, int]:
    tree = ET.parse(path)
    root = tree.getroot()

    def _as_int(node, name: str) -> int:
        raw = node.attrib.get(name) or "0"
        try:
            return int(float(raw))
        except ValueError:
            return 0

    suites = []
    if root.tag == "testsuite":
        suites = [root]
    elif root.tag == "testsuites":
        suites = list(root.findall("testsuite"))

    totals = {"tests": 0, "failures": 0, "errors": 0, "skipped": 0}
    for suite in suites:
        totals["tests"] += _as_int(suite, "tests")
        totals["failures"] += _as_int(suite, "failures")
        totals["errors"] += _as_int(suite, "errors")
        totals["skipped"] += _as_int(suite, "skipped")
    return totals


def _post_metrics(url: str, payload: str) -> None:
    req = urllib.request.Request(
        url,
        data=payload.encode("utf-8"),
        method="POST",
        headers={"Content-Type": "text/plain"},
    )
    with urllib.request.urlopen(req, timeout=10) as resp:
        if resp.status >= 400:
            raise RuntimeError(f"metrics push failed status={resp.status}")


def main() -> int:
    vm_url = os.getenv("VM_IMPORT_URL", "").strip()
    if not vm_url:
        print("VM_IMPORT_URL not set; skipping metrics push")
        return 0

    coverage_path = os.getenv("COVERAGE_JSON", "build/coverage.json")
    junit_path = os.getenv("JUNIT_XML", "build/junit.xml")

    if not os.path.exists(coverage_path):
        raise RuntimeError(f"missing coverage file {coverage_path}")
    if not os.path.exists(junit_path):
        raise RuntimeError(f"missing junit file {junit_path}")

    coverage = _load_coverage(coverage_path)
    totals = _load_junit(junit_path)
    passed = max(totals["tests"] - totals["failures"] - totals["errors"] - totals["skipped"], 0)

    labels = {
        "job": os.getenv("CI_JOB_NAME", "metis"),
        "branch": os.getenv("BRANCH_NAME", ""),
        "build_number": os.getenv("BUILD_NUMBER", ""),
        "commit": os.getenv("GIT_COMMIT", ""),
        "repo": os.getenv("REPO_NAME", "metis"),
    }

    prefix = os.getenv("METRICS_PREFIX", "ariadne_ci")
    lines = [
        f"{prefix}_coverage_percent{_label_str(labels)} {coverage:.3f}",
        f"{prefix}_tests_total{_label_str({**labels, 'result': 'passed'})} {passed}",
        f"{prefix}_tests_total{_label_str({**labels, 'result': 'failed'})} {totals['failures']}",
        f"{prefix}_tests_total{_label_str({**labels, 'result': 'error'})} {totals['errors']}",
        f"{prefix}_tests_total{_label_str({**labels, 'result': 'skipped'})} {totals['skipped']}",
        f"{prefix}_build_info{_label_str(labels)} 1",
    ]

    payload = "\n".join(lines) + "\n"
    _post_metrics(vm_url, payload)
    print("metrics push complete")
    return 0


if __name__ == "__main__":
    try:
        sys.exit(main())
    except Exception as exc:
        print(f"metrics push failed: {exc}")
        sys.exit(1)
