from importlib.resources import files
from typing import Union

from .. import RELAIData
from ..data.data_type import DataType


class FeedbackQuestion(RELAIData):
    """Data model for a feedback question component.

    Args:
        question (str): Question to be asked.
        type (Union[DataType, str]): Type of the question. Must be either a `DataType` object
            or the string "contextSelection".
        modifies (list[str]): A list of one or more strings that uniquely determine what needs to be changed in the application result.
        context (list[RELAIData], optional): A list of RELAIData components to show as context for the question.
            Defaults to None.
        column_span (int, optional): Number of columns the component should span. Defaults to 12.
    """

    schema_file: str = files("relai_visualization").joinpath("schema/components/feedback-question.schema.json")

    def __init__(
        self,
        question: str,
        type: Union[DataType, str],
        modifies: list[str],
        context: list[RELAIData] = None,
        column_span: int = 12,
    ) -> None:
        if context:
            # TODO: This check is broader than what is allowed by the schema.
            # They should be matched once the schema is updated.
            if len(context) < 1:
                raise ValueError("Context cannot be empty when provided.")
            if any([not isinstance(c, RELAIData) for c in context]):
                raise ValueError("Invalid context. Make sure all elements in `context` are of type RELAIData.")
            context = [c.data for c in context]

        if isinstance(type, DataType):
            type = type.data
        elif type != "contextSelection":
            raise ValueError("Invalid type.")

        super().__init__(
            componentName="feedback-question",
            columnSpan=column_span,
            question=question,
            context=context,
            type=type,
            modifies=modifies,
        )
