#!/usr/bin/env python3
"""解析 coverage.xml 并输出覆盖率汇总。"""

import os
import sys
import xml.etree.ElementTree as ET

THRESHOLD = int(os.environ.get("COVERAGE_THRESHOLD", 65))  # 行覆盖率门槛，百分比，可通过环境变量覆盖


def main() -> int:
    try:
        tree = ET.parse("coverage.xml")
    except FileNotFoundError:
        print("coverage.xml 不存在，跳过汇总")
        return 0

    root = tree.getroot()
    line_rate = float(root.get("line-rate", 0)) * 100
    branch_rate = float(root.get("branch-rate", 0)) * 100
    lines_covered = int(root.get("lines-covered", 0))
    lines_valid = int(root.get("lines-valid", 0))

    print(f"行覆盖率: {line_rate:.2f}% ({lines_covered}/{lines_valid})")
    print(f"分支覆盖率: {branch_rate:.2f}%")
    print(f"门槛: {THRESHOLD}%")
    status = "PASS ✅" if line_rate >= THRESHOLD else "FAIL ❌"
    print(f"状态: {status}")

    return 0 if line_rate >= THRESHOLD else 1


if __name__ == "__main__":
    sys.exit(main())

