from typing import List, Optional

from sqlalchemy.orm import Session

from app.core.entities.total_order import TotalOrderEntity
from app.infrastructure.database.models.total_orders import TotalOrder
from app.infrastructure.database.repository.total_order_repository import TotalOrdersRepository


class TotalOrdersService:
    def __init__(self, db: Session):
        self.repository = TotalOrdersRepository(db)

    def get_all_total_orders(self) -> List[TotalOrderEntity]:
        total_orders = self.repository.get_all_with_users_and_statuses()
        return [
            TotalOrderEntity(
                id=order.id,
                order_datetime=order.order_datetime,
                count_robots=order.count_robots,
                deadline=order.deadline,
                price=round(order.price),
                user_id=order.user_id,
                status_id=order.status_id,
                delivery_orders=[do.id for do in order.delivery_orders] if order.delivery_orders else [],
                user_login=order.user.login,
                status_name=order.status.name,
            )
            for order in total_orders
        ]

    def get_total_order_by_id(self, total_order_id: int) -> Optional[TotalOrderEntity]:
        total_order = self.repository.get_by_id(total_order_id)
        if total_order:
            return TotalOrderEntity(
                id=total_order.id,
                order_datetime=total_order.order_datetime,
                count_robots=total_order.count_robots,
                deadline=total_order.deadline,
                price=total_order.price,
                user_id=total_order.user_id,
                status_id=total_order.status_id,
                delivery_orders=[do.id for do in total_order.delivery_orders] if total_order.delivery_orders else []
            )
        return None

    def create_total_order(self, entity: TotalOrderEntity) -> TotalOrderEntity:
        total_order_model = TotalOrder(
            order_datetime=entity.order_datetime,
            count_robots=entity.count_robots,
            deadline=entity.deadline,
            price=entity.price,
            user_id=entity.user_id,
            status_id=entity.status_id
        )
        created_total_order = self.repository.create(total_order_model)
        return TotalOrderEntity(
            id=created_total_order.id,
            order_datetime=created_total_order.order_datetime,
            count_robots=created_total_order.count_robots,
            deadline=created_total_order.deadline,
            price=created_total_order.price,
            user_id=created_total_order.user_id,
            status_id=created_total_order.status_id,
            delivery_orders=[]
        )

    def update_total_order(self, total_order_id: int, entity: TotalOrderEntity) -> Optional[TotalOrderEntity]:
        total_order_model = self.repository.get_by_id(total_order_id)
        if total_order_model:
            total_order_model.order_datetime = entity.order_datetime
            total_order_model.count_robots = entity.count_robots
            total_order_model.deadline = entity.deadline
            total_order_model.price = entity.price
            total_order_model.user_id = entity.user_id
            total_order_model.status_id = entity.status_id
            self.repository.update(total_order_model)
            return TotalOrderEntity(
                id=total_order_model.id,
                order_datetime=total_order_model.order_datetime,
                count_robots=total_order_model.count_robots,
                deadline=total_order_model.deadline,
                price=total_order_model.price,
                user_id=total_order_model.user_id,
                status_id=total_order_model.status_id,
                delivery_orders=[do.id for do in
                                 total_order_model.delivery_orders] if total_order_model.delivery_orders else []
            )
        return None

    def delete_total_order(self, total_order_id: int) -> bool:
        return self.repository.delete(total_order_id) is not None
