import logging
from datetime import datetime
from typing import List, Tuple, Dict, Sequence, Optional, Union, TYPE_CHECKING, Any
from dataclasses import dataclass, fields

from immutabledict import immutabledict

from .utils import get_username, decode_timestamp, encode_timestamp
from .baseapiobject import ReadonlyApiObject, ApiObject
from .exceptions import *

if TYPE_CHECKING:
    from gitea import Gitea


class Organization(ApiObject):
    """see https://try.gitea.io/api/swagger#/organization/orgGetAll"""

    API_OBJECT = """/orgs/{name}"""  # <org>
    ORG_REPOS_REQUEST = """/orgs/%s/repos"""  # <org>
    ORG_TEAMS_REQUEST = """/orgs/%s/teams"""  # <org>
    ORG_TEAMS_CREATE = """/orgs/%s/teams"""  # <org>
    ORG_GET_MEMBERS = """/orgs/%s/members"""  # <org>
    ORG_IS_MEMBER = """/orgs/%s/members/%s"""  # <org>, <username>
    ORG_HEATMAP = """/users/%s/heatmap"""  # <username>
    ORG_LABELS = """/orgs/%s/labels"""

    # Fields of Organisation to enable completion/typechecking
    avatar_url: str
    description: str
    email: str
    full_name: str
    id: int
    location: str
    name: str
    repo_admin_change_team_access: bool
    username: str
    visibility: str
    website: str

    def __init__(self, gitea: "Gitea"):
        super().__init__(gitea)

    def __eq__(self, other: Any) -> bool:
        if not isinstance(other, Organization):
            return False
        return self.gitea == other.gitea and self.username == other.username

    def __hash__(self):
        return hash(self.gitea) ^ hash(self.username)

    @classmethod
    def request(cls, gitea: "Gitea", name: str) -> "Organization":
        return cls._request(gitea, {"name": name})

    _patchable_fields = {
        "description",
        "full_name",
        "location",
        "visibility",
        "website",
    }

    def commit(self):
        values = self.get_dirty_fields()
        args = {"name": self.name}
        self.gitea.requests_patch(Organization.API_OBJECT.format(**args), data=values)
        self._dirty_fields = {}

    def create_repo(
        self,
        repo_name: str,
        description: str = "",
        private: bool = False,
        auto_init: bool = True,
        gitignores: str | None = None,
        license: str | None = None,
        readme: str = "Default",
        issue_labels: str | None = None,
        default_branch: str = "master",
    ):
        """Create an organization Repository

        Throws:
            AlreadyExistsException: If the Repository exists already.
            Exception: If something else went wrong.
        """
        result = self.gitea.requests_post(
            f"/orgs/{self.name}/repos",
            data={
                "name": repo_name,
                "description": description,
                "private": private,
                "auto_init": auto_init,
                "gitignores": gitignores,
                "license": license,
                "issue_labels": issue_labels,
                "readme": readme,
                "default_branch": default_branch,
            },
        )
        if "id" in result:
            self.gitea.logger.info("Successfully created Repository %s " % result["name"])
        else:
            self.gitea.logger.error(result["message"])
            raise Exception("Repository not created... (gitea: %s)" % result["message"])
        return Repository.parse_response(self.gitea, result)

    def get_repositories(self) -> List["Repository"]:
        results = self.gitea.requests_get_paginated(Organization.ORG_REPOS_REQUEST % self.username)
        return [Repository.parse_response(self.gitea, result) for result in results]

    def get_repository(self, name) -> "Repository":
        repos = self.get_repositories()
        for repo in repos:
            if repo.name == name:
                return repo
        raise NotFoundException("Repository %s not existent in organization." % name)

    def get_teams(self) -> List["Team"]:
        results = self.gitea.requests_get(Organization.ORG_TEAMS_REQUEST % self.username)
        teams = [Team.parse_response(self.gitea, result) for result in results]
        # organisation seems to be missing using this request, so we add org manually
        for t in teams:
            setattr(t, "_organization", self)
        return teams

    def get_team(self, name, ignore_case: bool = False) -> "Team":
        teams = self.get_teams()
        for team in teams:
            if (not ignore_case and team.name == name) or (ignore_case and team.name.lower() == name.lower()):
                return team
        raise NotFoundException("Team not existent in organization.")

    def get_members(self) -> List["User"]:
        results = self.gitea.requests_get(Organization.ORG_GET_MEMBERS % self.username)
        return [User.parse_response(self.gitea, result) for result in results]

    def is_member(self, user: "str | User") -> bool:
        username = get_username(user, allow_orgs=False)
        try:
            # returns 204 if its ok, 404 if its not
            self.gitea.requests_get(Organization.ORG_IS_MEMBER % (self.username, username))
            return True
        except:
            return False

    def get_public_members(self):
        path = f"/orgs/{self.name}/public_members"
        return self.gitea.requests_get(path)

    def remove_member(self, user: "User"):
        path = f"/orgs/{self.username}/members/{user.login}"
        self.gitea.requests_delete(path)

    def delete(self):
        """Delete this Organization. Invalidates this Objects data. Also deletes all Repositories owned by the User"""
        for repo in self.get_repositories():
            repo.delete()
        self.gitea.requests_delete(Organization.API_OBJECT.format(name=self.username))
        self.deleted = True

    def get_heatmap(self) -> List[Tuple[datetime, int]]:
        results = self.gitea.requests_get(User.USER_HEATMAP % self.username)
        results = [(datetime.fromtimestamp(result["timestamp"]), result["contributions"]) for result in results]
        return results

    def create_label(
        self,
        name: str,
        color: str,
        description: str = "",
        exclusive: bool = False,
        is_archived: bool = False,
    ) -> "Label":
        result = self.gitea.requests_post(
            f"/orgs/{self.name}/labels",
            data={
                "name": name,
                "color": color,
                "description": description,
                "exclusive": exclusive,
                "is_archived": is_archived,
            },
        )
        if "id" in result:
            self.gitea.logger.info("Successfully created Label %s " % result["name"])
        else:
            self.gitea.logger.error(result["message"])
            raise Exception("Label not created... (gitea: %s)" % result["message"])
        return Label.parse_response(self.gitea, result)

    def get_labels(self) -> List["Label"]:
        results = self.gitea.requests_get(Organization.ORG_LABELS % self.name)
        return [Label.parse_response(self.gitea, result) for result in results]


