from typing import List, Optional

from sqlalchemy.orm import Session

from app.core.entities.accessory import AccessoryEntity
from app.infrastructure.database.models.accessory import Accessory
from app.infrastructure.database.repository.accessory_repository import AccessoriesRepository


class AccessoriesService:
    def __init__(self, db: Session):
        self.repository = AccessoriesRepository(db)

    def get_all_accessories(self) -> List[AccessoryEntity]:
        accessories = self.repository.get_with_cities_all()
        return [
            AccessoryEntity(
                id=a.id,
                name=a.name,
                volume=a.volume,
                count=a.count,
                weight=a.weight,
                period=a.period,
                city_id=a.city_id,
                city_name=a.city.name
            )
            for a in accessories
        ]

    def get_accessory_by_id(self, accessory_id: int) -> Optional[AccessoryEntity]:
        accessory = self.repository.get_by_id(accessory_id)
        if accessory:
            return AccessoryEntity(
                id=accessory.id,
                name=accessory.name,
                count=accessory.count,
                volume=accessory.volume,
                weight=accessory.weight,
                period=accessory.period,
                city_id=accessory.city_id
            )
        return None

    def create_accessory(self, entity: AccessoryEntity) -> AccessoryEntity:
        accessory_model = Accessory(
            name=entity.name,
            count=entity.count,
            volume=entity.volume,
            weight=entity.weight,
            period=entity.period,
            city_id=entity.city_id
        )
        created_accessory = self.repository.create(accessory_model)
        return AccessoryEntity(
            id=created_accessory.id,
            name=created_accessory.name,
            count=created_accessory.count,
            volume=created_accessory.volume,
            weight=created_accessory.weight,
            period=created_accessory.period,
            city_id=created_accessory.city_id
        )

    def update_accessory(self, accessory_id: int, entity: AccessoryEntity) -> Optional[AccessoryEntity]:
        accessory_model = self.repository.get_by_id(accessory_id)
        if accessory_model:
            accessory_model.name = entity.name
            accessory_model.count = entity.count
            accessory_model.volume = entity.volume
            accessory_model.weight = entity.weight
            accessory_model.period = entity.period
            accessory_model.city_id = entity.city_id
            self.repository.update(accessory_model)
            return AccessoryEntity(
                id=accessory_model.id,
                name=accessory_model.name,
                count=accessory_model.count,
                volume=accessory_model.volume,
                weight=accessory_model.weight,
                period=accessory_model.period,
                city_id=accessory_model.city_id
            )
        return None

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