from __future__ import annotations

from repo_core.base import BaseRepository

from .model import CodalStatementDetail


class CodalStatementDetailRepository(BaseRepository[CodalStatementDetail]):
    TABLE = "codal_statement_details"
    _backends = {"db"}

    async def upsert_by_tab(self, data: dict) -> CodalStatementDetail:
        """Insert or update by statement_id + tab_slug."""
        return await self.save(self.TABLE, data)

    async def find_by_statement_id(self, statement_id: str) -> list[CodalStatementDetail]:
        return await self.find(self.TABLE, {"statement_id": statement_id})

    async def find_pending_downloads(self, limit: int = 100) -> list[CodalStatementDetail]:
        return await self.find(
            self.TABLE,
            {"status": "pending"},
            limit=limit,
        )