class User(ApiObject):
    API_OBJECT = """/users/{name}"""  # <org>
    USER_MAIL = """/user/emails?sudo=%s"""  # <name>
    USER_PATCH = """/admin/users/%s"""  # <username>
    ADMIN_DELETE_USER = """/admin/users/%s"""  # <username>
    ADMIN_EDIT_USER = """/admin/users/{username}"""  # <username>
    USER_HEATMAP = """/users/%s/heatmap"""  # <username>

    # Fields of Organisation to enable completion/typechecking
    active: bool
    avatar_url: str
    created: str
    description: str
    email: str
    followers_count: int
    following_count: int
    full_name: str
    html_url: str
    id: int
    is_admin: bool
    language: str
    last_login: str
    location: str
    login: str  # login of the user, same as 'username'
    login_name: str  # identifier of the user, provided by the external authenticator (if configured)
    prohibit_login: bool
    restricted: bool
    source_id: int
    starred_repos_count: int
    visibility: str
    website: str

    def __init__(self, gitea: "Gitea"):
        super().__init__(gitea)
        self._emails = []

    def __eq__(self, other: Any):
        if not isinstance(other, User):
            return False
        return self.gitea == other.gitea and self.id == other.id

    def __hash__(self):
        return hash(self.gitea) ^ hash(self.id)

    @property
    def emails(self):
        self.__request_emails()
        return self._emails

    @classmethod
    def request(cls, gitea: "Gitea", name: str) -> "User":
        api_object = cls._request(gitea, {"name": name})
        return api_object

    _patchable_fields = {
        "active",
        "admin",
        "allow_create_organization",
        "allow_git_hook",
        "allow_import_local",
        "email",
        "full_name",
        "location",
        "login_name",
        "max_repo_creation",
        "must_change_password",
        "password",
        "prohibit_login",
        "website",
    }

    def commit(self, login_name: str, source_id: int = 0):
        """
        Unfortunately it is necessary to require the login name
        as well as the login source (that is not supplied when getting a user) for
        changing a user.
        Usually source_id is 0 and the login_name is equal to the username.
        """
        values = self.get_dirty_fields()
        values.update(
            # api-doc says that the "source_id" is necessary; works without though
            {"login_name": login_name, "source_id": source_id}
        )
        args = {"username": self.login}
        self.gitea.requests_patch(User.ADMIN_EDIT_USER.format(**args), data=values)
        self._dirty_fields = {}

    def create_repo(
        self,
        repoName: str,
        description: str = "",
        private: bool = False,
        autoInit: bool = True,
        gitignores: str | None = None,
        license: str | None = None,
        readme: str = "Default",
        issue_labels: str | None = None,
        default_branch: str = "master",
    ):
        """Create a user Repository

        Throws:
            AlreadyExistsException: If the Repository exists already.
            Exception: If something else went wrong.
        """
        result = self.gitea.requests_post(
            "/user/repos",
            data={
                "name": repoName,
                "description": description,
                "private": private,
                "auto_init": autoInit,
                "gitignores": gitignores,
                "license": license,
                "issue_labels": issue_labels,
                "readme": readme,
                "default_branch": default_branch,
            },
        )
        if "id" in result:
            self.gitea.logger.info("Successfully created Repository %s " % result["name"])
        else:
            self.gitea.logger.error(result["message"])
            raise Exception("Repository not created... (gitea: %s)" % result["message"])
        return Repository.parse_response(self.gitea, result)

    def get_repositories(self) -> List["Repository"]:
        """Get all Repositories owned by this User."""
        url = f"/users/{self.login}/repos"
        results = self.gitea.requests_get_paginated(url)
        return [Repository.parse_response(self.gitea, result) for result in results]

    def get_orgs(self) -> List[Organization]:
        """Get all Organizations this user is a member of."""
        url = f"/users/{self.login}/orgs"
        results = self.gitea.requests_get_paginated(url)
        return [Organization.parse_response(self.gitea, result) for result in results]

    def get_teams(self) -> List["Team"]:
        url = f"/user/teams"
        results = self.gitea.requests_get_paginated(url, sudo=self)
        return [Team.parse_response(self.gitea, result) for result in results]

    def get_accessible_repos(self) -> List["Repository"]:
        """Get all Repositories accessible by the logged in User."""
        results = self.gitea.requests_get("/user/repos", sudo=self)
        return [Repository.parse_response(self.gitea, result) for result in results]

    def __request_emails(self):
        result = self.gitea.requests_get(User.USER_MAIL % self.login)
        # report if the adress changed by this
        for mail in result:
            self._emails.append(mail["email"])
            if mail["primary"]:
                self._email = mail["email"]

    def delete(self):
        """Deletes this User. Also deletes all Repositories he owns."""
        self.gitea.requests_delete(User.ADMIN_DELETE_USER % self.login)
        self.deleted = True

    def get_heatmap(self) -> List[Tuple[datetime, int]]:
        results = self.gitea.requests_get(User.USER_HEATMAP % self.login)
        results = [(datetime.fromtimestamp(result["timestamp"]), result["contributions"]) for result in results]
        return results


