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

import argparse
import re
import shutil
import subprocess
import sys
from datetime import datetime
from pathlib import Path

HELPERS = r'''
def as_dict(value: Any) -> dict[str, Any]:
    """Return value as a dict, or an empty dict for any other JSON shape."""
    return value if isinstance(value, dict) else {}


def as_list(value: Any) -> list[Any]:
    """Return value as a list; wrap scalar/dict values and ignore null."""
    if value is None:
        return []
    if isinstance(value, list):
        return value
    if isinstance(value, tuple):
        return list(value)
    return [value]


def scalar_text(value: Any, *preferred_keys: str) -> str:
    """Safely normalize strings, numbers, booleans and common API objects."""
    if value is None:
        return ""
    if isinstance(value, str):
        return clean(value)
    if isinstance(value, (int, float, bool)):
        return clean(value)
    if isinstance(value, dict):
        keys = preferred_keys or (
            "identifier", "name", "value", "url", "html_url", "login", "full_name"
        )
        for key in keys:
            if key in value and value[key] is not None:
                text = scalar_text(value[key])
                if text:
                    return text
        return ""
    if isinstance(value, (list, tuple, set)):
        return ", ".join(unique_strings(scalar_text(x) for x in value))
    return clean(value)


def dict_text(value: Any, key: str, *fallback_keys: str) -> str:
    obj = as_dict(value)
    for candidate in (key, *fallback_keys):
        if candidate in obj:
            text = scalar_text(obj.get(candidate))
            if text:
                return text
    return ""


def nested_text(value: Any, *path: str) -> str:
    current = value
    for key in path:
        if not isinstance(current, dict):
            return ""
        current = current.get(key)
    return scalar_text(current)
'''

CVE_FUNC = r'''
def cve_id_from_record(record: dict[str, Any]) -> str:
    record = as_dict(record)
    value = scalar_text(
        record.get("name")
        or record.get("cve_id")
        or record.get("id")
        or record.get("cve"),
        "id",
        "name",
        "cve_id",
    ).upper()
    return value if CVE_RX.fullmatch(value) else ""
'''

REPO_FUNC = r'''
def get_repo_from_api_item(item: dict[str, Any]) -> str:
    item = as_dict(item)
    repository = item.get("repository")
    if isinstance(repository, dict):
        full_name = scalar_text(repository.get("full_name"), "full_name", "name")
        if full_name:
            return full_name
    elif isinstance(repository, str) and "/" in repository:
        return clean(repository)

    repo_url = scalar_text(item.get("repository_url"), "url")
    match = re.search(r"/repos/([^/]+/[^/]+)$", repo_url)
    return match.group(1) if match else ""
'''

ADVISORIES_FUNC = r'''
def collect_advisories(client: GitHubClient, cve_id: str) -> tuple[list[dict[str, Any]], list[str]]:
    evidence: list[dict[str, Any]] = []
    errors: list[str] = []

    for advisory_type in ("reviewed", "unreviewed"):
        try:
            response = client.get(
                "/advisories",
                {"cve_id": cve_id, "type": advisory_type, "per_page": 100},
            )
        except GitHubAPIError as exc:
            errors.append(f"advisory:{advisory_type}: {exc}")
            continue

        for raw_advisory in as_list(response):
            advisory = as_dict(raw_advisory)
            if not advisory:
                continue

            vulnerabilities: list[dict[str, str]] = []
            fixed_versions: list[dict[str, str]] = []

            for raw_vulnerability in as_list(advisory.get("vulnerabilities")):
                vulnerability = as_dict(raw_vulnerability)
                if not vulnerability:
                    continue

                raw_package = vulnerability.get("package")
                package = as_dict(raw_package)
                ecosystem = scalar_text(
                    package.get("ecosystem") if package else "",
                    "ecosystem",
                )
                package_name = scalar_text(
                    package.get("name") if package else raw_package,
                    "name",
                    "package",
                )

                raw_patched = vulnerability.get("first_patched_version")
                patched_version = scalar_text(
                    raw_patched,
                    "identifier",
                    "version",
                    "name",
                    "value",
                )

                vulnerable_range = scalar_text(
                    vulnerability.get("vulnerable_version_range"),
                    "identifier",
                    "range",
                    "value",
                )

                item = {
                    "ecosystem": ecosystem,
                    "package": package_name,
                    "vulnerable_version_range": vulnerable_range,
                    "first_patched_version": patched_version,
                }
                vulnerabilities.append(item)

                if patched_version:
                    fixed_versions.append(
                        {
                            "ecosystem": ecosystem,
                            "package": package_name,
                            "version": patched_version,
                            "source": "github_advisory",
                        }
                    )

            references: list[dict[str, Any]] = []
            for raw_reference in as_list(advisory.get("references")):
                if isinstance(raw_reference, dict):
                    ref_url = scalar_text(raw_reference, "url", "html_url", "href")
                else:
                    ref_url = scalar_text(raw_reference)
                classified = classify_reference_url(ref_url)
                if classified:
                    references.append(classified)

            ghsa_id = scalar_text(advisory.get("ghsa_id"), "ghsa_id", "id")
            evidence.append(
                {
                    "kind": "github_advisory",
                    "ghsa_id": ghsa_id,
                    "cve_id": scalar_text(advisory.get("cve_id"), "cve_id", "id") or cve_id,
                    "advisory_type": advisory_type,
                    "severity": scalar_text(advisory.get("severity"), "name", "value"),
                    "summary": short_text(scalar_text(advisory.get("summary")), 400),
                    "description": short_text(scalar_text(advisory.get("description")), 900),
                    "published_at": scalar_text(advisory.get("published_at")),
                    "updated_at": scalar_text(advisory.get("updated_at")),
                    "withdrawn_at": scalar_text(advisory.get("withdrawn_at")),
                    "url": scalar_text(advisory.get("html_url") or advisory.get("url"), "url", "html_url"),
                    "vulnerabilities": vulnerabilities,
                    "fixed_versions": fixed_versions,
                    "references": references,
                    "explicit_cve_reference": True,
                    "fix_language": bool(fixed_versions),
                    "verification": (
                        "patched_version_documented"
                        if fixed_versions
                        else "advisory_without_patched_version"
                    ),
                }
            )

    return evidence, errors
'''

