from sqlalchemy.ext.asyncio import AsyncSession

from api.models.managed_repo import ManagedRepo
from api.models.repository_rename import RepositoryForgeIdentity, RepositorySlug


def stage_initial_repository_identity(
    session: AsyncSession, repo: ManagedRepo
) -> None:
    """Attach non-guessed identity state to a newly flushed repository row."""
    if repo.id is None:
        raise ValueError("repository must be flushed before identity records are staged")
    session.add_all(
        [
            RepositoryForgeIdentity(repo_id=repo.id),
            RepositorySlug(
                repo_id=repo.id,
                slug=repo.slug,
                kind="canonical",
                protected=True,
            ),
        ]
    )
