from __future__ import annotations

"""Parse test results and format Pushgateway-friendly metrics payloads."""

from dataclasses import dataclass
import urllib.request
import xml.etree.ElementTree as ET
from pathlib import Path
from typing import Iterable


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


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


@dataclass(frozen=True)
class RunSummary:
    """Aggregate counts from a collection of JUnit XML files."""

    tests: int = 0
    failures: int = 0
    errors: int = 0
    skipped: int = 0

    @property
    def passed(self) -> int:
        """Return the number of passing test cases derived from the aggregate."""
        return max(self.tests - self.failures - self.errors - self.skipped, 0)


def load_junit_summary(paths: Iterable[Path]) -> RunSummary:
    """Load one or more JUnit XML files and combine their result counts.

    WHY: CI needs a single stable view of each suite instead of separately
    parsing backend, unit, component, and e2e XML files in shell.
    """

    totals = {"tests": 0, "failures": 0, "errors": 0, "skipped": 0}
    for path in paths:
        if not path.exists():
            continue
        root = ET.parse(path).getroot()
        suites = [root] if root.tag == "testsuite" else list(root.findall("testsuite")) if root.tag == "testsuites" else []
        for node in suites:
            for key in totals:
                raw = node.attrib.get(key) or "0"
                try:
                    totals[key] += int(float(raw))
                except ValueError:
                    continue
    return RunSummary(**totals)


def load_junit_cases(paths: Iterable[Path]) -> list[tuple[str, str]]:
    """Load per-test outcomes from one or more JUnit XML files."""
    cases: list[tuple[str, str]] = []
    for path in paths:
        if not path.exists():
            continue
        root = ET.parse(path).getroot()
        suites = [root] if root.tag == "testsuite" else list(root.findall("testsuite")) if root.tag == "testsuites" else []
        for suite in suites:
            for case in suite.findall("testcase"):
                name = (case.attrib.get("name") or "").strip()
                classname = (case.attrib.get("classname") or "").strip()
                if not name:
                    continue
                test_id = f"{classname}::{name}" if classname else name
                status = "passed"
                if case.find("failure") is not None:
                    status = "failed"
                elif case.find("error") is not None:
                    status = "error"
                elif case.find("skipped") is not None:
                    status = "skipped"
                cases.append((test_id, status))
    return cases


def read_pushgateway_counters(text: str, *, suite: str, job: str) -> dict[str, float]:
    """Read the current quality-gate counters for a suite from Pushgateway text."""

    counters: dict[str, float] = {"ok": 0.0, "failed": 0.0}
    for line in text.splitlines():
        if not line.startswith("platform_quality_gate_runs_total{"):
            continue
        if f'job="{job}"' not in line or f'suite="{suite}"' not in line:
            continue
        parts = line.split()
        if len(parts) < 2:
            continue
        for status in counters:
            if f'status="{status}"' not in line:
                continue
            try:
                counters[status] = float(parts[1])
            except ValueError:
                counters[status] = 0.0
    return counters


def pushgateway_series_exists(text: str, *, metric: str, labels: dict[str, str]) -> bool:
    """Return whether a labeled series already exists in Pushgateway text."""

    for line in text.splitlines():
        if not line.startswith(metric + "{"):
            continue
        if all(f'{key}="{value}"' in line for key, value in labels.items()):
            return True
    return False


