from typing import Sequence, Optional

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

from app.domain.models import Backup


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

    async def get_all(self) -> Sequence[Backup]:
        stmt = select(Backup)
        result = await self.db.execute(stmt)
        return result.scalars().all()

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

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

    async def delete(self, backup: Backup) -> Backup:
        await self.db.delete(backup)
        await self.db.commit()
        return backup
