"""Report repository with specific methods."""

from typing import Optional, List
from uuid import UUID
from datetime import datetime
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select, update, desc

from src.models.report import Report
from src.repositories.base import BaseRepository


class ReportRepository(BaseRepository[Report]):
    """Repository for Report model with specific methods."""

    def __init__(self):
        super().__init__(Report)

    async def get_by_scenario(
        self, db: AsyncSession, scenario_id: UUID, skip: int = 0, limit: int = 100
    ) -> List[Report]:
        """Get reports for a specific scenario."""
        query = (
            select(Report)
            .where(Report.scenario_id == scenario_id)
            .order_by(desc(Report.created_at))
            .offset(skip)
            .limit(limit)
        )
        result = await db.execute(query)
        return result.scalars().all()

    async def count_by_scenario(self, db: AsyncSession, scenario_id: UUID) -> int:
        """Count reports for a specific scenario."""
        query = select(Report).where(Report.scenario_id == scenario_id)
        result = await db.execute(query)
        return len(result.scalars().all())

    async def update_file_size(
        self, db: AsyncSession, report_id: UUID, file_size_bytes: int
    ) -> Optional[Report]:
        """Update report file size."""
        result = await db.execute(
            update(Report)
            .where(Report.id == report_id)
            .values(file_size_bytes=file_size_bytes)
            .returning(Report)
        )
        await db.commit()
        return result.scalar_one_or_none()


# Singleton instance
report_repository = ReportRepository()
