"""Migration authority for legacy projects whose historical source boundary is lost."""

from __future__ import annotations

from dataclasses import dataclass


LEGACY_SOURCE_BOUNDARY_UNAVAILABLE = "LEGACY_SOURCE_BOUNDARY_UNAVAILABLE"


@dataclass(frozen=True)
class CurrentHeadMigrationBoundary:
    repository: str
    branch: str
    current_source_sha: str
    managed_runtime_fingerprint: str
    semantic_fingerprint: str
    binding_fingerprint: str
    dependency_fingerprint: str
    lockfile_status: str
    ownership_evidence: str
    evidence_schema_version: str
    historical_boundary_status: str = LEGACY_SOURCE_BOUNDARY_UNAVAILABLE


def authorize_current_head_boundary(
    *,
    repository: str,
    branch: str,
    current_source_sha: str,
    managed_runtime_fingerprint: str,
    semantic_fingerprint: str,
    binding_fingerprint: str,
    dependency_fingerprint: str,
    lockfile_status: str,
    ownership_evidence: str,
    evidence_schema_version: str = "phase5e4b.v1",
) -> CurrentHeadMigrationBoundary:
    """Create a new authority boundary only from independently verified evidence."""
    required = {
        "repository": repository,
        "branch": branch,
        "current_source_sha": current_source_sha,
        "managed_runtime_fingerprint": managed_runtime_fingerprint,
        "semantic_fingerprint": semantic_fingerprint,
        "binding_fingerprint": binding_fingerprint,
        "dependency_fingerprint": dependency_fingerprint,
        "lockfile_status": lockfile_status,
        "ownership_evidence": ownership_evidence,
    }
    if any(not str(value).strip() for value in required.values()):
        raise ValueError("current_head_boundary_requires_complete_evidence")
    if ownership_evidence != "P2HS_MANAGED_UNMODIFIED":
        raise ValueError("current_head_boundary_requires_unmodified_managed_runtime")
    if lockfile_status not in {"conformant", "certified_migration_metadata"}:
        raise ValueError("current_head_boundary_requires_certified_dependency_evidence")
    return CurrentHeadMigrationBoundary(
        **required,
        evidence_schema_version=evidence_schema_version,
    )