def render_payload(
    *,
    suite: str,
    ok: int,
    failed: int,
    summary: RunSummary,
    workspace_line_coverage_percent: float = 0.0,
    source_files_total: int = 0,
    source_lines_over_500: int = 0,
    branch: str = "",
    build_number: str = "",
    jenkins_job: str = "",
    checks: dict[str, str] | None = None,
    test_cases: list[tuple[str, str]] | None = None,
) -> str:
    """Render the Pushgateway payload for the quality-gate counters."""
    build_labels = {
        "suite": suite,
        "branch": branch or "unknown",
        "build_number": build_number or "unknown",
        "jenkins_job": jenkins_job or suite,
    }
    test_case_base_labels = dict(build_labels)
    payload = (
        "# TYPE platform_quality_gate_runs_total counter\n"
        f'platform_quality_gate_runs_total{{suite="{suite}",status="ok"}} {ok}\n'
        f'platform_quality_gate_runs_total{{suite="{suite}",status="failed"}} {failed}\n'
        "# TYPE bstein_home_quality_gate_tests_total gauge\n"
        f'bstein_home_quality_gate_tests_total{{suite="{suite}",result="passed"}} {summary.passed}\n'
        f'bstein_home_quality_gate_tests_total{{suite="{suite}",result="failed"}} {summary.failures}\n'
        f'bstein_home_quality_gate_tests_total{{suite="{suite}",result="error"}} {summary.errors}\n'
        f'bstein_home_quality_gate_tests_total{{suite="{suite}",result="skipped"}} {summary.skipped}\n'
        "# TYPE platform_quality_gate_workspace_line_coverage_percent gauge\n"
        f'platform_quality_gate_workspace_line_coverage_percent{{suite="{suite}"}} {workspace_line_coverage_percent:.3f}\n'
        "# TYPE platform_quality_gate_source_files_total gauge\n"
        f'platform_quality_gate_source_files_total{{suite="{suite}"}} {int(source_files_total)}\n'
        "# TYPE platform_quality_gate_source_lines_over_500_total gauge\n"
        f'platform_quality_gate_source_lines_over_500_total{{suite="{suite}"}} {int(source_lines_over_500)}\n'
        "# TYPE platform_quality_gate_build_info gauge\n"
        f"platform_quality_gate_build_info{_label_str(build_labels)} 1\n"
    )
    if checks:
        payload += "# TYPE bstein_home_quality_gate_checks_total gauge\n"
        payload += "".join(
            f'bstein_home_quality_gate_checks_total{{suite="{suite}",check="{check_name}",result="{check_status}"}} 1\n'
            for check_name, check_status in checks.items()
        )
    payload += "# TYPE platform_quality_gate_test_case_result gauge\n"
    if test_cases:
        payload += "".join(
            f"platform_quality_gate_test_case_result{_label_str({**test_case_base_labels, 'test': test_name, 'status': test_status})} 1\n"
            for test_name, test_status in test_cases
        )
    else:
        payload += (
            f"platform_quality_gate_test_case_result"
            f"{_label_str({**test_case_base_labels, 'test': '__no_test_cases__', 'status': 'skipped'})} 1\n"
        )
    return payload


def publish_quality_metrics(
    *,
    gateway: str,
    suite: str,
    job: str,
    status: str,
    summary: RunSummary,
    workspace_line_coverage_percent: float = 0.0,
    source_files_total: int = 0,
    source_lines_over_500: int = 0,
    branch: str = "",
    build_number: str = "",
    jenkins_job: str = "",
    checks: dict[str, str] | None = None,
    test_cases: list[tuple[str, str]] | None = None,
) -> None:
    """Publish run and test totals to Pushgateway."""

    gateway = gateway.rstrip("/")
    text = urllib.request.urlopen(f"{gateway}/metrics", timeout=10).read().decode("utf-8", errors="replace")
    counters = read_pushgateway_counters(text, suite=suite, job=job)
    already_recorded = bool(build_number) and pushgateway_series_exists(
        text,
        metric="platform_quality_gate_build_info",
        labels={
            "job": job,
            "suite": suite,
            "branch": branch or "unknown",
            "build_number": build_number or "unknown",
            "jenkins_job": jenkins_job or suite,
        },
    )
    if not already_recorded:
        if status == "ok":
            counters["ok"] += 1
        else:
            counters["failed"] += 1

    payload = render_payload(
        suite=suite,
        ok=int(counters["ok"]),
        failed=int(counters["failed"]),
        summary=summary,
        workspace_line_coverage_percent=workspace_line_coverage_percent,
        source_files_total=source_files_total,
        source_lines_over_500=source_lines_over_500,
        branch=branch,
        build_number=build_number,
        jenkins_job=jenkins_job or suite,
        checks=checks,
        test_cases=test_cases,
    )
    req = urllib.request.Request(
        f"{gateway}/metrics/job/{job}/suite/{suite}",
        data=payload.encode("utf-8"),
        method="PUT",
        headers={"Content-Type": "text/plain"},
    )
    urllib.request.urlopen(req, timeout=10).read()
