from requests import get
from json import loads
from octopype.errors.repos import RepositoryNotFound

class getRepoOwner:
    def __init__(self, given_json) -> None:
        oj = given_json["owner"]
        self.name = oj["login"]
        self.id = oj["id"]
        self.node_id = oj["node_id"]
        self.avatar = oj["avatar_url"]
        self.gravatar = oj["gravatar_id"]
        self.api_url = oj["url"]
        self.github_page = oj["html_url"]
        self.followers_api = oj["followers_url"]
        self.following_api = oj["following_url"]
        self.gists_api = oj["gists_url"]
        self.starred_api = oj["starred_url"]
        self.subscriptions_api = oj["subscriptions_url"]
        self.organizations_api = oj["organizations_url"]
        self.repositories_api = oj["repos_url"]
        self.events_api = oj["events_url"]
        self.received_events_api = oj["received_events_url"]
        self.account_type = oj["type"]
        self.is_administrator = oj["site_admin"]

class getRepoLicense:
    def __init__(self, given_json) -> None:
        lj = given_json["license"]
        self.key = lj["key"]
        self.name = lj["name"]
        self.spdx_id = lj["spdx_id"]
        self.api_url = lj["url"]
        self.node_id = lj["node_id"]

class getRepo:
    def __init__(self, token, repository, org) -> None:
        raw = loads(get(headers={"Accept":"application/vnd.github+json", "Authorization":f"Bearer {token}", "X-GitHub-Api-Version":"2022-11-28"}, url=f"https://api.github.com/repos/{org}/{repository}").text)
        if raw == {"message":"Not Found", "documentation_url":"https://docs.github.com/rest/repos/repos#get-a-repository"}:
            raise RepositoryNotFound("the requested repository doesn't exist or you can not access it")
        self.owner = getRepoOwner(raw)
        self.license = getRepoLicense(raw)
        self.id = raw["id"]
        self.node_id = raw["node_id"]
        self.name = raw["name"]
        self.full_name = raw["full_name"]
        self.is_private = raw["private"]
        self.github_page = raw["html_url"]
        self.description = raw["description"]
        self.is_fork = raw["fork"]
        self.api_url = raw["url"]
        self.api_forks = raw["forks_url"]
        self.api_keys = raw["keys_url"]
        self.api_collaborators = raw["collaborators_url"]
        self.api_teams = raw["teams_url"]
        self.api_hooks = raw["hooks_url"]
        self.api_issue_events = raw["issue_events_url"]
        self.api_assignees = raw["assignees_url"]
        self.api_branches = raw["branches_url"]
        self.api_tags = raw["tags_url"]
        self.api_blobs = raw["blobs_url"]
        self.api_git_tags = raw["git_tags_url"]
        self.api_refs_tags = raw["git_refs_url"]
        self.api_trees = raw["trees_url"]
        self.api_statuses = raw["statuses_url"]
        self.api_languages = raw["languages_url"]
        self.api_stargazers = raw["stargazers_url"]
        self.api_contributors = raw["contributors_url"]
        self.api_subscribers = raw["subscribers_url"]
        self.api_subscription = raw["subscription_url"]
        self.api_commits = raw["commits_url"]
        self.api_git_commits = raw["git_commits_url"]
        self.api_comments = raw["comments_url"]
        self.api_issue_comment = raw["issue_comment_url"]
        self.api_contents = raw["contents_url"]
        self.api_compare = raw["compare_url"]
        self.api_merges = raw["merges_url"]
        self.api_archive = raw["archive_url"]
        self.api_downloads = raw["downloads_url"]
        self.api_issues = raw["issues_url"]
        self.api_pulls = raw["pulls_url"]
        self.api_milestones = raw["milestones_url"]
        self.api_notifications = raw["notifications_url"]
        self.api_labels = raw["labels_url"]
        self.api_releases = raw["releases_url"]
        self.api_deployments = raw["deployments_url"]
        self.created_timestamp = raw["created_at"]
        self.updated_timestamp = raw["updated_at"]
        self.pushed_timestamp = raw["pushed_at"]
        self.git_url = raw["git_url"]
        self.ssh_url = raw["ssh_url"]
        self.clone_url = raw["clone_url"]
        self.svn_url = raw["svn_url"]
        self.homepage_url = raw["homepage"]
        self.size = raw["size"]
        self.stars = raw["stargazers_count"]
        self.watchers = raw["watchers_count"]
        self.most_used_language = raw["language"]
        self.has_issues = raw["has_issues"]
        self.has_downloads = raw["has_downloads"]
        self.has_wiki = raw["has_wiki"]
        self.has_pages = raw["has_pages"]
        self.has_discussions = raw["has_discussions"]
        self.forks = raw["forks_count"]
        self.mirror = raw["mirror_url"]
        self.is_archived = raw["archived"]
        self.is_disabled = raw["disabled"]
        self.open_issues = raw["open_issues_count"]
        self.is_forkable = raw["allow_forking"]
        self.is_template = raw["is_template"]
        self.web_commit_signoff_required = raw["web_commit_signoff_required"]
        self.topics = raw["topics"]
        self.visibility = raw["visibility"]
        self.network_count = raw["network_count"]
        self.subscribers_amount = raw["subscribers_count"]