#!/usr/bin/env python3
# Copyright (c) 2006-Present, Redis Ltd.
# All rights reserved.
#
# Licensed under your choice of the Redis Source Available License 2.0
# (RSALv2); or (b) the Server Side Public License v1 (SSPLv1); or (c) the
# GNU Affero General Public License v3 (AGPLv3).

"""Validated backport publication and deterministic PR/summary templates.

The unified workflow calls apply_target() with a trusted target allow-list and
an agent manifest. It owns the combined summary and error recovery in unified.py.
The standalone CLI remains useful with APPLY_DRY_RUN=1 for local inspection.
Every git/gh invocation uses argument arrays and PrivilegedGit sanitizes the
agent's clone before installing the publication credential.
"""

from __future__ import annotations

import html
import json
import os
import re
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parent))
import common  # noqa: E402

# APPLY_DRY_RUN=1 makes this print the push / PR / label / comment it WOULD do
# without performing any write (read-only pre-flight still runs). Lets the whole
# resolve -> cherry-pick -> apply pipeline be exercised locally, and is a safe
# operational plan mode.
DRY_RUN = os.environ.get("APPLY_DRY_RUN") == "1"

# Same release-branch shape the resolve step validates targets against; a branch
# name is only ever `backport-agent/pr-<pr>-to-<target>` with a valid target.
TARGET_RE = re.compile(r"^\d{1,4}\.\d{1,4}(?:-[A-Za-z0-9._-]{1,64})?$")
VALID_STATUSES = {"clean", "conflicts", "skipped"}


def branch_for(pr: int, target: str) -> str:
    return f"backport-agent/pr-{pr}-to-{target}"


def release_notes_block(original_body: str) -> str:
    """Replicate the original PR's release-notes checkbox state (exactly one is
    checked, per the PR template). Falls back to 'requires' — the conservative
    choice that forces a human to reconsider — if neither line is found checked.
    """
    requires = "- [ ] This PR requires release notes"
    does_not = "- [ ] This PR does not require release notes"
    body = original_body or ""
    if re.search(r"^\s*-\s*\[[xX]\]\s*This PR requires release notes", body, re.M):
        requires = "- [x] This PR requires release notes"
    elif re.search(r"^\s*-\s*\[[xX]\]\s*This PR does not require release notes", body, re.M):
        does_not = "- [x] This PR does not require release notes"
    else:
        requires = "- [x] This PR requires release notes"
    return f"{requires}\n{does_not}"


def pr_body(ctx: dict, entry: dict) -> str:
    """Build the backport PR body from templates + the agent's conflict log."""
    pr, sha = ctx["pr"], ctx["sha"]
    lines = [
        f"Backport of #{pr} to `{entry['target']}`.",
        "",
        "## Original PR",
        f"- Title: {ctx.get('title', '')}",
        f"- Link: {ctx.get('url', '')}",
        f"- Merge commit: `{sha}`",
        "",
        "## Cherry-pick result",
    ]
    cl = entry.get("conflict_log")
    conflict_log = cl if isinstance(cl, list) else []
    if entry.get("status") == "conflicts" and conflict_log:
        lines.append(f"- Resolved {len(conflict_log)} conflict(s) — see Conflict Log below.")
        lines += ["", "## Conflict Log"]
        for c in conflict_log:
            if not isinstance(c, dict):
                continue
            lines += [
                f"### `{c.get('path', '')}`",
                f"- **Conflict:** {c.get('conflict', '')}",
                f"- **Why it conflicted:** {c.get('why', '')}",
                f"- **Resolution:** {c.get('resolution', '')}",
                f"- **Rationale:** {c.get('rationale', '')}",
            ]
    else:
        lines.append("- Clean cherry-pick — no conflicts.")
    lines += ["", "## Release notes", "", release_notes_block(ctx.get("body", "")),
              "", "🤖 Generated by the auto-backport workflow."]
    return "\n".join(lines)