class Branch(ReadonlyApiObject):

    # Fields of Branch to enable completion/typechecking
    commit: Any
    effective_branch_protection_name: str
    enable_status_check: bool
    name: str
    protected: bool
    required_approvals: int
    status_check_contexts: Any
    user_can_merge: bool
    user_can_push: bool

    def __init__(self, gitea: "Gitea"):
        super().__init__(gitea)

    def __eq__(self, other: Any):
        if not isinstance(other, Branch):
            return False
        return self.commit == other.commit and self.name == other.name

    def __hash__(self):
        return hash(self.commit["id"]) ^ hash(self.name)

    _fields_to_parsers = {
        # This is not a commit object
        # "commit": lambda gitea, c: Commit.parse_response(gitea, c)
    }

    @classmethod
    def request(cls, gitea: "Gitea", owner: str, repo: str, ref: str):
        return cls._request(gitea, {"owner": owner, "repo": repo, "ref": ref})


class Tag(ReadonlyApiObject):

    commit: Any
    id: int
    message: str
    name: str
    tarball_url: str
    zipball_url: str

    def __init__(self, gitea):
        super().__init__(gitea)

    def __eq__(self, other: Any):
        if not isinstance(other, Tag):
            return False
        return self.id == other.id and self.name == other.name

    def __hash__(self):
        return hash(self.id) ^ hash(self.name)

    @classmethod
    def request(cls, gitea: "Gitea", owner: str, repo: str, ref: str):
        return cls._request(gitea, {"owner": owner, "repo": repo, "ref": ref})


