from sqlalchemy.ext.asyncio import AsyncSession

from app.application.appointment_types_repository import AppointmentTypesRepository
from app.domain.entities.appointment_type import AppointmentTypeEntity
from app.domain.models import AppointmentType


class AppointmentTypesService:
    def __init__(self, db: AsyncSession):
        self.appointment_types_repository = AppointmentTypesRepository(db)

    async def get_all_appointment_types(self) -> list[AppointmentTypeEntity]:
        appointment_types = await self.appointment_types_repository.get_all()

        return [
            self.model_to_entity(appointment_type)
            for appointment_type in appointment_types
        ]

    @staticmethod
    def entity_to_model(appointment_type: AppointmentTypeEntity) -> AppointmentType:
        appointment_type_model = AppointmentType(
            title=appointment_type.title,
        )

        if appointment_type.id is not None:
            appointment_type_model.id = appointment_type.id

        return appointment_type_model

    @staticmethod
    def model_to_entity(appointment_type_model: AppointmentType) -> AppointmentTypeEntity:
        return AppointmentTypeEntity(
            id=appointment_type_model.id,
            title=appointment_type_model.title,
        )
