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

transaction: DBTransactions = DBTransactions()


@dataclass
class Routes:
    """
    Data class that represents each route, 
    reflection of the data and how it's store in the database.
    """
    route_id: int = None
    delivery_id: int = None

    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 Routes object.
        """

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

    def push_to_database(self, data: Self) -> None:
        """
        Push driver data to the database, one route at a time.
        """
        transformed_data: tuple = (data.route_id,
                                   data.delivery_id
                                   )
        transaction.add_data("routes", transformed_data)

    def remove_from_database(self, route_id: int) -> None:
        """
        Remove route data from the database, one route at a time.
        """
        transaction.remove_data(table="routes", property="routeid" ,id=route_id)

    def bulk_push_to_database(self, data: list[Self | tuple]) -> None:
        string_data: list[str] = []
        for item in data:
            if (type(item) == type(Self)):
                self.convert_to_string(item)
            string_data.append(item)
        transaction.bulkPushToDatabase("routes", string_data)

    def convert_to_string(self, data: Self) -> tuple:
        return (data.route_id,
                data.delivery_id
                )
