from typing import Optional, Sequence

from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession

from app.domain.models import AppointmentFile


class AppointmentFilesRepository:
    def __init__(self, db: AsyncSession):
        self.db = db

    async def get_by_id(self, file_id: int) -> Optional[AppointmentFile]:
        stmt = select(AppointmentFile).filter_by(id=file_id)
        result = await self.db.execute(stmt)
        return result.scalars().first()

    async def get_by_appointment_id(self, appointment_id: int) -> Sequence[AppointmentFile]:
        stmt = select(AppointmentFile).filter_by(appointment_id=appointment_id)
        result = await self.db.execute(stmt)
        return result.scalars().all()

    async def create(self, appointment_file: AppointmentFile) -> AppointmentFile:
        self.db.add(appointment_file)
        await self.db.commit()
        await self.db.refresh(appointment_file)
        return appointment_file

    async def delete(self, appointment_file: AppointmentFile) -> AppointmentFile:
        await self.db.delete(appointment_file)
        await self.db.commit()
        return appointment_file
