"""
BackendConnectionDatabaseTag automatically generated by SDKgen please do not edit this file manually
https://sdkgen.app
"""

import requests
import sdkgen
from requests import RequestException
from typing import List
from typing import Dict
from typing import Any
from urllib.parse import parse_qs

from .backend_database_row import BackendDatabaseRow
from .backend_database_row_collection import BackendDatabaseRowCollection
from .backend_database_table import BackendDatabaseTable
from .backend_database_table_collection import BackendDatabaseTableCollection
from .common_message import CommonMessage
from .common_message_exception import CommonMessageException

class BackendConnectionDatabaseTag(sdkgen.TagAbstract):
    def __init__(self, http_client: requests.Session, parser: sdkgen.Parser):
        super().__init__(http_client, parser)


    def create_row(self, connection_id: str, table_name: str, payload: BackendDatabaseRow) -> CommonMessage:
        """
        Creates a new row at a table on a database
        """
        try:
            path_params = {}
            path_params['connection_id'] = connection_id
            path_params['table_name'] = table_name

            query_params = {}

            query_struct_names = []

            url = self.parser.url('/backend/connection/:connection_id/database/:table_name/rows', path_params)

            options = {}
            options['headers'] = {}
            options['params'] = self.parser.query(query_params, query_struct_names)

            options['json'] = payload.model_dump(by_alias=True)

            options['headers']['Content-Type'] = 'application/json'

            response = self.http_client.request('POST', url, **options)

            if response.status_code >= 200 and response.status_code < 300:
                data = CommonMessage.model_validate_json(json_data=response.content)

                return data

            statusCode = response.status_code
            if statusCode >= 0 and statusCode <= 999:
                data = CommonMessage.model_validate_json(json_data=response.content)

                raise CommonMessageException(data)

            raise sdkgen.UnknownStatusCodeException('The server returned an unknown status code: ' + str(statusCode))
        except RequestException as e:
            raise sdkgen.ClientException('An unknown error occurred: ' + str(e))

    def create_table(self, connection_id: str, payload: BackendDatabaseTable) -> CommonMessage:
        """
        Creates a new table on a database
        """
        try:
            path_params = {}
            path_params['connection_id'] = connection_id

            query_params = {}

            query_struct_names = []

            url = self.parser.url('/backend/connection/:connection_id/database', path_params)

            options = {}
            options['headers'] = {}
            options['params'] = self.parser.query(query_params, query_struct_names)

            options['json'] = payload.model_dump(by_alias=True)

            options['headers']['Content-Type'] = 'application/json'

            response = self.http_client.request('POST', url, **options)

            if response.status_code >= 200 and response.status_code < 300:
                data = CommonMessage.model_validate_json(json_data=response.content)

                return data

            statusCode = response.status_code
            if statusCode >= 0 and statusCode <= 999:
                data = CommonMessage.model_validate_json(json_data=response.content)

                raise CommonMessageException(data)

            raise sdkgen.UnknownStatusCodeException('The server returned an unknown status code: ' + str(statusCode))
        except RequestException as e:
            raise sdkgen.ClientException('An unknown error occurred: ' + str(e))

    def delete_row(self, connection_id: str, table_name: str, id: str) -> CommonMessage:
        """
        Deletes an existing row at a table on a database
        """
        try:
            path_params = {}
            path_params['connection_id'] = connection_id
            path_params['table_name'] = table_name
            path_params['id'] = id

            query_params = {}

            query_struct_names = []

            url = self.parser.url('/backend/connection/:connection_id/database/:table_name/rows/:id', path_params)

            options = {}
            options['headers'] = {}
            options['params'] = self.parser.query(query_params, query_struct_names)



            response = self.http_client.request('DELETE', url, **options)

            if response.status_code >= 200 and response.status_code < 300:
                data = CommonMessage.model_validate_json(json_data=response.content)

                return data

            statusCode = response.status_code
            if statusCode >= 0 and statusCode <= 999:
                data = CommonMessage.model_validate_json(json_data=response.content)

                raise CommonMessageException(data)

            raise sdkgen.UnknownStatusCodeException('The server returned an unknown status code: ' + str(statusCode))
        except RequestException as e:
            raise sdkgen.ClientException('An unknown error occurred: ' + str(e))

    def delete_table(self, connection_id: str, table_name: str) -> CommonMessage:
        """
        Deletes an existing table on a database
        """
        try:
            path_params = {}
            path_params['connection_id'] = connection_id
            path_params['table_name'] = table_name

            query_params = {}

            query_struct_names = []

            url = self.parser.url('/backend/connection/:connection_id/database/:table_name', path_params)

            options = {}
            options['headers'] = {}
            options['params'] = self.parser.query(query_params, query_struct_names)



            response = self.http_client.request('DELETE', url, **options)

            if response.status_code >= 200 and response.status_code < 300:
                data = CommonMessage.model_validate_json(json_data=response.content)

                return data

            statusCode = response.status_code
            if statusCode >= 0 and statusCode <= 999:
                data = CommonMessage.model_validate_json(json_data=response.content)

                raise CommonMessageException(data)

            raise sdkgen.UnknownStatusCodeException('The server returned an unknown status code: ' + str(statusCode))
        except RequestException as e:
            raise sdkgen.ClientException('An unknown error occurred: ' + str(e))

    def get_row(self, connection_id: str, table_name: str, id: str) -> BackendDatabaseRow:
        """
        Returns a specific row at a table on a database
        """
        try:
            path_params = {}
            path_params['connection_id'] = connection_id
            path_params['table_name'] = table_name
            path_params['id'] = id

            query_params = {}

            query_struct_names = []

            url = self.parser.url('/backend/connection/:connection_id/database/:table_name/rows/:id', path_params)

            options = {}
            options['headers'] = {}
            options['params'] = self.parser.query(query_params, query_struct_names)



            response = self.http_client.request('GET', url, **options)

            if response.status_code >= 200 and response.status_code < 300:
                data = BackendDatabaseRow.model_validate_json(json_data=response.content)

                return data

            statusCode = response.status_code
            if statusCode >= 0 and statusCode <= 999:
                data = CommonMessage.model_validate_json(json_data=response.content)

                raise CommonMessageException(data)

            raise sdkgen.UnknownStatusCodeException('The server returned an unknown status code: ' + str(statusCode))
        except RequestException as e:
            raise sdkgen.ClientException('An unknown error occurred: ' + str(e))

    def get_rows(self, connection_id: str, table_name: str, start_index: int, count: int, filter_by: str, filter_op: str, filter_value: str, sort_by: str, sort_order: str, columns: str) -> BackendDatabaseRowCollection:
        """
        Returns paginated rows at a table on a database
        """
        try:
            path_params = {}
            path_params['connection_id'] = connection_id
            path_params['table_name'] = table_name

            query_params = {}
            query_params['startIndex'] = start_index
            query_params['count'] = count
            query_params['filterBy'] = filter_by
            query_params['filterOp'] = filter_op
            query_params['filterValue'] = filter_value
            query_params['sortBy'] = sort_by
            query_params['sortOrder'] = sort_order
            query_params['columns'] = columns

            query_struct_names = []

            url = self.parser.url('/backend/connection/:connection_id/database/:table_name/rows', path_params)

            options = {}
            options['headers'] = {}
            options['params'] = self.parser.query(query_params, query_struct_names)



            response = self.http_client.request('GET', url, **options)

            if response.status_code >= 200 and response.status_code < 300:
                data = BackendDatabaseRowCollection.model_validate_json(json_data=response.content)

                return data

            statusCode = response.status_code
            if statusCode >= 0 and statusCode <= 999:
                data = CommonMessage.model_validate_json(json_data=response.content)

                raise CommonMessageException(data)

            raise sdkgen.UnknownStatusCodeException('The server returned an unknown status code: ' + str(statusCode))
        except RequestException as e:
            raise sdkgen.ClientException('An unknown error occurred: ' + str(e))

    def get_table(self, connection_id: str, table_name: str) -> BackendDatabaseTable:
        """
        Returns the schema of a specific table on a database
        """
        try:
            path_params = {}
            path_params['connection_id'] = connection_id
            path_params['table_name'] = table_name

            query_params = {}

            query_struct_names = []

            url = self.parser.url('/backend/connection/:connection_id/database/:table_name', path_params)

            options = {}
            options['headers'] = {}
            options['params'] = self.parser.query(query_params, query_struct_names)



            response = self.http_client.request('GET', url, **options)

            if response.status_code >= 200 and response.status_code < 300:
                data = BackendDatabaseTable.model_validate_json(json_data=response.content)

                return data

            statusCode = response.status_code
            if statusCode >= 0 and statusCode <= 999:
                data = CommonMessage.model_validate_json(json_data=response.content)

                raise CommonMessageException(data)

            raise sdkgen.UnknownStatusCodeException('The server returned an unknown status code: ' + str(statusCode))
        except RequestException as e:
            raise sdkgen.ClientException('An unknown error occurred: ' + str(e))

    def get_tables(self, connection_id: str, start_index: int, count: int) -> BackendDatabaseTableCollection:
        """
        Returns all available tables on a database
        """
        try:
            path_params = {}
            path_params['connection_id'] = connection_id

            query_params = {}
            query_params['startIndex'] = start_index
            query_params['count'] = count

            query_struct_names = []

            url = self.parser.url('/backend/connection/:connection_id/database', path_params)

            options = {}
            options['headers'] = {}
            options['params'] = self.parser.query(query_params, query_struct_names)



            response = self.http_client.request('GET', url, **options)

            if response.status_code >= 200 and response.status_code < 300:
                data = BackendDatabaseTableCollection.model_validate_json(json_data=response.content)

                return data

            statusCode = response.status_code
            if statusCode >= 0 and statusCode <= 999:
                data = CommonMessage.model_validate_json(json_data=response.content)

                raise CommonMessageException(data)

            raise sdkgen.UnknownStatusCodeException('The server returned an unknown status code: ' + str(statusCode))
        except RequestException as e:
            raise sdkgen.ClientException('An unknown error occurred: ' + str(e))

    def update_row(self, connection_id: str, table_name: str, id: str, payload: BackendDatabaseRow) -> CommonMessage:
        """
        Updates an existing row at a table on a database
        """
        try:
            path_params = {}
            path_params['connection_id'] = connection_id
            path_params['table_name'] = table_name
            path_params['id'] = id

            query_params = {}

            query_struct_names = []

            url = self.parser.url('/backend/connection/:connection_id/database/:table_name/rows/:id', path_params)

            options = {}
            options['headers'] = {}
            options['params'] = self.parser.query(query_params, query_struct_names)

            options['json'] = payload.model_dump(by_alias=True)

            options['headers']['Content-Type'] = 'application/json'

            response = self.http_client.request('PUT', url, **options)

            if response.status_code >= 200 and response.status_code < 300:
                data = CommonMessage.model_validate_json(json_data=response.content)

                return data

            statusCode = response.status_code
            if statusCode >= 0 and statusCode <= 999:
                data = CommonMessage.model_validate_json(json_data=response.content)

                raise CommonMessageException(data)

            raise sdkgen.UnknownStatusCodeException('The server returned an unknown status code: ' + str(statusCode))
        except RequestException as e:
            raise sdkgen.ClientException('An unknown error occurred: ' + str(e))

    def update_table(self, connection_id: str, table_name: str, payload: BackendDatabaseTable) -> CommonMessage:
        """
        Updates an existing table on a database
        """
        try:
            path_params = {}
            path_params['connection_id'] = connection_id
            path_params['table_name'] = table_name

            query_params = {}

            query_struct_names = []

            url = self.parser.url('/backend/connection/:connection_id/database/:table_name', path_params)

            options = {}
            options['headers'] = {}
            options['params'] = self.parser.query(query_params, query_struct_names)

            options['json'] = payload.model_dump(by_alias=True)

            options['headers']['Content-Type'] = 'application/json'

            response = self.http_client.request('PUT', url, **options)

            if response.status_code >= 200 and response.status_code < 300:
                data = CommonMessage.model_validate_json(json_data=response.content)

                return data

            statusCode = response.status_code
            if statusCode >= 0 and statusCode <= 999:
                data = CommonMessage.model_validate_json(json_data=response.content)

                raise CommonMessageException(data)

            raise sdkgen.UnknownStatusCodeException('The server returned an unknown status code: ' + str(statusCode))
        except RequestException as e:
            raise sdkgen.ClientException('An unknown error occurred: ' + str(e))



