"""Authentication dependency for FastAPI routes."""

from __future__ import annotations

from dataclasses import dataclass

from fastapi import Request


@dataclass
class AuthInfo:
    """Standardized access to the authenticated user in route handlers."""

    auth_method: str | None = None  # "session", "api_key", or None
    user: dict | None = None  # user_id, github_id, login, etc.

    @property
    def is_api_key(self) -> bool:
        return self.auth_method == "api_key"

    @property
    def is_session(self) -> bool:
        return self.auth_method == "session"

    @property
    def user_id(self) -> int | None:
        if self.user:
            return self.user.get("user_id")
        return None


async def get_auth(request: Request) -> AuthInfo:
    """FastAPI dependency that extracts auth info from request state.

    Reads ``request.state.auth_method`` and ``request.state.user`` which are
    set by :class:`~peon_mcp.web.AuthMiddleware`.
    """
    auth_method = getattr(request.state, "auth_method", None)
    user = getattr(request.state, "user", None)
    return AuthInfo(auth_method=auth_method, user=user)
