import github

from mindsdb_sql_parser import parse_sql

from mindsdb.integrations.handlers.github_handler.github_tables import (
    GithubIssuesTable,
    GithubPullRequestsTable,
    GithubCommitsTable,
    GithubReleasesTable,
    GithubBranchesTable,
    GithubContributorsTable,
    GithubMilestonesTable,
    GithubProjectsTable, GithubFilesTable
)
from mindsdb.integrations.handlers.github_handler.generate_api import get_github_types, get_github_methods, GHTable
from mindsdb.integrations.libs.api_handler import APIHandler
from mindsdb.integrations.libs.response import (
    HandlerStatusResponse as StatusResponse,
)
from mindsdb.utilities import log


logger = log.getLogger(__name__)


class GithubHandler(APIHandler):
    """The GitHub handler implementation"""

    def __init__(self, name: str, **kwargs):
        """Initialize the GitHub handler.

        Parameters
        ----------
        name : str
            name of a handler instance
        """
        super().__init__(name)

        connection_data = kwargs.get("connection_data", {})
        self.connection_data = connection_data
        self.repository = connection_data["repository"]
        self.kwargs = kwargs

        self.connection = None
        self.is_connected = False

        # custom tables
        self._register_table("issues", GithubIssuesTable(self))
        self._register_table("files", GithubFilesTable(self))

        # generated tables
        github_types = get_github_types()

        # generate tables from repository object
        for method in get_github_methods(github.Repository.Repository):
            if method.table_name in self._tables:
                continue

            table = GHTable(self, github_types=github_types, method=method)
            self._register_table(method.table_name, table)


    def connect(self) -> StatusResponse:
        """Set up the connection required by the handler.

        Returns
        -------
        StatusResponse
            connection object
        """

        if self.is_connected is True:
            return self.connection

        connection_kwargs = {}

        if self.connection_data.get("api_key", None):
            connection_kwargs["login_or_token"] = self.connection_data["api_key"]

        if self.connection_data.get("github_url", None):
            connection_kwargs["base_url"] = self.connection_data["github_url"]

        self.connection = github.Github(**connection_kwargs)
        self.is_connected = True

        return self.connection

    def check_connection(self) -> StatusResponse:
        """Check connection to the handler.

        Returns
        -------
        StatusResponse
            Status confirmation
        """
        response = StatusResponse(False)

        try:
            self.connect()
            if self.connection_data.get("api_key", None):
                current_user = self.connection.get_user().name
                logger.info(f"Authenticated as user {current_user}")
            else:
                logger.info("Proceeding without an API key")

            current_limit = self.connection.get_rate_limit()
            logger.info(f"Current rate limit: {current_limit}")
            response.success = True
        except Exception as e:
            logger.error(f"Error connecting to GitHub API: {e}!")
            response.error_message = e

        self.is_connected = response.success

        return response

    def native_query(self, query: str) -> StatusResponse:
        """Receive and process a raw query.

        Parameters
        ----------
        query : str
            query in a native format

        Returns
        -------
        StatusResponse
            Request status
        """
        ast = parse_sql(query)
        return self.query(ast)