class Repository(ApiObject):
    API_OBJECT = """/repos/{owner}/{name}"""  # <owner>, <reponame>
    REPO_MIGRATE = """/repos/migrate"""
    REPO_IS_COLLABORATOR = """/repos/%s/%s/collaborators/%s"""  # <owner>, <reponame>, <username>
    REPO_PERMISSION = """/repos/%s/%s/collaborators/%s/permission"""
    REPO_SEARCH = """/repos/search/%s"""  # <reponame>
    REPO_BRANCHES = """/repos/%s/%s/branches"""  # <owner>, <reponame>
    REPO_TAGS = """/repos/%s/%s/tags"""  # <owner>, <reponame>
    REPO_ISSUES = """/repos/{owner}/{repo}/issues"""  # <owner, reponame>
    REPO_DELETE = """/repos/%s/%s"""  # <owner>, <reponame>
    REPO_TIMES = """/repos/%s/%s/times"""  # <owner>, <reponame>
    REPO_TOPICS = """/repos/%s/%s/topics"""  # <owner, <reponame>
    REPO_TOPIC = """/repos/%s/%s/topics/%s"""  # <owner, <reponame>, <topicname>
    REPO_USER_TIME = """/repos/%s/%s/times/%s"""  # <owner>, <reponame>, <username>
    REPO_COMMITS = "/repos/%s/%s/commits"  # <owner>, <reponame>
    REPO_TRANSFER = "/repos/{owner}/{repo}/transfer"
    REPO_MILESTONES = """/repos/{owner}/{repo}/milestones"""
    REPO_LABELS = """/repos/%s/%s/labels"""

    # Fields for typechecking
    allow_fast_forward_only_merge: bool
    allow_manual_merge: bool
    allow_merge_commits: bool
    allow_rebase: bool
    allow_rebase_explicit: bool
    allow_rebase_update: bool
    allow_squash_merge: bool
    archived: bool
    archived_at: datetime
    autodetect_manual_merge: Any
    avatar_url: str
    branch_count: int
    clone_url: Any
    created_at: Any
    default_allow_maintainer_edit: Any
    default_branch: Any
    default_delete_branch_after_merge: Any
    default_merge_style: Any
    # default_target_branch: Any
    description: Any
    empty: Any
    external_tracker: Any
    external_wiki: Any
    fork: Any
    forks_count: Any
    full_name: Any
    has_actions: Any
    has_code: Any
    has_issues: Any
    has_packages: Any
    has_projects: Any
    has_pull_requests: Any
    has_releases: Any
    has_wiki: Any
    html_url: Any
    id: Any
    ignore_whitespace_conflicts: Any
    internal: Any
    internal_tracker: Any
    language: Any
    languages_url: Any
    licenses: Any
    link: Any
    mirror: Any
    mirror_interval: Any
    mirror_updated: Any
    name: Any
    object_format_name: Any
    open_issues_count: Any
    open_pr_counter: Any
    original_url: Any
    owner: "User"
    # parent: Any
    permissions: Any
    private: Any
    projects_mode: Any
    release_counter: Any
    # repo_transfer: Any
    size: Any
    ssh_url: Any
    stars_count: Any
    template: Any
    topics: Any
    updated_at: Any
    url: Any
    watchers_count: Any
    website: Any

    def __init__(self, gitea: "Gitea"):
        super().__init__(gitea)

    def __eq__(self, other: Any):
        if not isinstance(other, Repository):
            return False
        return self.owner == other.owner and self.name == other.name

    def __hash__(self):
        return hash(self.owner) ^ hash(self.name)

    _fields_to_parsers = {
        # dont know how to tell apart user and org as owner except form email being empty.
        "owner": lambda gitea, r: (
            Organization.parse_response(gitea, r) if r["email"] == "" else User.parse_response(gitea, r)
        ),
        "updated_at": lambda gitea, t: decode_timestamp(t),
        "archived_at": lambda gitea, t: decode_timestamp(t),
    }

    @classmethod
    def request(cls, gitea: "Gitea", owner: "str | User | Organization", name: str):
        return cls._request(gitea, {"owner": get_username(owner), "name": name})

    _patchable_fields = {
        "allow_manual_merge",
        "allow_merge_commits",
        "allow_rebase",
        "allow_rebase_explicit",
        "allow_rebase_update",
        "allow_squash_merge",
        "archived",
        "autodetect_manual_merge",
        "default_branch",
        "default_delete_branch_after_merge",
        "default_merge_style",
        "description",
        "enable_prune",
        "external_tracker",
        "external_wiki",
        "has_actions",
        "has_issues",
        "has_projects",
        "has_pull_requests",
        "has_wiki",
        "has_releases",
        "has_actions",
        "has_packages",
        "ignore_whitespace_conflicts",
        "internal_tracker",
        "mirror_interval",
        "name",
        "private",
        "template",
        "website",
    }

    def commit(self):
        values = self.get_dirty_fields()
        args = {"owner": get_username(self.owner), "name": self.name}
        self.gitea.requests_patch(self.API_OBJECT.format(**args), data=values)
        self._dirty_fields = {}

    def get_branches(self) -> List["Branch"]:
        """Get all the Branches of this Repository."""
        results = self.gitea.requests_get_paginated(Repository.REPO_BRANCHES % (get_username(self.owner), self.name))
        return [Branch.parse_response(self.gitea, result) for result in results]

    def add_branch(self, create_from: Branch, newname: str) -> "Branch":
        """Add a branch to the repository"""
        # Note: will only work with gitea 1.13 or higher!
        data = {"new_branch_name": newname, "old_branch_name": create_from.name}
        result = self.gitea.requests_post(Repository.REPO_BRANCHES % (get_username(self.owner), self.name), data=data)
        return Branch.parse_response(self.gitea, result)

    def get_tags(self) -> List["Tag"]:
        """Get all the Tags of this Repository."""
        results = self.gitea.requests_get_paginated(Repository.REPO_TAGS % (get_username(self.owner), self.name))
        return [Tag.parse_response(self.gitea, result) for result in results]

    def add_tag(self, tag_name: str, target: str, message: str) -> "Tag":
        """Add a tag to the repository ('target' is commit/hash)"""
        data = {"tag_name": tag_name, "target": target, "message": message}
        result = self.gitea.requests_post(Repository.REPO_TAGS % (get_username(self.owner), self.name), data=data)
        return Tag.parse_response(self.gitea, result)

    def get_issues(self) -> List["Issue"]:
        """Get all Issues of this Repository (open and closed)"""
        return self.get_issues_state(Issue.OPENED) + self.get_issues_state(Issue.CLOSED)

    def get_commits(self, page_limit: int = 0) -> List["Commit"]:
        """Get all the Commits of this Repository."""
        try:
            results = self.gitea.requests_get_paginated(
                Repository.REPO_COMMITS % (get_username(self.owner), self.name),
                page_limit=page_limit,
            )
        except ConflictException as err:
            logging.warning(err)
            logging.warning("Repository %s/%s is Empty" % (get_username(self.owner), self.name))
            results = []
        return [Commit.parse_response(self.gitea, result) for result in results]

    def get_issues_state(self, state) -> List["Issue"]:
        """Get issues of state Issue.open or Issue.closed of a repository."""
        assert state in [Issue.OPENED, Issue.CLOSED]
        issues = []
        data = immutabledict({"state": state})
        results = self.gitea.requests_get_paginated(
            Repository.REPO_ISSUES.format(owner=get_username(self.owner), repo=self.name),
            params=data,
        )
        for result in results:
            issue = Issue.parse_response(self.gitea, result)
            # adding data not contained in the issue answer
            Issue._add_read_property("repo", self, issue)
            Issue._add_read_property("owner", self.owner, issue)
            issues.append(issue)
        return issues

    def get_milestones(self, state: str = "all") -> list["Milestone"]:
        assert state is "open" or state is "closed" or state is "all"
        result = self.gitea.requests_get_paginated(
            f"/repos/{get_username(self.owner)}/{self.name}/milestones",
            params=immutabledict({"state": state}),
        )
        inject = {"repo": self}
        return [Milestone.parse_response(self.gitea, result, inject) for result in result]

    def get_times(self):
        results = self.gitea.requests_get(Repository.REPO_TIMES % (get_username(self.owner), self.name))
        return results

    def get_topics(self) -> list[str]:
        results = self.gitea.requests_get(Repository.REPO_TOPICS % (get_username(self.owner), self.name))
        return results["topics"]

    def add_topic(self, topic: str):
        """Add a topic to the repository"""
        self.gitea.requests_put(Repository.REPO_TOPIC % (get_username(self.owner), self.name, topic))

    def del_topic(self, topic: str):
        """Delete a topic to the repository"""
        self.gitea.requests_delete(Repository.REPO_TOPIC % (get_username(self.owner), self.name, topic))

    def get_user_time(self, user: User | str) -> float | int:
        user = get_username(user)
        results = self.gitea.requests_get(Repository.REPO_USER_TIME % (get_username(self.owner), self.name, user))
        time = sum(r["time"] for r in results)
        return time

    def get_full_name(self) -> str:
        return get_username(self.owner) + "/" + self.name

    def create_issue(self, title, assignees: set[User] | None = None, description: str = "") -> ApiObject:
        assignees = assignees if assignees else set()
        # TODO: unify with the ISSUE variant of this
        data = {
            "assignees": list(assignees),
            "body": description,
            "closed": False,
            "title": title,
        }
        result = self.gitea.requests_post(
            Repository.REPO_ISSUES.format(owner=get_username(self.owner), repo=self.name),
            data=data,
        )
        return Issue.parse_response(self.gitea, result)

    def create_milestone(
        self, title: str, description: str, due_date: str | None = None, state: str = "open"
    ) -> "Milestone":
        url = Repository.REPO_MILESTONES.format(owner=get_username(self.owner), repo=self.name)
        data = {"title": title, "description": description, "state": state}
        if due_date:
            data["due_date"] = due_date
        result = self.gitea.requests_post(url, data=data)
        return Milestone.parse_response(self.gitea, result)

    def create_gitea_hook(self, hook_url: str, events: List[str]):
        url = f"/repos/{get_username(self.owner)}/{self.name}/hooks"
        data = {
            "type": "gitea",
            "config": {"content_type": "json", "url": hook_url},
            "events": events,
            "active": True,
        }
        return self.gitea.requests_post(url, data=data)

    def list_hooks(self):
        url = f"/repos/{get_username(self.owner)}/{self.name}/hooks"
        return self.gitea.requests_get(url)

    def delete_hook(self, id: str):
        url = f"/repos/{get_username(self.owner)}/{self.name}/hooks/{id}"
        self.gitea.requests_delete(url)

    def is_collaborator(self, user: str | User) -> bool:
        # TODO: Check this, this does not look like a proper request
        try:
            # returns 204 if its ok, 404 if its not
            self.gitea.requests_get(
                Repository.REPO_IS_COLLABORATOR
                % (get_username(self.owner), self.name, get_username(user, allow_orgs=False))
            )
            return True
        except:
            return False

    def get_user_permission(self, username) -> "UserRepoPermission":
        url = Repository.REPO_PERMISSION % (
            get_username(self.owner),
            self.name,
            get_username(username, allow_orgs=False),
        )
        response = self.gitea.requests_get(url)
        perm = UserRepoPermission.parse_response(self.gitea, response)
        return perm

    def get_users_with_access(self) -> Sequence[User]:
        url = f"/repos/{get_username(self.owner)}/{self.name}/collaborators"
        response = self.gitea.requests_get(url)
        collabs = [User.parse_response(self.gitea, user) for user in response]
        if isinstance(self.owner, User):
            return collabs + [self.owner]
        else:
            # owner must be org
            teams = self.owner.get_teams()
            for team in teams:
                team_repos = team.get_repos()
                if self.name in [n.name for n in team_repos]:
                    collabs += team.get_members()
            return collabs

    def remove_collaborator(self, user_name: str):
        url = f"/repos/{get_username(self.owner)}/{self.name}/collaborators/{user_name}"
        self.gitea.requests_delete(url)

    def transfer_ownership(
        self,
        new_owner: Union["User", "Organization"],
        new_teams: frozenset["Team"] = frozenset(),
    ):
        url = Repository.REPO_TRANSFER.format(owner=get_username(self.owner), repo=self.name)
        data = {"new_owner": get_username(new_owner)}
        if isinstance(new_owner, Organization):
            new_team_ids = [team.id for team in new_teams if team in new_owner.get_teams()]
            data["team_ids"] = new_team_ids
        self.gitea.requests_post(url, data=data)
        # TODO: make sure this instance is either updated or discarded

    def get_git_content(self, commit: "Commit | None" = None) -> List["Content"]:
        """https://try.gitea.io/api/swagger#/repository/repoGetContentsList"""
        url = f"/repos/{get_username(self.owner)}/{self.name}/contents"
        data = {"ref": commit.sha} if commit else {}
        result = [
            Content.parse_response(self.gitea, f, inject_fields={"repo": self})
            for f in self.gitea.requests_get(url, data)
        ]
        return result

    def get_file_content(self, content: "Content", commit: "Commit | None" = None) -> Union[str, List["Content"]]:
        """https://try.gitea.io/api/swagger#/repository/repoGetContents"""
        url = f"/repos/{get_username(self.owner)}/{self.name}/contents/{content.path}"
        data = {"ref": commit.sha} if commit else {}
        if content.type == Content.FILE:
            return self.gitea.requests_get(url, data)["content"]
        else:
            return [Content.parse_response(self.gitea, f) for f in self.gitea.requests_get(url, data)]

    def create_file(self, file_path: str, content: str, data: dict | None = None):
        """https://try.gitea.io/api/swagger#/repository/repoCreateFile"""
        if not data:
            data = {}
        url = f"/repos/{get_username(self.owner)}/{self.name}/contents/{file_path}"
        data.update({"content": content})
        return self.gitea.requests_post(url, data)

    def change_file(self, file_path: str, file_sha: str, content: str, data: dict | None = None):
        """https://try.gitea.io/api/swagger#/repository/repoCreateFile"""
        if not data:
            data = {}
        url = f"/repos/{get_username(self.owner)}/{self.name}/contents/{file_path}"
        data.update({"sha": file_sha, "content": content})
        return self.gitea.requests_put(url, data)

    def delete_file(self, file_path: str, file_sha: str, data: dict | None = None):
        """https://try.gitea.io/api/swagger#/repository/repoCreateFile"""
        if not data:
            data = {}
        url = f"/repos/{get_username(self.owner)}/{self.name}/contents/{file_path}"
        data.update({"sha": file_sha})
        return self.gitea.requests_delete(url, data)

    def delete(self):
        self.gitea.requests_delete(Repository.REPO_DELETE % (get_username(self.owner), self.name))
        self.deleted = True

    def create_label(
        self,
        name: str,
        color: str,
        description: str = "",
        exclusive: bool = False,
        is_archived: bool = False,
    ) -> "Label":
        result = self.gitea.requests_post(
            f"/repos/{get_username(self.owner)}/{self.name}/labels",
            data={
                "name": name,
                "color": color,
                "description": description,
                "exclusive": exclusive,
                "is_archived": is_archived,
            },
        )
        if "id" in result:
            self.gitea.logger.info("Successfully created Label %s " % result["name"])
        else:
            self.gitea.logger.error(result["message"])
            raise Exception("Label not created... (gitea: %s)" % result["message"])
        return Label.parse_response(self.gitea, result)

    def get_labels(self) -> List["Label"]:
        results = self.gitea.requests_get(Repository.REPO_LABELS % (get_username(self.owner), self.name))
        return [Label.parse_response(self.gitea, result) for result in results]

    @classmethod
    def migrate_repo(
        cls,
        gitea: "Gitea",
        service: str,
        clone_addr: str,
        repo_name: str,
        description: str = "",
        private: bool = False,
        auth_token: str | None = None,
        auth_username: str | None = None,
        auth_password: str | None = None,
        mirror: bool = False,
        mirror_interval: str | None = None,
        lfs: bool = False,
        lfs_endpoint: str = "",
        wiki: bool = False,
        labels: bool = False,
        issues: bool = False,
        pull_requests: bool = False,
        releases: bool = False,
        milestones: bool = False,
        repo_owner: str | None = None,
    ):
        """Migrate a Repository from another service.

        Throws:
            AlreadyExistsException: If the Repository exists already.
            Exception: If something else went wrong.
        """
        result = gitea.requests_post(
            cls.REPO_MIGRATE,
            data={
                "auth_password": auth_password,
                "auth_token": auth_token,
                "auth_username": auth_username,
                "clone_addr": clone_addr,
                "description": description,
                "issues": issues,
                "labels": labels,
                "lfs": lfs,
                "lfs_endpoint": lfs_endpoint,
                "milestones": milestones,
                "mirror": mirror,
                "mirror_interval": mirror_interval,
                "private": private,
                "pull_requests": pull_requests,
                "releases": releases,
                "repo_name": repo_name,
                "repo_owner": repo_owner,
                "service": service,
                "wiki": wiki,
            },
        )
        if "id" in result:
            gitea.logger.info("Successfully created Job to Migrate Repository %s " % result["name"])
        else:
            gitea.logger.error(result["message"])
            raise Exception("Repository not Migrated... (gitea: %s)" % result["message"])
        return Repository.parse_response(gitea, result)