def existing_pr(branch: str) -> str | None:
    """`<state> <url>` of a same-repo PR already on this head branch, else None.

    Filters to `isCrossRepository == false` so a fork PR reusing the predictable
    branch name can't make us treat the slot as taken (or skip a real backport).
    """
    out = common.gh(
        "pr", "list", "--head", branch, "--state", "all", "--limit", "100",
        "--json", "url,state,isCrossRepository",
        "--jq", '[.[] | select(.isCrossRepository == false)] | .[0] | select(.) '
                '| "\\(.state) \\(.url)"',
        check=False,
    )
    return out.strip() or None


def apply_target(ctx: dict, git: common.PrivilegedGit, entry: dict) -> dict:
    """Push + open the PR for one manifest entry. Returns a summary row.

    `git` is the sanitized, token-bearing handle from `main()` — every mutation
    below goes through it, so the hardening lives in one place (common.py).
    """
    pr = ctx["pr"]
    target = entry.get("target")
    status = entry.get("status")
    # `error` = a validation/apply failure that must fail the run (and is NOT a
    # valid coverage outcome); `skipped` is reserved for legit no-ops (an explicit
    # agent skip, a missing target branch, an already-open PR). Type-check the
    # (agent-authored) entry so a malformed field is an error row, never a crash
    # and never a silently-"covered" target.
    row = {"target": target if isinstance(target, str) else str(target),
           "status": "error", "detail": ""}

    if not isinstance(target, str) or not TARGET_RE.match(target) or target not in ctx["targets"]:
        row["detail"] = f"invalid/unknown target {target!r}"
        return row
    if not isinstance(status, str) or status not in VALID_STATUSES:
        row["detail"] = f"invalid status {status!r}"
        return row
    if status == "skipped":
        row["status"] = "skipped"
        row["detail"] = entry.get("reason", "skipped by agent")
        return row

    branch = branch_for(pr, target)
    if entry.get("branch") != branch:
        row["detail"] = f"branch {entry.get('branch')!r} != expected {branch!r}"
        return row

    # The agent must actually have produced the local branch.
    if git("rev-parse", "--verify", f"refs/heads/{branch}", check=False).returncode != 0:
        row["detail"] = "agent did not produce the branch"
        return row
    # Target must still exist on origin — a legit skip, not an error.
    if git("ls-remote", "--exit-code", "--heads", "origin", target,
           check=False).returncode != 0:
        row["status"] = "skipped"
        row["detail"] = f"no such branch {target}"
        return row
    # Idempotency: never re-open / force over an existing backport PR — legit skip.
    if (ex := existing_pr(branch)):
        row["status"] = "skipped"
        row["detail"] = f"already {ex}"
        return row

    title = f"[{target}] {ctx.get('title', '')}"
    labels = list(dict.fromkeys(ctx.get("labels", []) + ["auto-backport"] +
                              (["auto-backport-conflicts"] if status == "conflicts" else [])))
    cl = entry.get("conflict_log")
    n_conflicts = len(cl) if isinstance(cl, list) else 0
    status_label = f"conflicts({n_conflicts})" if status == "conflicts" else "clean"

    if DRY_RUN:
        row["status"] = status_label
        row["detail"] = f"[dry-run] would push {branch}, open PR '{title}', labels {labels}"
        return row

    # Plain (non-force) push of a fresh ref; `push_ref` supplies the `--no-verify`
    # and full refspec hardening (see common.PrivilegedGit).
    push = git.push_ref(branch)
    if push.returncode != 0:
        row["detail"] = f"push failed: {push.stderr.strip()[:200]}"
        return row
    pushed_sha = git("rev-parse", branch, check=False).stdout.strip()

    body_file = os.path.join(os.environ["RUNNER_TEMP"], f"backport-body-{target}.md")
    with open(body_file, "w") as f:
        f.write(pr_body(ctx, entry))
    created = common.gh("pr", "create", "--base", target, "--head", branch,
                        "--title", title, "--body-file", body_file, check=False)
    url = created.strip().splitlines()[-1] if created.strip() else ""
    if not url.startswith("http"):
        # PR creation failed after the push — delete the just-created remote ref
        # so it isn't left orphaned, leased to the SHA we pushed.
        deleted = git.delete_ref(branch, pushed_sha)
        row["status"] = "error"
        row["detail"] = ("gh pr create failed; pushed branch deleted"
                         if deleted.returncode == 0
                         else "gh pr create failed; branch changed concurrently, left in place")
        return row

    for label in labels:
        common.gh("pr", "edit", url, "--add-label", label, check=False)

    if ctx.get("author") and not ctx["author"].endswith("[bot]"):
        common.gh("pr", "edit", url, "--add-reviewer", ctx["author"], check=False)

    common.gh("pr", "merge", url, "--auto", "--merge")

    row["status"] = status_label
    row["detail"] = url
    return row


