"""
Fluent accessor classes for Questionnaire resources.

Auto-generated from OpenAPI specification.
"""

from typing import TYPE_CHECKING, Any, Dict, Optional, Union

from ..models import (
    PaginatedResponse,
    QuestionnaireDuplicate,
    QuestionnaireResponse,
)

if TYPE_CHECKING:
    from ..client import AsyncCredoAI, CredoAI


# =============================================================================
# Questionnaire Fluent Accessors (Sync)
# =============================================================================


class QuestionnaireDuplicateAccessor:
    """Manage duplicate for a specific questionnaire.

    This accessor provides methods to add
    duplicate associated with a questionnaire.

    Example:
        >>> client.questionnaire("questionnaire-id").duplicate.list()
        >>> client.questionnaire("questionnaire-id").duplicate.add("duplicate-id")
    """

    def __init__(self, client: "CredoAI", questionnaire_id: str):
        """Initialize the duplicate accessor.

        Args:
            client: The CredoAI client instance
            questionnaire_id: The ID of the parent questionnaire
        """
        self._client = client
        self._questionnaire_id = questionnaire_id

    def add(
        self, duplicate: "Union[QuestionnaireDuplicate, QuestionnaireResponse]"
    ) -> "QuestionnaireResponse":
        """Add a duplicate to this questionnaire.

        Args:
            duplicate: A QuestionnaireDuplicate object, or a QuestionnaireResponse to extract fields from

        Returns:
            QuestionnaireResponse: The added duplicate

        Examples:
            >>> # Using request model directly
            >>> questionnaire.duplicate.add(QuestionnaireDuplicate(...))
            >>> # Using a fetched response object (extracts matching fields)
            >>> fetched = client.duplicate.get("id")
            >>> questionnaire.duplicate.add(fetched)
        """
        if isinstance(duplicate, QuestionnaireDuplicate):
            data = duplicate
        else:
            # Extract fields from response object to create request model
            field_values = {
                field: getattr(duplicate, field)
                for field in QuestionnaireDuplicate.model_fields
                if hasattr(duplicate, field)
            }
            data = QuestionnaireDuplicate(**field_values)

        return self._client._make_request(
            method="POST",
            url=f"/questionnaires/{self._questionnaire_id}/duplicate",
            data=data,
            response_model=QuestionnaireResponse,
        )


class Questionnaire:
    """Fluent accessor for a specific questionnaire.

    Provides access to related resources through property accessors that enable
    intuitive chaining.

    Example:
        >>> questionnaire = client.questionnaire("questionnaire-id")
        >>> questionnaire.duplicate.list()
        >>> questionnaire.duplicate.add("duplicate-id")
    """

    def __init__(self, client: "CredoAI", questionnaire_id: str):
        """Initialize the questionnaire accessor.

        Args:
            client: The CredoAI client instance
            questionnaire_id: The ID of the questionnaire
        """
        self._client = client
        self._questionnaire_id = questionnaire_id

    @property
    def duplicate(self) -> "QuestionnaireDuplicateAccessor":
        """Access duplicate associated with this questionnaire.

        Returns:
            QuestionnaireDuplicateAccessor: Accessor for duplicate operations
        """
        return QuestionnaireDuplicateAccessor(self._client, self._questionnaire_id)


# =============================================================================
# Questionnaire Fluent Accessors (Async)
# =============================================================================


class AsyncQuestionnaireDuplicateAccessor:
    """Async manage duplicate for a specific questionnaire.

    This accessor provides async methods to add
    duplicate associated with a questionnaire.

    Example:
        >>> await client.questionnaire("questionnaire-id").duplicate.list()
        >>> await client.questionnaire("questionnaire-id").duplicate.add("duplicate-id")
    """

    def __init__(self, client: "AsyncCredoAI", questionnaire_id: str):
        """Initialize the async duplicate accessor.

        Args:
            client: The AsyncCredoAI client instance
            questionnaire_id: The ID of the parent questionnaire
        """
        self._client = client
        self._questionnaire_id = questionnaire_id

    async def add(
        self, duplicate: "Union[QuestionnaireDuplicate, QuestionnaireResponse]"
    ) -> "QuestionnaireResponse":
        """Add a duplicate to this questionnaire.

        Args:
            duplicate: A QuestionnaireDuplicate object, or a QuestionnaireResponse to extract fields from

        Returns:
            QuestionnaireResponse: The added duplicate

        Examples:
            >>> # Using request model directly
            >>> questionnaire.duplicate.add(QuestionnaireDuplicate(...))
            >>> # Using a fetched response object (extracts matching fields)
            >>> fetched = client.duplicate.get("id")
            >>> questionnaire.duplicate.add(fetched)
        """
        if isinstance(duplicate, QuestionnaireDuplicate):
            data = duplicate
        else:
            # Extract fields from response object to create request model
            field_values = {
                field: getattr(duplicate, field)
                for field in QuestionnaireDuplicate.model_fields
                if hasattr(duplicate, field)
            }
            data = QuestionnaireDuplicate(**field_values)

        return await self._client._make_request_async(
            method="POST",
            url=f"/questionnaires/{self._questionnaire_id}/duplicate",
            data=data,
            response_model=QuestionnaireResponse,
        )


class AsyncQuestionnaire:
    """Async fluent accessor for a specific questionnaire.

    Provides access to related resources through property accessors that enable
    intuitive chaining with async/await.

    Example:
        >>> questionnaire = client.questionnaire("questionnaire-id")
        >>> await questionnaire.duplicate.list()
        >>> await questionnaire.duplicate.add("duplicate-id")
    """

    def __init__(self, client: "AsyncCredoAI", questionnaire_id: str):
        """Initialize the async questionnaire accessor.

        Args:
            client: The AsyncCredoAI client instance
            questionnaire_id: The ID of the questionnaire
        """
        self._client = client
        self._questionnaire_id = questionnaire_id

    @property
    def duplicate(self) -> "AsyncQuestionnaireDuplicateAccessor":
        """Access duplicate associated with this questionnaire.

        Returns:
            AsyncQuestionnaireDuplicateAccessor: Async accessor for duplicate operations
        """
        return AsyncQuestionnaireDuplicateAccessor(self._client, self._questionnaire_id)


# =============================================================================
# Export list
# =============================================================================

__all__ = [
    "Questionnaire",
    "AsyncQuestionnaire",
    "QuestionnaireDuplicateAccessor",
    "AsyncQuestionnaireDuplicateAccessor",
]