class Milestone(ApiObject):
    API_OBJECT = """/repos/{owner}/{repo}/milestones/{id}"""

    OPEN = "open"
    CLOSED = "closed"

    # Fields for typechecking
    closed_at: datetime
    closed_issues: int
    created_at: datetime
    description: str
    due_on: datetime
    id: int
    open_issues: int
    state: str
    title: str
    updated_at: datetime

    # automatically added (but not an api field)
    repo: "Repository"

    def __init__(self, gitea: "Gitea"):
        super().__init__(gitea)

    def __eq__(self, other: "ApiObject"):
        if not isinstance(other, Milestone):
            return False
        return self.gitea == other.gitea and self.id == other.id

    def __hash__(self):
        return hash(self.gitea) ^ hash(self.id)

    _fields_to_parsers = {
        "closed_at": lambda gitea, t: decode_timestamp(t),
        "created_at": lambda gitea, t: decode_timestamp(t),
        "due_on": lambda gitea, t: decode_timestamp(t),
        "updated_at": lambda gitea, t: decode_timestamp(t),
    }

    _parsers_to_fields = {
        "closed_at": lambda m: encode_timestamp(m),
        "created_at": lambda m: encode_timestamp(m),
        "due_on": lambda m: encode_timestamp(m),
        "updated_at": lambda m: encode_timestamp(m),
    }

    _patchable_fields = {
        "description",
        "due_on",
        "state",
        "title",
    }

    @classmethod
    def request(cls, gitea: "Gitea", owner: User | Organization, repo: Repository, id: str) -> "Milestone":
        api_object = cls._request(gitea, {"owner": get_username(owner), "repo": repo.id, "id": id})
        api_object.repo = repo
        return api_object

    def commit(self):
        values = self.get_dirty_fields()
        args = {"owner": get_username(self.repo.owner), "repo": self.repo.name, "id": self.id}
        self.gitea.requests_patch(self.API_OBJECT.format(**args), data=values)
        self._dirty_fields = {}


