"""UUID parsing helpers for gRPC request validation.

Provides parse_*_id functions that validate and convert string IDs to UUIDs,
aborting with INVALID_ARGUMENT if the format is invalid.
"""

from __future__ import annotations

from uuid import UUID

from noteflow.config.constants import (
    ERROR_INVALID_ENTITY_ID_FORMAT,
    ERROR_INVALID_INTEGRATION_ID_FORMAT,
    ERROR_INVALID_MEETING_ID_FORMAT,
    ERROR_INVALID_PROJECT_ID_FORMAT,
    ERROR_INVALID_WEBHOOK_ID_FORMAT,
    ERROR_INVALID_WORKSPACE_ID_FORMAT,
)

from ._abort import AbortableContext, abort_invalid_argument


async def parse_workspace_id(
    workspace_id_str: str,
    context: AbortableContext,
) -> UUID:
    """Parse workspace_id string to UUID, aborting with INVALID_ARGUMENT if invalid.

    Args:
        workspace_id_str: The workspace ID string from request.
        context: gRPC servicer context for abort.

    Returns:
        Parsed UUID.

    Raises:
        grpc.RpcError: If workspace_id format is invalid.
    """
    try:
        return UUID(workspace_id_str)
    except ValueError:
        await abort_invalid_argument(context, ERROR_INVALID_WORKSPACE_ID_FORMAT)


async def parse_project_id(
    project_id_str: str,
    context: AbortableContext,
) -> UUID:
    """Parse project_id string to UUID, aborting with INVALID_ARGUMENT if invalid.

    Args:
        project_id_str: The project ID string from request.
        context: gRPC servicer context for abort.

    Returns:
        Parsed UUID.

    Raises:
        grpc.RpcError: If project_id format is invalid.
    """
    try:
        return UUID(project_id_str)
    except ValueError:
        await abort_invalid_argument(context, ERROR_INVALID_PROJECT_ID_FORMAT)


async def parse_meeting_id(
    meeting_id_str: str,
    context: AbortableContext,
) -> UUID:
    """Parse meeting_id string to UUID, aborting with INVALID_ARGUMENT if invalid.

    Args:
        meeting_id_str: The meeting ID string from request.
        context: gRPC servicer context for abort.

    Returns:
        Parsed UUID.

    Raises:
        grpc.RpcError: If meeting_id format is invalid.
    """
    try:
        return UUID(meeting_id_str)
    except ValueError:
        await abort_invalid_argument(context, ERROR_INVALID_MEETING_ID_FORMAT)
        raise  # Unreachable: abort raises, but helps type checker


async def parse_integration_id(
    integration_id_str: str,
    context: AbortableContext,
) -> UUID:
    """Parse integration_id string to UUID, aborting with INVALID_ARGUMENT if invalid.

    Args:
        integration_id_str: The integration ID string from request.
        context: gRPC servicer context for abort.

    Returns:
        Parsed UUID.

    Raises:
        grpc.RpcError: If integration_id format is invalid.
    """
    try:
        return UUID(integration_id_str)
    except ValueError:
        await abort_invalid_argument(context, ERROR_INVALID_INTEGRATION_ID_FORMAT)


async def parse_webhook_id(
    webhook_id_str: str,
    context: AbortableContext,
) -> UUID:
    """Parse webhook_id string to UUID, aborting with INVALID_ARGUMENT if invalid.

    Args:
        webhook_id_str: The webhook ID string from request.
        context: gRPC servicer context for abort.

    Returns:
        Parsed UUID.

    Raises:
        grpc.RpcError: If webhook_id format is invalid.
    """
    try:
        return UUID(webhook_id_str)
    except ValueError:
        await abort_invalid_argument(context, ERROR_INVALID_WEBHOOK_ID_FORMAT)
        raise  # Unreachable: abort raises, but helps type checker


async def parse_entity_id(
    entity_id_str: str,
    context: AbortableContext,
) -> UUID:
    """Parse entity_id string to UUID, aborting with INVALID_ARGUMENT if invalid.

    Args:
        entity_id_str: The entity ID string from request.
        context: gRPC servicer context for abort.

    Returns:
        Parsed UUID.

    Raises:
        grpc.RpcError: If entity_id format is invalid.
    """
    try:
        return UUID(entity_id_str)
    except ValueError:
        await abort_invalid_argument(context, ERROR_INVALID_ENTITY_ID_FORMAT)
