#!/usr/bin/env python3
"""
This script downloads issues from a given Gitea repository and produces a pull request for each issue.
It assumes that the default branch (default "main") exists and that you have a valid API token if authentication is required.
"""

import logging
import argparse
import requests
import sys
import dataclasses

logger = logging.getLogger(__name__)


class GiteaClient:

    def __init__(self, gitea_url: str, token: str) -> None:
        self.gitea_url = gitea_url
        self.session = requests.Session()
        self.session.headers["Content-Type"] = "application/json"
        if token:
            self.session.headers["Authorization"] = f"token {token}"

    def get_default_branch_sha(self, owner, repo, branch):
        """Retrieve the commit SHA of the default branch."""
        url = f"{self.gitea_url}/repos/{owner}/{repo}/branches/{branch}"
        response = self.session.get(url)
        response.raise_for_status()
        data = response.json()
        print(data)
        return data['commit']['sha']

    def create_branch(self, owner, repo, new_branch, sha):
        """Create a new branch from the provided SHA."""
        url = f"{self.gitea_url}/repos/{owner}/{repo}/git/refs"
        json_data = {"ref": f"refs/heads/{new_branch}", "sha": sha}
        response = self.session.post(url,  json=json_data)
        if response.status_code == 422:
            print(f"Branch {new_branch} already exists.")
            return False
        response.raise_for_status()
        return True

    def get_issues(self, owner, repo):
        """Download issues from the specified repository."""
        url = f"{self.gitea_url}/repos/{owner}/{repo}/issues"
        response = self.session.get(url)
        response.raise_for_status()
        return response.json()

    def create_pull_request(self, owner, repo, title, head, base, body):
        """Create a pull request for the given branch."""
        url = f"{self.gitea_url}/repos/{owner}/{repo}/pulls"
        json_data = {"title": title, "head": head, "base": base, "body": body}
        response = self.session.post(url, json=json_data)
        response.raise_for_status()
        return response.json()

def parse_args():
    parser = argparse.ArgumentParser(description="Download issues and create pull requests for a Gitea repository.")
    parser.add_argument("--gitea-url", required=True, help="Base URL for the Gitea instance, e.g., https://gitfub.space/api/v1")
    parser.add_argument("--owner", required=True, help="Owner of the repository")
    parser.add_argument("--repo", required=True, help="Repository name")
    parser.add_argument("--base-branch", default="main", help="Base branch to use for new branches (default: main)")
    parser.add_argument("--token", default="", help="Authentication token if required")
    return parser.parse_args()

def main():
    logging.basicConfig()
    args = parse_args()

    client = GiteaClient(args.gitea_url, args.token)


    try:
        issues = client.get_issues(args.owner, args.repo)
    except Exception as e:
        logger.exception('Failed to retrieve issues')
        sys.exit(1)

    if not issues:
        print("No issues found.")
        return

    try:
        base_sha = client.get_default_branch_sha(args.owner, args.repo, args.base_branch)
    except Exception as e:
        logger.exception('Failed to retrieve base branch SHA')
        sys.exit(1)

    for issue in issues:
        issue_number = issue.get("number")
        title = issue.get("title", f"Issue {issue_number}")
        branch_name = f"issue-{issue_number}"
        try:
            created = client.create_branch(args.owner, args.repo, branch_name, base_sha)
            if created:
                print(f"Created branch {branch_name} for issue {issue_number}.")
        except Exception as e:
            print(f"Error creating branch {branch_name}: {e}")
        body = f"Automatically generated pull request for issue: {issue.get('html_url', 'unknown')}"
        try:
            pr = client.create_pull_request(args.owner, args.repo, f"[Issue {issue_number}] {title}", branch_name, args.base_branch, body)
            print(f"Created pull request: {pr.get('html_url', 'unknown')} for issue {issue_number}.")
        except Exception as e:
            print(f"Error creating pull request for branch {branch_name}: {e}")

if __name__ == "__main__":
    main()