"""
BackendDatabaseTag 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 .backend_database_connections import BackendDatabaseConnections
from .backend_database_row import BackendDatabaseRow
from .backend_database_rows import BackendDatabaseRows
from .backend_database_table import BackendDatabaseTable
from .backend_database_tables import BackendDatabaseTables
from .common_message import CommonMessage
from .common_message_exception import CommonMessageException

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


    def delete_row(self, connection_id: str, table_name: str, id: str) -> CommonMessage:
        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/database/:connection_id/:table_name/rows/:id", path_params)

            headers = {}

            response = self.http_client.delete(url, headers=headers, params=self.parser.query(query_params, query_struct_names))

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

            if response.status_code == 400:
                raise CommonMessageException(response.content)
            if response.status_code == 401:
                raise CommonMessageException(response.content)
            if response.status_code == 404:
                raise CommonMessageException(response.content)
            if response.status_code == 500:
                raise CommonMessageException(response.content)

            raise sdkgen.UnknownStatusCodeException("The server returned an unknown status code")
        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:
        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/database/:connection_id/:table_name/rows/:id", path_params)

            headers = {}
            headers["Content-Type"] = "application/json"

            response = self.http_client.put(url, headers=headers, params=self.parser.query(query_params, query_struct_names), json=payload.model_dump(by_alias=True))

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

            if response.status_code == 400:
                raise CommonMessageException(response.content)
            if response.status_code == 401:
                raise CommonMessageException(response.content)
            if response.status_code == 404:
                raise CommonMessageException(response.content)
            if response.status_code == 500:
                raise CommonMessageException(response.content)

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

    def create_row(self, connection_id: str, table_name: str, payload: BackendDatabaseRow) -> CommonMessage:
        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/database/:connection_id/:table_name/rows", path_params)

            headers = {}
            headers["Content-Type"] = "application/json"

            response = self.http_client.post(url, headers=headers, params=self.parser.query(query_params, query_struct_names), json=payload.model_dump(by_alias=True))

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

            if response.status_code == 400:
                raise CommonMessageException(response.content)
            if response.status_code == 401:
                raise CommonMessageException(response.content)
            if response.status_code == 404:
                raise CommonMessageException(response.content)
            if response.status_code == 500:
                raise CommonMessageException(response.content)

            raise sdkgen.UnknownStatusCodeException("The server returned an unknown status code")
        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:
        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/database/:connection_id/:table_name/rows/:id", path_params)

            headers = {}

            response = self.http_client.get(url, headers=headers, params=self.parser.query(query_params, query_struct_names))

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

            if response.status_code == 401:
                raise CommonMessageException(response.content)
            if response.status_code == 404:
                raise CommonMessageException(response.content)
            if response.status_code == 500:
                raise CommonMessageException(response.content)

            raise sdkgen.UnknownStatusCodeException("The server returned an unknown status code")
        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) -> BackendDatabaseRows:
        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/database/:connection_id/:table_name/rows", path_params)

            headers = {}

            response = self.http_client.get(url, headers=headers, params=self.parser.query(query_params, query_struct_names))

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

            if response.status_code == 401:
                raise CommonMessageException(response.content)
            if response.status_code == 404:
                raise CommonMessageException(response.content)
            if response.status_code == 500:
                raise CommonMessageException(response.content)

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

    def delete_table(self, connection_id: str, table_name: str) -> CommonMessage:
        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/database/:connection_id/:table_name", path_params)

            headers = {}

            response = self.http_client.delete(url, headers=headers, params=self.parser.query(query_params, query_struct_names))

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

            if response.status_code == 400:
                raise CommonMessageException(response.content)
            if response.status_code == 401:
                raise CommonMessageException(response.content)
            if response.status_code == 404:
                raise CommonMessageException(response.content)
            if response.status_code == 500:
                raise CommonMessageException(response.content)

            raise sdkgen.UnknownStatusCodeException("The server returned an unknown status code")
        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:
        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/database/:connection_id/:table_name", path_params)

            headers = {}
            headers["Content-Type"] = "application/json"

            response = self.http_client.put(url, headers=headers, params=self.parser.query(query_params, query_struct_names), json=payload.model_dump(by_alias=True))

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

            if response.status_code == 400:
                raise CommonMessageException(response.content)
            if response.status_code == 401:
                raise CommonMessageException(response.content)
            if response.status_code == 404:
                raise CommonMessageException(response.content)
            if response.status_code == 500:
                raise CommonMessageException(response.content)

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

    def create_table(self, connection_id: str, payload: BackendDatabaseTable) -> CommonMessage:
        try:
            path_params = {}
            path_params["connection_id"] = connection_id

            query_params = {}

            query_struct_names = []

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

            headers = {}
            headers["Content-Type"] = "application/json"

            response = self.http_client.post(url, headers=headers, params=self.parser.query(query_params, query_struct_names), json=payload.model_dump(by_alias=True))

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

            if response.status_code == 400:
                raise CommonMessageException(response.content)
            if response.status_code == 401:
                raise CommonMessageException(response.content)
            if response.status_code == 404:
                raise CommonMessageException(response.content)
            if response.status_code == 500:
                raise CommonMessageException(response.content)

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

    def get_table(self, connection_id: str, table_name: str) -> BackendDatabaseTable:
        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/database/:connection_id/:table_name", path_params)

            headers = {}

            response = self.http_client.get(url, headers=headers, params=self.parser.query(query_params, query_struct_names))

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

            if response.status_code == 401:
                raise CommonMessageException(response.content)
            if response.status_code == 404:
                raise CommonMessageException(response.content)
            if response.status_code == 500:
                raise CommonMessageException(response.content)

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

    def get_tables(self, connection_id: str) -> BackendDatabaseTables:
        try:
            path_params = {}
            path_params["connection_id"] = connection_id

            query_params = {}

            query_struct_names = []

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

            headers = {}

            response = self.http_client.get(url, headers=headers, params=self.parser.query(query_params, query_struct_names))

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

            if response.status_code == 401:
                raise CommonMessageException(response.content)
            if response.status_code == 404:
                raise CommonMessageException(response.content)
            if response.status_code == 500:
                raise CommonMessageException(response.content)

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

    def get_connections(self) -> BackendDatabaseConnections:
        try:
            path_params = {}

            query_params = {}

            query_struct_names = []

            url = self.parser.url("/backend/database", path_params)

            headers = {}

            response = self.http_client.get(url, headers=headers, params=self.parser.query(query_params, query_struct_names))

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

            if response.status_code == 401:
                raise CommonMessageException(response.content)
            if response.status_code == 500:
                raise CommonMessageException(response.content)

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