COMMITS_FUNC = r'''
def collect_commits(
    client: GitHubClient,
    cve_id: str,
    max_results: int,
) -> tuple[list[dict[str, Any]], list[str]]:
    evidence: list[dict[str, Any]] = []
    if max_results <= 0:
        return evidence, []

    try:
        response = client.get(
            "/search/commits",
            {
                "q": f'"{cve_id}"',
                "sort": "committer-date",
                "order": "desc",
                "per_page": min(max_results, 100),
            },
        )
    except GitHubAPIError as exc:
        return [], [f"commits: {exc}"]

    response_obj = as_dict(response)
    for raw_item in as_list(response_obj.get("items")):
        item = as_dict(raw_item)
        if not item:
            continue
        commit = as_dict(item.get("commit"))
        author = as_dict(commit.get("author"))
        committer = as_dict(commit.get("committer"))
        message = scalar_text(commit.get("message"))
        explicit = has_explicit_cve(message, cve_id)
        fixing = explicit and fix_language(message)

        evidence.append(
            {
                "kind": "commit_search_result",
                "repository": get_repo_from_api_item(item),
                "sha": scalar_text(item.get("sha"), "sha", "id"),
                "url": scalar_text(item.get("html_url") or item.get("url"), "url", "html_url"),
                "message": short_text(message, 700),
                "author_date": scalar_text(author.get("date")),
                "committer_date": scalar_text(committer.get("date")),
                "explicit_cve_reference": explicit,
                "fix_language": fixing,
                "security_context": bool(SECURITY_RX.search(message)),
                "regression_test_language": bool(TEST_RX.search(message)),
                "verification": "explicit_cve_fix_commit" if fixing else "cve_mention_in_commit",
            }
        )

    return evidence[:max_results], []
'''

ISSUES_FUNC = r'''
def collect_issues_and_pulls(
    client: GitHubClient,
    cve_id: str,
    max_results: int,
) -> tuple[list[dict[str, Any]], list[str]]:
    evidence: list[dict[str, Any]] = []
    if max_results <= 0:
        return evidence, []

    try:
        response = client.get(
            "/search/issues",
            {
                "q": f'"{cve_id}" in:title,body',
                "sort": "updated",
                "order": "desc",
                "per_page": min(max_results, 100),
            },
        )
    except GitHubAPIError as exc:
        return [], [f"issues: {exc}"]

    response_obj = as_dict(response)
    for raw_item in as_list(response_obj.get("items")):
        item = as_dict(raw_item)
        if not item:
            continue

        title = scalar_text(item.get("title"))
        body = scalar_text(item.get("body"))
        text = f"{title} {body}".strip()
        html_url = scalar_text(item.get("html_url") or item.get("url"), "url", "html_url")
        parsed = classify_reference_url(html_url) or {}
        raw_pull = item.get("pull_request")
        pull = as_dict(raw_pull)
        is_pull_request = bool(raw_pull)
        explicit = has_explicit_cve(text, cve_id)
        fixing = explicit and fix_language(text)
        merged_at = scalar_text(pull.get("merged_at"))

        evidence.append(
            {
                "kind": "pull_request_search_result" if is_pull_request else "issue_search_result",
                "repository": scalar_text(parsed.get("repository")),
                "number": item.get("number") if isinstance(item.get("number"), int) else scalar_text(item.get("number")),
                "url": html_url,
                "title": short_text(title, 300),
                "body_excerpt": short_text(body, 700),
                "state": scalar_text(item.get("state"), "name", "value"),
                "created_at": scalar_text(item.get("created_at")),
                "updated_at": scalar_text(item.get("updated_at")),
                "closed_at": scalar_text(item.get("closed_at")),
                "merged_at": merged_at,
                "explicit_cve_reference": explicit,
                "fix_language": fixing,
                "security_context": bool(SECURITY_RX.search(text)),
                "regression_test_language": bool(TEST_RX.search(text)),
                "version_language": bool(VERSION_RX.search(text)),
                "verification": (
                    "merged_explicit_cve_fix_pull_request"
                    if is_pull_request and merged_at and fixing
                    else "explicit_cve_fix_candidate"
                    if fixing
                    else "cve_mention_in_pull_request"
                    if is_pull_request
                    else "cve_mention_in_issue"
                ),
            }
        )

    return evidence[:max_results], []
'''


