from dataclasses import dataclass
from typing import Self
from db.dbtransactions import DBTransactions

transaction: DBTransactions = DBTransactions()


@dataclass
class Driver:
    """
    Data class that represents each driver, 
    reflection of the data and how it's store in the database.
    """
    driver_id: int
    route_id: int

    def pull_from_database(self) -> list[Self]:
        """
        Pull delvery data from the database.\n
        Returns - A list of items from the database 
        where each item is a drivers object.
        """

        database_data: list[tuple] = transaction.get_data("drivers")
        transformed_data: list[Self] = []
        for drivers in database_data:
            transformed_data.append(Driver(
                drivers[0],
                drivers[1]))
        return transformed_data

    def push_to_database(self, database_data: Self) -> None:
        """
        Push driver data to the database, one drivers at a time.
        """
        transaction.add_data("drivers", self.convert_to_tuple(database_data))

    def bulk_push_to_database(self, database_data: list[Self | tuple]) -> None:
        string_data: list[int] = []
        for item in database_data:
            if (type(item) == type(Self)):
                self.convert_to_tuple(item)
            string_data.append(item)
        transaction.bulk_add_data("drivers", string_data)

    def remove_from_database(self, drivers_id: int) -> None:
        transaction.remove_data("drivers", "driverid", drivers_id)

    def update_in_database(self, drivers_id: int, data: Self) -> None:
        transaction.update_data("drivers", "driverid",
                                drivers_id, self.transform_to_query(data))

    def transform_to_query(self, data: Self):
        return f"""driverid={data.driver_id},
                   routeid='{data.route_id}','"""

    def convert_to_tuple(self, data: Self) -> tuple:
        return (data.driver_id,
                data.route_id,)