class Comment(ApiObject):

    assets: Any
    body: str
    created_at: datetime
    html_url: str
    id: int
    issue_url: str
    original_author: "User"
    original_author_id: int
    pull_request_url: str
    updated_at: datetime
    user: "User"

    # added
    repo: "Repository"

    def __init__(self, gitea: "Gitea"):
        super().__init__(gitea)

    def __eq__(self, other: Any):
        if not isinstance(other, Comment):
            return False
        return self.repo == other.repo and self.id == other.id

    def __hash__(self):
        return hash(self.repo) ^ hash(self.id)

    _fields_to_parsers = {
        "user": lambda gitea, r: User.parse_response(gitea, r),
        "original_author": lambda gitea, r: User.parse_response(gitea, r),
        "created_at": lambda gitea, t: decode_timestamp(t),
        "updated_at": lambda gitea, t: decode_timestamp(t),
        "closed_at": lambda gitea, t: decode_timestamp(t),
    }


class Commit(ReadonlyApiObject):

    author: dict[str, Any]
    commit: dict[str, Any]
    committer: dict[str, Any]
    created: datetime
    files: Any
    html_url: str
    parents: str
    sha: str
    stats: Any
    url: str

    def __init__(self, gitea: "Gitea"):
        super().__init__(gitea)

    _fields_to_parsers = {
        # NOTE: api may return None for commiters that are no gitea users
        # "author": lambda gitea, u: User.parse_response(gitea, u) if u else None,
        "created": lambda gitea, t: decode_timestamp(t),
    }

    def __eq__(self, other: Any):
        if not isinstance(other, Commit):
            return False
        return self.sha == other.sha

    def __hash__(self):
        return hash(self.sha)

    @classmethod
    def parse_response(cls, gitea, result) -> "Commit":
        commit_cache = result["commit"]
        api_object = cls(gitea)
        # inner_commit for legacy reasons
        cls._initialize(gitea, api_object, result, inject_fields={"inner_commit": commit_cache})
        return api_object


