from typing import Any, Generic, Mapping, Sequence, Type, TypeVar

from sqlalchemy import delete, insert, select, update
from sqlalchemy.ext.asyncio import AsyncSession

ModelType = TypeVar("ModelType")


class BaseRepo(Generic[ModelType]):
    model: Type[ModelType]

    def __init__(self, session: AsyncSession) -> None:
        self.session: AsyncSession = session

    async def get_filtered(
        self, *filters: Any, **filter_by: Any
    ) -> Sequence[ModelType]:
        query = select(self.model).filter(*filters).filter_by(**filter_by)
        result = await self.session.execute(query)
        models = result.scalars().all()
        return models

    async def create_one(self, data: Mapping[str, Any]) -> ModelType:
        statement = insert(self.model).values(data).returning(self.model)
        result = await self.session.execute(statement)
        obj: ModelType = result.scalar_one()
        return obj

    async def create_bulk(self, data: Sequence[Mapping[str, Any]]) -> list[ModelType]:
        result = [await self.create_one(item) for item in data]
        return result

    async def get_one_or_none(self, **filter_by: Any) -> ModelType | None:
        query = select(self.model).filter_by(**filter_by)
        result = await self.session.execute(query)
        model_obj: ModelType | None = result.scalars().one_or_none()
        return model_obj

    async def get_all(self, *args: Any, **kwargs: Any) -> Sequence[ModelType]:
        result: Sequence[ModelType] = await self.get_filtered(*args, **kwargs)
        return result

    async def delete_one(self, **filter_by) -> None:
        await self.session.execute(delete(self.model).filter_by(**filter_by))

    async def update_one(self, data: dict[str, Any], **filter_by: Any) -> ModelType:
        stmt = (
            update(self.model).filter_by(**filter_by).values(data).returning(self.model)
        )
        result = await self.session.execute(stmt)
        model = result.scalar_one()
        return model
