"""Repository identity model for Accountable Record.

WHY: Stage 010 establishes repository identity before later stages consume
contracts, load elements, build catalogs, or generate artifacts.
"""

from pathlib import Path

from accountable_record.pipeline.model import IdentityArtifact
from accountable_record.stage_010_identity.base.constants import PYPROJECT_FILENAME

__all__ = ["IdentityArtifact", "find_repository_root"]


def find_repository_root(start: Path | None = None) -> Path:
    """Find the repository root by walking upward from start.

    A directory is treated as the repository root when it contains
    pyproject.toml. This keeps the function independent of Git metadata.
    """
    current = (start or Path.cwd()).resolve()

    if current.is_file():
        current = current.parent

    for candidate in (current, *current.parents):
        if (candidate / PYPROJECT_FILENAME).is_file():
            return candidate

    msg = f"Could not find repository root from {current}"
    raise FileNotFoundError(msg)