def replace_function(source: str, name: str, replacement: str) -> str:
    pattern = re.compile(
        rf"(?ms)^def {re.escape(name)}\(.*?(?=^def |^# -{{5,}}|\Z)"
    )
    match = pattern.search(source)
    if not match:
        raise RuntimeError(f"Function not found: {name}")
    return source[: match.start()] + replacement.lstrip("\n") + "\n\n" + source[match.end():]


def main() -> int:
    parser = argparse.ArgumentParser(description="Harden github_cve_fixes.py JSON parsing")
    parser.add_argument("script", nargs="?", type=Path, default=Path("github_cve_fixes.py"))
    args = parser.parse_args()
    path = args.script.resolve()
    if not path.is_file():
        print(f"ERROR: not found: {path}", file=sys.stderr)
        return 2

    source = path.read_text(encoding="utf-8")
    stamp = datetime.now().strftime("%Y%m%d-%H%M%S")
    backup = path.with_name(f"{path.name}.bak-{stamp}")
    shutil.copy2(path, backup)

    # Insert normalization helpers immediately before cve_id_from_record.
    if "def as_dict(" not in source:
        marker = "def cve_id_from_record("
        index = source.find(marker)
        if index < 0:
            raise RuntimeError("cve_id_from_record marker not found")
        source = source[:index] + HELPERS.lstrip("\n") + "\n\n" + source[index:]

    source = replace_function(source, "cve_id_from_record", CVE_FUNC)
    source = replace_function(source, "get_repo_from_api_item", REPO_FUNC)
    source = replace_function(source, "collect_advisories", ADVISORIES_FUNC)
    source = replace_function(source, "collect_commits", COMMITS_FUNC)
    source = replace_function(source, "collect_issues_and_pulls", ISSUES_FUNC)

    # Make source field extraction tolerant too.
    source = source.replace(
        '"published": clean(source_record.get("published")),',
        '"published": scalar_text(as_dict(source_record).get("published")),',
    ).replace(
        '"severity": clean(source_record.get("score")),',
        '"severity": scalar_text(as_dict(source_record).get("score"), "name", "value"),',
    ).replace(
        '"cvss": source_record.get("cvss"),',
        '"cvss": as_dict(source_record).get("cvss"),',
    ).replace(
        '"surfaces": source_record.get("surfaces", []),',
        '"surfaces": unique_strings(as_list(as_dict(source_record).get("surfaces"))),',
    ).replace(
        '"nvd_url": clean(source_record.get("url")),',
        '"nvd_url": scalar_text(as_dict(source_record).get("url"), "url", "html_url"),',
    )

    # A malformed record must be recorded and skipped, not terminate all 149 CVEs.
    needle = '''            except RateLimitError as exc:\n                stopped_early = True\n                stop_reason = str(exc)\n                print(f"[WARN] {exc}", file=sys.stderr)\n                break\n            finally:\n'''
    replacement = '''            except RateLimitError as exc:\n                stopped_early = True\n                stop_reason = str(exc)\n                print(f"[WARN] {exc}", file=sys.stderr)\n                break\n            except Exception as exc:\n                output_records.append({\n                    "cve_id": cve_id,\n                    "remediation_status": "parse_error",\n                    "confidence": "unknown",\n                    "reasons": ["unexpected_record_shape_or_parser_error"],\n                    "repositories": [],\n                    "fixed_versions": [],\n                    "counts": {\n                        "advisories": 0,\n                        "commits": 0,\n                        "issues_and_pull_requests": 0,\n                        "advisory_references": 0,\n                        "fixed_versions": 0,\n                    },\n                    "github": {\n                        "advisories": [],\n                        "commits": [],\n                        "issues_and_pull_requests": [],\n                        "advisory_references": [],\n                    },\n                    "errors": [f"{type(exc).__name__}: {exc}"],\n                })\n                print(\n                    f"[WARN] {cve_id}: parse_error: {type(exc).__name__}: {exc}",\n                    file=sys.stderr,\n                )\n            finally:\n'''
    if needle not in source:
        raise RuntimeError("main exception block not found")
    source = source.replace(needle, replacement, 1)

    path.write_text(source, encoding="utf-8")
    result = subprocess.run(
        [sys.executable, "-m", "py_compile", str(path)],
        text=True,
        capture_output=True,
    )
    if result.returncode != 0:
        shutil.copy2(backup, path)
        print(result.stderr, file=sys.stderr)
        print(f"ERROR: syntax check failed; restored {backup}", file=sys.stderr)
        return 3

    print(f"Updated: {path}")
    print(f"Backup : {backup}")
    print("Syntax : OK")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
