from __future__ import annotations

from repo_core.base import BaseRepository

from .model import CodalStatementListing


class CodalStatementListingRepository(BaseRepository[CodalStatementListing]):
    TABLE = "codal_statement_listings"
    _backends = {"db"}

    async def upsert_page(self, data: dict) -> CodalStatementListing:
        """Insert or update by statement_type + page (unique constraint)."""
        return await self.save(self.TABLE, data)

    async def find_by_type_and_page(
        self, statement_type: str, page: int
    ) -> CodalStatementListing | None:
        return await self.first(
            self.TABLE, {"statement_type": statement_type, "page": page}
        )

    async def count_by_type(self, statement_type: str) -> int:
        return await self.count(self.TABLE, {"statement_type": statement_type})
