from sqlalchemy import select
from .database import DatingOrm, new_session
from .schemas import SDatingAdd, SDating


class DatingRepository:
    @classmethod
    async def add_dating(cls, dating: SDatingAdd) -> int | None:
        async with new_session() as session:
            try:
                data = dating.model_dump()
                new_dating = DatingOrm(**data)

                session.add(new_dating)
                await session.flush()
                await session.commit()
                return new_dating.id
            except Exception as e:
                print(e)
                return None

    @classmethod
    async def get_datings(cls) -> list[SDating]:
        async with new_session() as session:
            query = select(DatingOrm)
            result = await session.execute(query)
            dating_models = result.scalars().all()
            datings = [
                SDating.model_validate(dating_model) for dating_model in dating_models
            ]
            return datings