class Issue(ApiObject):
    API_OBJECT = """/repos/{owner}/{repo}/issues/{index}"""  # <owner, repo, index>
    GET_TIME = """/repos/%s/%s/issues/%s/times"""  # <owner, repo, index>
    GET_COMMENTS = """/repos/%s/%s/issues/comments"""
    CREATE_ISSUE = """/repos/{owner}/{repo}/issues"""
    API_LABELS = """/repos/{owner}/{repo}/issues/{index}/labels"""

    OPENED = "open"
    CLOSED = "closed"

    # Fields of Issue for type checking
    assets: Any
    assignee: "User | None"
    assignees: list["User"]
    body: str
    closed_at: datetime
    comments: list["Comment"]
    content_version: Any
    created_at: datetime
    due_date: datetime
    html_url: str
    id: int
    is_locked: bool
    labels: list["Label"]
    milestone: "Milestone| None"
    number: int
    original_author: User
    original_author_id: int
    pin_order: int | None
    pull_request: str | None
    ref: Any
    repository: Repository
    state: str
    time_estimate: int
    title: str
    updated_at: datetime
    url: str
    user: User

    def __init__(self, gitea):
        super().__init__(gitea)

    def __eq__(self, other):
        if not isinstance(other, Issue):
            return False
        return self.repository == other.repository and self.id == other.id

    def __hash__(self):
        return hash(self.repository) ^ hash(self.id)

    _fields_to_parsers = {
        "milestone": lambda gitea, m: Milestone.parse_response(gitea, m),
        "user": lambda gitea, u: User.parse_response(gitea, u),
        "assignee": lambda gitea, u: User.parse_response(gitea, u),
        "assignees": lambda gitea, us: [User.parse_response(gitea, u) for u in us],
        "state": lambda gitea, s: Issue.CLOSED if s == "closed" else Issue.OPENED,
        # Repository in this request is just a "RepositoryMeta" record, thus request whole object
        "repository": lambda gitea, r: Repository.request(gitea, r["owner"], r["name"]),
        "labels": lambda gitea, ls: [Label.parse_response(gitea, l) for l in ls],
        "created_at": lambda gitea, t: decode_timestamp(t),
        "closed_at": lambda gitea, t: decode_timestamp(t),
        "updated_at": lambda gitea, t: decode_timestamp(t),
        "due_date": lambda gitea, t: decode_timestamp(t),
    }

    _parsers_to_fields = {
        "milestone": lambda m: m.id,
        "due_date": lambda m: encode_timestamp(m),
    }

    _patchable_fields = {
        "assignee",
        "assignees",
        "body",
        "due_date",
        "milestone",
        "state",
        "title",
    }

    def commit(self):
        values = self.get_dirty_fields()
        args = {
            "owner": get_username(self.repository.owner),
            "repo": self.repository.name,
            "index": self.number,
        }
        self.gitea.requests_patch(Issue.API_OBJECT.format(**args), data=values)
        self._dirty_fields = {}

    @classmethod
    def request(cls, gitea: "Gitea", owner: str, repo: str, number: str):
        api_object = cls._request(gitea, {"owner": owner, "repo": repo, "index": number})
        return api_object

    @classmethod
    def create_issue(cls, gitea, repo: Repository, title: str, body: str = "") -> "Issue":
        args = {"owner": get_username(repo.owner), "repo": repo.name}
        data = {"title": title, "body": body}
        result = gitea.requests_post(Issue.CREATE_ISSUE.format(**args), data=data)
        return Issue.parse_response(gitea, result)

    def get_time_sum(self, user: User) -> int:
        results = self.gitea.requests_get(
            Issue.GET_TIME % (get_username(self.repository.owner), self.repository.name, self.number)
        )
        return sum(result["time"] for result in results if result and result["user_id"] == user.id)

    def get_times(self) -> Optional[Dict]:
        return self.gitea.requests_get(
            Issue.GET_TIME % (get_username(self.repository.owner), self.repository.name, self.number)
        )

    def delete_time(self, time_id: str):
        path = (
            f"/repos/{get_username(self.repository.owner)}/{self.repository.name}/issues/{self.number}/times/{time_id}"
        )
        self.gitea.requests_delete(path)

    def add_time(self, time: int, created: str | None = None, user_name: User | None = None):
        path = f"/repos/{get_username(self.repository.owner)}/{self.repository.name}/issues/{self.number}/times"
        self.gitea.requests_post(path, data={"created": created, "time": int(time), "user_name": user_name})

    def get_comments(self) -> List[ApiObject]:
        results = self.gitea.requests_get(
            Issue.GET_COMMENTS % (get_username(self.repository.owner), self.repository.name)
        )
        allProjectComments = [Comment.parse_response(self.gitea, result) for result in results]
        # Comparing the issue id with the URL seems to be the only (!) way to get to the comments of one issue
        return [comment for comment in allProjectComments if comment.issue_url.endswith("/" + str(self.number))]

    def set_labels(self, labels: List["Label"]):
        args = {
            "owner": get_username(self.repository.owner),
            "repo": self.repository.name,
            "index": self.number,
        }
        self.gitea.requests_put(Issue.API_LABELS.format(**args), data={"labels": [l.id for l in labels]})


class Team(ApiObject):
    API_OBJECT = """/teams/{id}"""  # <id>
    ADD_REPO = """/teams/%s/repos/%s/%s"""  # <id, org, repo>
    TEAM_DELETE = """/teams/%s"""  # <id>
    GET_MEMBERS = """/teams/%s/members"""  # <id>
    GET_REPOS = """/teams/%s/repos"""  # <id>

    can_create_org_repo: bool
    description: str
    id: int
    includes_all_repositories: bool
    name: str
    organization: Organization
    permission: str
    units: list[str]
    units_map: dict[str, str]

    def __init__(self, gitea: "Gitea"):
        super().__init__(gitea)

    def __eq__(self, other: Any):
        if not isinstance(other, Team):
            return False
        return self.organization == other.organization and self.id == other.id

    def __hash__(self):
        return hash(self.organization) ^ hash(self.id)

    _fields_to_parsers = {
        "organization": lambda gitea, o: Organization.parse_response(gitea, o),
        "units_map": lambda gitea, o: RepoUnits.from_dict(o),
    }

    _parsers_to_fields = {
        "units_map": lambda m: RepoUnits.to_dict(m),
    }

    _patchable_fields = {
        "can_create_org_repo",
        "description",
        "includes_all_repositories",
        "name",
        "permission",
        "units",
        "units_map",
    }

    @classmethod
    def request(cls, gitea: "Gitea", id: int):
        return cls._request(gitea, {"id": id})

    def commit(self):
        values = self.get_dirty_fields()
        args = {"id": self.id}
        self.gitea.requests_patch(self.API_OBJECT.format(**args), data=values)
        self._dirty_fields = {}

    def add_user(self, user: User):
        """https://try.gitea.io/api/swagger#/organization/orgAddTeamMember"""
        url = f"/teams/{self.id}/members/{user.login}"
        self.gitea.requests_put(url)

    def add_repo(self, org: Organization, repo: Repository):
        self.gitea.requests_put(Team.ADD_REPO % (self.id, org, repo.name))

    def get_members(self):
        """Get all users assigned to the team."""
        results = self.gitea.requests_get(Team.GET_MEMBERS % self.id)
        return [User.parse_response(self.gitea, result) for result in results]

    def get_repos(self):
        """Get all repos of this Team."""
        results = self.gitea.requests_get(Team.GET_REPOS % self.id)
        return [Repository.parse_response(self.gitea, result) for result in results]

    def delete(self):
        self.gitea.requests_delete(Team.TEAM_DELETE % self.id)
        self.deleted = True

    def remove_team_member(self, user_name: str):
        url = f"/teams/{self.id}/members/{user_name}"
        self.gitea.requests_delete(url)


