"""Meta/instance endpoints for diagnostics and identification."""

from fastapi import APIRouter

from app.config import settings
from app.schemas.common import InstanceMetaResponse
from app.services.local_bootstrap import LocalBootstrapService

router = APIRouter(prefix="/meta", tags=["Meta"])


@router.get("/instance", response_model=InstanceMetaResponse)
async def get_instance_meta() -> InstanceMetaResponse:
    """
    Get instance metadata.

    This endpoint is stable and works even before tenant bootstrap completes.
    Use it for diagnostics, health checks, and instance identification.

    Returns:
        - instance_id: Unique instance identifier (from Hub activation)
        - local_mode: Whether running in single-tenant local mode
        - version: Application version
        - tenant_id: Local tenant ID (null if not bootstrapped or in multi-tenant mode)
        - bootstrap_status: Detailed bootstrap status for debugging
    """
    tenant_id = LocalBootstrapService.get_local_tenant_id()

    return InstanceMetaResponse(
        instance_id=settings.INSTANCE_ID,
        local_mode=settings.LOCAL_MODE,
        version=settings.APP_VERSION,
        tenant_id=str(tenant_id) if tenant_id else None,
        bootstrap_status=LocalBootstrapService.get_bootstrap_status(),
    )