def summary_cell(value: str) -> str:
    """Keep agent-authored explanations inside their table cell."""
    return (html.escape(str(value), quote=False).replace("|", "&#124;")
            .replace("\n", " ").replace("\r", " "))


def summary_comment(pr: int, sha: str, rows: list[dict]) -> str:
    lines = ["🤖 Auto-backport summary", "", "| Target | Status | Backport PR |",
             "|---|---|---|"]
    for row in rows:
        detail = str(row["detail"])
        if re.fullmatch(r"https://github.com/[^/]+/[^/]+/pull/[0-9]+", detail):
            cell = f"#{detail.rsplit('/', 1)[-1]}"
        else:
            cell = summary_cell(detail)
        lines.append(f"| `{summary_cell(row['target'])}` | {summary_cell(row['status'])} | {cell} |")
    lines += ["", f"(PR #{pr}, `{sha[:9]}` — generated by the auto-backport workflow.)"]
    return "\n".join(lines)


def main() -> int:
    ctx = json.loads(Path(os.environ["BACKPORT_CONTEXT_FILE"]).read_text())
    manifest_path = os.environ["BACKPORT_MANIFEST_FILE"]
    work = os.environ["BACKPORT_WORK"]
    token = os.environ.get("GH_TOKEN", "")

    if not os.path.exists(manifest_path):
        # The agent step ran (we're past the skip gate) but produced no manifest
        # — a model/tool interruption or output-contract miss. Fail loudly rather
        # than reporting a green no-op that silently opened no backports.
        common.log("ERROR: agent produced no manifest; failing the run.")
        return 1
    manifest = json.loads(Path(manifest_path).read_text())
    entries = manifest.get("targets") or []
    if not isinstance(entries, list):
        common.log("Malformed manifest: 'targets' is not a list.")
        return 1

    # Constructing this treats the agent's clone `.git` as hostile (hooks + local
    # config are stripped) and then installs the push credential — in that order,
    # before any token-holding git call can happen.
    git = common.PrivilegedGit(work, os.environ["GITHUB_REPOSITORY"], token,
                               dry_run=DRY_RUN)

    rows = [apply_target(ctx, git, e) for e in entries if isinstance(e, dict)]

    # Every requested target must be accounted for. A target the manifest omits
    # entirely (a truncated/empty agent output) would otherwise be a silent
    # green no-op — surface it as an error row so it appears in the summary and
    # fails the run.
    covered = {r["target"] for r in rows}
    omitted = [t for t in ctx["targets"] if t not in covered]
    rows += [{"target": t, "status": "error", "detail": "omitted from manifest"}
             for t in omitted]

    if rows and not DRY_RUN:
        comment_file = os.path.join(os.environ["RUNNER_TEMP"], "backport-summary.md")
        with open(comment_file, "w") as f:
            f.write(summary_comment(ctx["pr"], ctx["sha"], rows))
        common.gh("pr", "comment", str(ctx["pr"]), "--body-file", comment_file, check=False)
    elif rows:
        common.log("[dry-run] would comment the summary on the original PR:")
        common.log(summary_comment(ctx["pr"], ctx["sha"], rows))

    common.log("Auto-backport apply summary:")
    for r in rows:
        common.log(f"  {r['target']:<12} {r['status']:<14} {r['detail']}")
    # Fail the run if any requested target was omitted or errored, so a partial /
    # empty manifest isn't reported as success.
    return 1 if any(r["status"] == "error" for r in rows) else 0


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