"""
Entry point for coverage change check (`cov_change_check`).
"""
import json
import argparse
import os


def main() -> None:
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "coverage_change_file",
        nargs="?",
        help="coverage change file generated by previous run",
        default="coverage_change.json",
    )
    parser.add_argument(
        "--total", help="minimum total coverage percent", default=None, required=False
    )
    parser.add_argument(
        "--file", help="minimum coverage percent per file", default=None, required=False
    )

    args = parser.parse_args()
    if not os.path.exists(args.coverage_change_file):
        raise Exception(f"{args.coverage_change_file} not found for coverage data")
    with open(args.coverage_change_file, "r") as f:
        json_data = json.load(f)

    fail = False
    if args.total is not None:
        total = float(args.total)
        if json_data["total_coverage"] < total:
            print(f"Total coverage = {json_data['total_coverage']}%, less than {total}")
            fail = True
    if args.file is not None:
        file = float(args.file)
        for file_name, file_data in json_data["files"].items():
            if file_data["coverage_percent"] < file:
                print(
                    f"File coverage is {file_data['coverage_percent']}% for {file_name}, less than {file}"
                )
                fail = True
    if fail:
        exit(1)
    else:
        print("Coverage is good")


if __name__ == "__main__":
    main()
