from __future__ import annotations

from repo_core.base import BaseRepository

from .model import CodalPublisherPage


class CodalPublisherPageRepository(BaseRepository[CodalPublisherPage]):
    TABLE = "codal_publisher_pages"
    _backends = {"db"}

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

    async def find_by_publisher_id(self, publisher_id: int) -> list[CodalPublisherPage]:
        return await self.find(self.TABLE, {"publisher_id": publisher_id})

    async def find_pending(self, limit: int = 100) -> list[CodalPublisherPage]:
        """Find publisher pages not yet fetched (for resume)."""
        return await self.find(
            self.TABLE,
            {"status": "pending"},
            limit=limit,
        )
