#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------------------------------------
# Copyright (c) 2026 Huawei Technologies Co., Ltd.
# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
# CANN Open Software License Agreement Version 2.0 (the "License").
# Please refer to the License for details. You may not use this file except in compliance with the License.
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
# See LICENSE in the root of the software repository for the full text of the License.
# -----------------------------------------------------------------------------------------------------------
"""
向 GitCode PR 发布行内评论。

环境变量:
- GITCODE_API_TOKEN

用法:
  python post_pr_inline_comment.py \
    --owner cann \
    --repo runtime \
    --pr 123 \
    --path src/foo.cc \
    --position 120 \
    --body "review comment"
"""

import argparse
import json
import os
import sys

import requests


API_ROOT = "https://api.gitcode.com/api/v5"


def post_inline_comment(
    owner: str,
    repo: str,
    pr_number: int,
    token: str,
    body: str,
    path: str,
    position: int,
    start_position: int | None = None,
) -> dict:
    url = f"{API_ROOT}/repos/{owner}/{repo}/pulls/{pr_number}/comments"
    payload = {
        "body": body,
        "path": path,
        "position": position,
    }
    if start_position is not None:
        payload["start_position"] = start_position

    response = requests.post(
        url,
        params={"access_token": token},
        headers={"Content-Type": "application/json", "Accept": "application/json"},
        json=payload,
        timeout=30,
    )
    response.raise_for_status()
    return response.json()


def main() -> int:
    parser = argparse.ArgumentParser(description="Post GitCode PR inline comment")
    parser.add_argument("--owner", required=True)
    parser.add_argument("--repo", required=True)
    parser.add_argument("--pr", required=True, type=int)
    parser.add_argument("--path", required=True)
    parser.add_argument("--position", required=True, type=int)
    parser.add_argument("--start-position", type=int)
    parser.add_argument("--body", required=True)
    args = parser.parse_args()

    token = os.environ.get("GITCODE_API_TOKEN")
    if not token:
        print("GITCODE_API_TOKEN is required", file=sys.stderr)
        return 1

    try:
        result = post_inline_comment(
            owner=args.owner,
            repo=args.repo,
            pr_number=args.pr,
            token=token,
            body=args.body,
            path=args.path,
            position=args.position,
            start_position=args.start_position,
        )
    except requests.exceptions.RequestException as exc:
        print(f"Failed to post PR inline comment: {exc}", file=sys.stderr)
        return 1
    print(json.dumps(result, ensure_ascii=False, indent=2))
    return 0


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