"""Canonical identity policy for the Mais Humana platform.

The institutional decision is explicit: the platform's canonical name is
``tudo-para-ia-mais-humana-platform``.  The physical repository may still be
materialized as the historical no-suffix folder while synchronization and Gitea
renames are coordinated, so the policy keeps compatibility aliases visible
instead of treating them as competing sources of truth.
"""

from __future__ import annotations

from dataclasses import dataclass
from typing import Any

from .models import as_plain_data, merge_unique


CANONICAL_PROJECT_ID = "tudo-para-ia-mais-humana-platform"
CURRENT_PROJECT_ID = "tudo-para-ia-mais-humana"
LEGACY_PLATAFORM_ALIAS = "tudo-para-ia-mais-humana-plataform"
CENTRAL_FOLDER_NAME = "15_repo_tudo-para-ia-mais-humana-platform"
CANONICAL_DECISION_STATUS = "approved"
CANONICAL_DECISION_SOURCE = "015-ROTEADOR-PERMANENTE-DE-ORDEM_DE_SERVICO.MD"
MCP_CONTROL_PLANE_ID = "tudo-para-ia-mcps-internos-plataform"
UI_SUPPORT_PLATFORM_ID = "tudo-para-ia-ui-platform"
MCP_EXECUTE_ENDPOINT = "https://mcps-gateway.ami-app.workers.dev/v1/execute"

LEGACY_PROJECT_IDS: tuple[str, ...] = (
    CURRENT_PROJECT_ID,
    LEGACY_PLATAFORM_ALIAS,
)

ACCEPTED_PROJECT_IDS: tuple[str, ...] = merge_unique(
    (CANONICAL_PROJECT_ID, *LEGACY_PROJECT_IDS)
)

CANONICAL_COMPATIBILITY_RULE = (
    "Nome canonico institucional aprovado como tudo-para-ia-mais-humana-platform; "
    "o repositorio local historico tudo-para-ia-mais-humana e o alias "
    "tudo-para-ia-mais-humana-plataform permanecem aliases de compatibilidade "
    "ate migracao Git/MCP sincronizada, sem duplicar repositorio nem sobrescrever historico."
)


@dataclass(frozen=True, slots=True)
class CanonicalIdentityPolicy:
    """Serializable policy consumed by MCP contracts, reports, and gates."""

    canonical_project_id: str = CANONICAL_PROJECT_ID
    current_project_id: str = CURRENT_PROJECT_ID
    legacy_project_ids: tuple[str, ...] = LEGACY_PROJECT_IDS
    accepted_project_ids: tuple[str, ...] = ACCEPTED_PROJECT_IDS
    decision_status: str = CANONICAL_DECISION_STATUS
    decision_source: str = CANONICAL_DECISION_SOURCE
    central_folder_name: str = CENTRAL_FOLDER_NAME
    control_plane_id: str = MCP_CONTROL_PLANE_ID
    compatibility_rule: str = CANONICAL_COMPATIBILITY_RULE

    def to_dict(self) -> dict[str, Any]:
        return as_plain_data(self)


def canonical_identity_policy() -> CanonicalIdentityPolicy:
    """Return the immutable identity policy for this round."""

    return CanonicalIdentityPolicy()


def canonicalize_project_id(project_id: str) -> str:
    """Map known historical ids to the approved canonical project id."""

    normalized = str(project_id).strip()
    if normalized in ACCEPTED_PROJECT_IDS:
        return CANONICAL_PROJECT_ID
    return normalized


def is_accepted_project_id(project_id: str) -> bool:
    """Return whether an id is canonical or an approved compatibility alias."""

    return canonicalize_project_id(project_id) == CANONICAL_PROJECT_ID