class Content(ReadonlyApiObject):
    FILE = "file"

    content: str
    download_url: str
    encoding: str
    git_url: str
    html_url: str
    last_author_date: str
    # last_commit_message: str
    last_commit_sha: str
    last_committer_date: str
    # lfs_oid: str
    # lfs_size: str
    name: str
    path: str
    sha: str
    size: int
    submodule_git_url: str
    target: str
    type: str
    url: str

    # automatically added fields
    repo: "Repository"

    def __init__(self, gitea: "Gitea"):
        super().__init__(gitea)

    def __eq__(self, other: Any):
        if not isinstance(other, Content):
            return False
        return self.repo == other.repo and self.sha == other.sha and self.name == other.name

    def __hash__(self):
        return hash(self.repo) ^ hash(self.sha) ^ hash(self.name)


class Label(ApiObject):
    API_OBJECT_REPO = """/repos/{owner}/{repo}/labels/{id}"""
    API_OBJECT_ORG = """/orgs/{org}/labels/{id}"""

    # Fields of Label for Typechecking
    color: str
    description: str
    exclusive: bool
    id: int
    is_archived: bool
    name: str
    url: str

    # automatically added
    # TODO: owner?

    def __init__(self, gitea):
        super().__init__(gitea)

    def __eq__(self, other):
        if not isinstance(other, Label):
            return False
        return self.id == other.id

    def __hash__(self):
        return hash(self.id)

    _fields_to_parsers = {
        "color": lambda gitea, o: o,
        "description": lambda gitea, o: o,
        "excluses": lambda gitea, o: bool(o),
        "id": lambda gitea, o: int(o),
        "is_archived": lambda gitea, o: bool(o),
        "name": lambda gitea, o: o,
    }

    _patchable_fields = {"color", "description", "exclusive", "is_archived", "name"}

    @classmethod
    def request(cls, gitea: "Gitea", owner: str, repo: str, id: int):
        if repo is None:
            obj = cls._request(gitea, {"org": owner, "id": id})
            obj.org_name = owner
            obj.owner_name = None
            obj.repo_name = None
        else:
            obj = cls._request(gitea, {"owner": owner, "repo": repo, "id": id})
            obj.org_name = None
            obj.owner_name = owner
            obj.repo_name = repo
        return obj

    @classmethod
    def _get_gitea_api_object(cls, gitea, args):
        if "org" in args:
            return gitea.requests_get(cls.API_OBJECT_ORG.format(**args))
        else:
            return gitea.requests_get(cls.API_OBJECT_REPO.format(**args))

    def commit(self):
        values = self.get_dirty_fields()
        url_copy = self.url
        endpoint = url_copy.replace(self.gitea.url + "/api/v1/", "")
        endpoint_list = endpoint.split("/")
        if endpoint_list[0] == "orgs":
            args = {"org": endpoint_list[1], "id": self.id}
            self.gitea.requests_patch(self.API_OBJECT_ORG.format(**args), data=values)
        else:
            args = {"owner": endpoint_list[1], "repo": endpoint_list[2], "id": self.id}
            self.gitea.requests_patch(self.API_OBJECT_REPO.format(**args), data=values)
        self._dirty_fields = {}


class MigrationServices:
    GIT = "1"
    GITHUB = "2"
    GITEA = "3"
    GITLAB = "4"
    GOGS = "5"
    ONEDEV = "6"
    GITBUCKET = "7"
    CODEBASE = "8"


class UserRepoPermission(ReadonlyApiObject):
    READ = "read"
    WRITE = "write"
    ADMIN = "admin"

    permission: str
    role_name: str
    user: User

    def __init__(self, gitea: "Gitea"):
        super().__init__(gitea)

    def __eq__(self, other: ReadonlyApiObject):
        if not isinstance(other, UserRepoPermission):
            return False
        return self.permission == other.permission and self.role_name == other.role_name and self.user == other.user

    def __hash__(self):
        return hash(self.permission) ^ hash(self.role_name) ^ hash(self.user)

    _fields_to_parsers = {
        "user": lambda gitea, r: User.parse_response(gitea, r),
    }


@dataclass(frozen=True)
class RepoUnits:
    code: str = "none"
    issues: str = "none"
    ext_issues: str = "none"
    wiki: str = "none"
    pulls: str = "none"
    releases: str = "none"
    ext_wiki: str = "none"
    actions: str = "none"

    def to_dict(self) -> dict[str, str]:
        """Return the correctly prefixed (added "repo.") representation for gitea Repository unit Rights"""
        return {f"repo.{field.name}": getattr(self, field.name) for field in fields(self)}

    @classmethod
    def from_dict(cls, unit_dict: dict[str, str]) -> "RepoUnits":
        """Parse all known repo units from the dictionary returned by the api"""
        return RepoUnits(**{k[5:]: v for k, v in unit_dict.items() if k[5:] in {field.name for field in fields(cls)}})
