# gitea-client — Issue operations

load("star/http_helpers.star", "gitea_get", "gitea_post", "gitea_patch")

def get_issues(conn, owner, repo, state):
    """List issues for a repository."""
    state = state or "open"
    path = "/repos/" + owner + "/" + repo + "/issues?state=" + state + "&limit=50&type=issues"
    data = gitea_get(conn, path)
    if data == None:
        return None
    items = []
    for i in data:
        labels = [l.get("name", "") for l in (i.get("labels") or [])]
        items.append({
            "number":     i.get("number", 0),
            "title":      i.get("title", ""),
            "state":      i.get("state", ""),
            "labels":     labels,
            "assignee":   (i.get("assignee") or {}).get("login", ""),
            "created_at": i.get("created_at", ""),
            "updated_at": i.get("updated_at", ""),
            "html_url":   i.get("html_url", ""),
        })
    return items

def get_issue(conn, owner, repo, number):
    """Get full issue details including comments."""
    path = "/repos/" + owner + "/" + repo + "/issues/" + str(number)
    data = gitea_get(conn, path)
    if data == None:
        return None
    comments_data = gitea_get(conn, path + "/comments") or []
    comments = []
    for c in comments_data:
        comments.append({
            "user":       (c.get("user") or {}).get("login", ""),
            "body":       c.get("body", ""),
            "created_at": c.get("created_at", ""),
        })
    return {
        "number":     data.get("number", 0),
        "title":      data.get("title", ""),
        "body":       data.get("body", ""),
        "state":      data.get("state", ""),
        "labels":     [l.get("name", "") for l in (data.get("labels") or [])],
        "assignee":   (data.get("assignee") or {}).get("login", ""),
        "created_at": data.get("created_at", ""),
        "comments":   comments,
    }

def create_issue(conn, owner, repo, title, body, labels):
    """Create a new issue."""
    path = "/repos/" + owner + "/" + repo + "/issues"
    payload = {"title": title}
    if body:
        payload["body"] = body
    if labels:
        payload["labels"] = [l.strip() for l in labels.split(",") if l.strip()]
    data = gitea_post(conn, path, payload)
    if data == None:
        return None
    return {
        "number":   data.get("number", 0),
        "title":    data.get("title", ""),
        "html_url": data.get("html_url", ""),
    }

def update_issue(conn, owner, repo, number, title, body, state):
    """Update an existing issue."""
    path = "/repos/" + owner + "/" + repo + "/issues/" + str(number)
    patch = {}
    if title:
        patch["title"] = title
    if body:
        patch["body"] = body
    if state:
        patch["state"] = state
    data = gitea_patch(conn, path, patch)
    if data == None:
        return None
    return {
        "number": data.get("number", 0),
        "state":  data.get("state", ""),
        "title":  data.get("title", ""),
    }

def add_comment(conn, owner, repo, number, body):
    """Add a comment to an issue."""
    path = "/repos/" + owner + "/" + repo + "/issues/" + str(number) + "/comments"
    data = gitea_post(conn, path, {"body": body})
    if data == None:
        return None
    return {"id": data.get("id", 0), "body": data.get("body", "")}
