"""
Generic configuration resolution middleware for user-specific settings and feature availability.

This module provides a pluggable interface for resolving user-specific configuration
and determining feature availability. The default implementation provides passthrough
behavior that allows all operations.
"""

from typing import Any, Dict, List
from solace_ai_connector.common.log import log

LOG_IDENTIFIER = "[ConfigResolver]"


class ConfigResolver:
    """
    Resolves user-specific configuration and determines feature availability.

    This class provides a generic interface for configuration resolution that can be
    extended or replaced at runtime. The default implementation is permissive and
    allows all operations.
    """

    @staticmethod
    async def resolve_user_config(
        user_identity: Any, gateway_context: Dict[str, Any], base_config: Dict[str, Any]
    ) -> Dict[str, Any]:
        """
        Resolve user-specific configuration settings.

        Args:
            user_identity: Identifier for the user (username, email, ID, etc.)
            gateway_context: Context information from the gateway (gateway_id, etc.)
            base_config: Base configuration to start with

        Returns:
            Dict containing user-specific configuration. Default implementation
            returns base_config unchanged.
        """
        log.debug(
            "%s Resolving user config for identity: %s (default implementation)",
            LOG_IDENTIFIER,
            user_identity,
        )
        return base_config

    @staticmethod
    def is_feature_enabled(
        user_config: Dict[str, Any],
        feature_descriptor: Dict[str, Any],
        context: Dict[str, Any],
    ) -> bool:
        """
        Check if a feature is enabled for the user.

        Args:
            user_config: User-specific configuration from resolve_user_config
            feature_descriptor: Description of the feature being checked
            context: Additional context for the feature check

        Returns:
            True if feature is enabled, False otherwise. Default implementation
            returns True (all features enabled).
        """
        feature_type = feature_descriptor.get("feature_type", "unknown")
        feature_name = feature_descriptor.get("function_name", "unknown")

        log.debug(
            "%s Feature check for %s:%s - enabled (default implementation)",
            LOG_IDENTIFIER,
            feature_type,
            feature_name,
        )
        return True

    @staticmethod
    def validate_operation_config(
        user_config: Dict[str, Any],
        operation_spec: Dict[str, Any],
        validation_context: Dict[str, Any],
    ) -> Dict[str, Any]:
        """
        Validate operation against user configuration.

        Args:
            user_config: User-specific configuration from resolve_user_config
            operation_spec: Specification of the operation being validated
            validation_context: Additional context for validation

        Returns:
            Dict with validation result. Must include 'valid' boolean key.
            Default implementation returns valid=True for all operations.
        """
        operation_type = operation_spec.get("operation_type", "unknown")

        log.debug(
            "%s Operation validation for %s - valid (default implementation)",
            LOG_IDENTIFIER,
            operation_type,
        )

        return {
            "valid": True,
            "reason": "default_validation",
            "operation_type": operation_type,
        }

    @staticmethod
    def filter_available_options(
        user_config: Dict[str, Any],
        available_options: List[Dict[str, Any]],
        filter_context: Dict[str, Any],
    ) -> List[Dict[str, Any]]:
        """
        Filter available options based on user configuration.

        Args:
            user_config: User-specific configuration from resolve_user_config
            available_options: List of available options to filter
            filter_context: Additional context for filtering

        Returns:
            Filtered list of options. Default implementation returns all options.
        """
        log.debug(
            "%s Filtering %d options - returning all (default implementation)",
            LOG_IDENTIFIER,
            len(available_options),
        )
        return available_options
