import copy
import functools
import inspect
import warnings
from datetime import datetime
from types import FunctionType
from typing import Any, Dict, List, Optional, Type, Union

import dill
import pandas as pd
import pyarrow
from typeguard import typechecked

from feast.base_feature_view import BaseFeatureView
from feast.batch_feature_view import BatchFeatureView
from feast.data_source import RequestSource
from feast.errors import RegistryInferenceFailure, SpecifiedFeaturesNotPresentError
from feast.feature_view import FeatureView
from feast.feature_view_projection import FeatureViewProjection
from feast.field import Field, from_value_type
from feast.protos.feast.core.OnDemandFeatureView_pb2 import (
    OnDemandFeatureView as OnDemandFeatureViewProto,
)
from feast.protos.feast.core.OnDemandFeatureView_pb2 import (
    OnDemandFeatureViewMeta,
    OnDemandFeatureViewSpec,
    OnDemandSource,
)
from feast.protos.feast.core.Transformation_pb2 import (
    FeatureTransformationV2 as FeatureTransformationProto,
)
from feast.protos.feast.core.Transformation_pb2 import (
    UserDefinedFunctionV2 as UserDefinedFunctionProto,
)
from feast.transformation.pandas_transformation import PandasTransformation
from feast.transformation.python_transformation import PythonTransformation
from feast.transformation.substrait_transformation import SubstraitTransformation
from feast.usage import log_exceptions
from feast.value_type import ValueType

warnings.simplefilter("once", DeprecationWarning)


@typechecked
class OnDemandFeatureView(BaseFeatureView):
    """
    [Experimental] An OnDemandFeatureView defines a logical group of features that are
    generated by applying a transformation on a set of input sources, such as feature
    views and request data sources.

    Attributes:
        name: The unique name of the on demand feature view.
        features: The list of features in the output of the on demand feature view.
        source_feature_view_projections: A map from input source names to actual input
            sources with type FeatureViewProjection.
        source_request_sources: A map from input source names to the actual input
            sources with type RequestSource.
        feature_transformation: The user defined transformation.
        description: A human-readable description.
        tags: A dictionary of key-value pairs to store arbitrary metadata.
        owner: The owner of the on demand feature view, typically the email of the primary
            maintainer.
    """

    name: str
    features: List[Field]
    source_feature_view_projections: Dict[str, FeatureViewProjection]
    source_request_sources: Dict[str, RequestSource]
    feature_transformation: Union[
        PandasTransformation, PythonTransformation, SubstraitTransformation
    ]
    mode: str
    description: str
    tags: Dict[str, str]
    owner: str

    @log_exceptions  # noqa: C901
    def __init__(  # noqa: C901
        self,
        *,
        name: str,
        schema: List[Field],
        sources: List[
            Union[
                FeatureView,
                RequestSource,
                FeatureViewProjection,
            ]
        ],
        udf: Optional[FunctionType] = None,
        udf_string: str = "",
        feature_transformation: Union[
            PandasTransformation, PythonTransformation, SubstraitTransformation
        ],
        mode: str = "pandas",
        description: str = "",
        tags: Optional[Dict[str, str]] = None,
        owner: str = "",
    ):
        """
        Creates an OnDemandFeatureView object.

        Args:
            name: The unique name of the on demand feature view.
            schema: The list of features in the output of the on demand feature view, after
                the transformation has been applied.
            sources: A map from input source names to the actual input sources, which may be
                feature views, or request data sources. These sources serve as inputs to the udf,
                which will refer to them by name.
            udf (deprecated): The user defined transformation function, which must take pandas
                dataframes as inputs.
            udf_string (deprecated): The source code version of the udf (for diffing and displaying in Web UI)
            feature_transformation: The user defined transformation.
            mode: Mode of execution (e.g., Pandas or Python native)
            description (optional): A human-readable description.
            tags (optional): A dictionary of key-value pairs to store arbitrary metadata.
            owner (optional): The owner of the on demand feature view, typically the email
                of the primary maintainer.
        """
        super().__init__(
            name=name,
            features=schema,
            description=description,
            tags=tags,
            owner=owner,
        )

        if mode not in {"python", "pandas", "substrait"}:
            raise Exception(
                f"Unknown mode {mode}. OnDemandFeatureView only supports python or pandas UDFs and substrait."
            )
        else:
            self.mode = mode
        if not feature_transformation:
            if udf:
                warnings.warn(
                    "udf and udf_string parameters are deprecated. Please use transformation=PandasTransformation(udf, udf_string) instead.",
                    DeprecationWarning,
                )
                # Note inspecting the return signature won't work with isinstance so this is the best alternative
                if mode == "pandas":
                    feature_transformation = PandasTransformation(udf, udf_string)
                elif mode == "python":
                    feature_transformation = PythonTransformation(udf, udf_string)
                else:
                    pass
            else:
                raise Exception(
                    "OnDemandFeatureView needs to be initialized with either feature_transformation or udf arguments"
                )

        self.source_feature_view_projections: Dict[str, FeatureViewProjection] = {}
        self.source_request_sources: Dict[str, RequestSource] = {}
        for odfv_source in sources:
            if isinstance(odfv_source, RequestSource):
                self.source_request_sources[odfv_source.name] = odfv_source
            elif isinstance(odfv_source, FeatureViewProjection):
                self.source_feature_view_projections[odfv_source.name] = odfv_source
            else:
                self.source_feature_view_projections[odfv_source.name] = (
                    odfv_source.projection
                )

        self.feature_transformation = feature_transformation

    @property
    def proto_class(self) -> Type[OnDemandFeatureViewProto]:
        return OnDemandFeatureViewProto

    def __copy__(self):
        fv = OnDemandFeatureView(
            name=self.name,
            schema=self.features,
            sources=list(self.source_feature_view_projections.values())
            + list(self.source_request_sources.values()),
            feature_transformation=self.feature_transformation,
            mode=self.mode,
            description=self.description,
            tags=self.tags,
            owner=self.owner,
        )
        fv.projection = copy.copy(self.projection)
        return fv

    def __eq__(self, other):
        if not isinstance(other, OnDemandFeatureView):
            raise TypeError(
                "Comparisons should only involve OnDemandFeatureView class objects."
            )

        if not super().__eq__(other):
            return False

        if (
            self.source_feature_view_projections
            != other.source_feature_view_projections
            or self.source_request_sources != other.source_request_sources
            or self.mode != other.mode
            or self.feature_transformation != other.feature_transformation
        ):
            return False

        return True

    def __hash__(self):
        return super().__hash__()

    def to_proto(self) -> OnDemandFeatureViewProto:
        """
        Converts an on demand feature view object to its protobuf representation.

        Returns:
            A OnDemandFeatureViewProto protobuf.
        """
        meta = OnDemandFeatureViewMeta()
        if self.created_timestamp:
            meta.created_timestamp.FromDatetime(self.created_timestamp)
        if self.last_updated_timestamp:
            meta.last_updated_timestamp.FromDatetime(self.last_updated_timestamp)
        sources = {}
        for source_name, fv_projection in self.source_feature_view_projections.items():
            sources[source_name] = OnDemandSource(
                feature_view_projection=fv_projection.to_proto()
            )
        for (
            source_name,
            request_sources,
        ) in self.source_request_sources.items():
            sources[source_name] = OnDemandSource(
                request_data_source=request_sources.to_proto()
            )

        feature_transformation = FeatureTransformationProto(
            user_defined_function=self.feature_transformation.to_proto()
            if isinstance(
                self.feature_transformation,
                (PandasTransformation, PythonTransformation),
            )
            else None,
            substrait_transformation=self.feature_transformation.to_proto()
            if isinstance(self.feature_transformation, SubstraitTransformation)
            else None,
        )
        spec = OnDemandFeatureViewSpec(
            name=self.name,
            features=[feature.to_proto() for feature in self.features],
            sources=sources,
            feature_transformation=feature_transformation,
            mode=self.mode,
            description=self.description,
            tags=self.tags,
            owner=self.owner,
        )

        return OnDemandFeatureViewProto(spec=spec, meta=meta)

    @classmethod
    def from_proto(
        cls,
        on_demand_feature_view_proto: OnDemandFeatureViewProto,
        skip_udf: bool = False,
    ):
        """
        Creates an on demand feature view from a protobuf representation.

        Args:
            on_demand_feature_view_proto: A protobuf representation of an on-demand feature view.
            skip_udf: A boolean indicating whether to skip loading the udf

        Returns:
            A OnDemandFeatureView object based on the on-demand feature view protobuf.
        """
        sources = []
        for (
            _,
            on_demand_source,
        ) in on_demand_feature_view_proto.spec.sources.items():
            if on_demand_source.WhichOneof("source") == "feature_view":
                sources.append(
                    FeatureView.from_proto(on_demand_source.feature_view).projection
                )
            elif on_demand_source.WhichOneof("source") == "feature_view_projection":
                sources.append(
                    FeatureViewProjection.from_proto(
                        on_demand_source.feature_view_projection
                    )
                )
            else:
                sources.append(
                    RequestSource.from_proto(on_demand_source.request_data_source)
                )

        if (
            on_demand_feature_view_proto.spec.feature_transformation.WhichOneof(
                "transformation"
            )
            == "user_defined_function"
            and on_demand_feature_view_proto.spec.feature_transformation.user_defined_function.body_text
            != ""
            and on_demand_feature_view_proto.spec.mode == "pandas"
        ):
            transformation = PandasTransformation.from_proto(
                on_demand_feature_view_proto.spec.feature_transformation.user_defined_function
            )
        elif (
            on_demand_feature_view_proto.spec.feature_transformation.WhichOneof(
                "transformation"
            )
            == "user_defined_function"
            and on_demand_feature_view_proto.spec.feature_transformation.user_defined_function.body_text
            != ""
            and on_demand_feature_view_proto.spec.mode == "python"
        ):
            transformation = PythonTransformation.from_proto(
                on_demand_feature_view_proto.spec.feature_transformation.user_defined_function
            )
        elif (
            on_demand_feature_view_proto.spec.feature_transformation.WhichOneof(
                "transformation"
            )
            == "substrait_transformation"
        ):
            transformation = SubstraitTransformation.from_proto(
                on_demand_feature_view_proto.spec.feature_transformation.substrait_transformation
            )
        elif (
            hasattr(on_demand_feature_view_proto.spec, "user_defined_function")
            and on_demand_feature_view_proto.spec.feature_transformation.user_defined_function.body_text
            == ""
        ):
            backwards_compatible_udf = UserDefinedFunctionProto(
                name=on_demand_feature_view_proto.spec.user_defined_function.name,
                body=on_demand_feature_view_proto.spec.user_defined_function.body,
                body_text=on_demand_feature_view_proto.spec.user_defined_function.body_text,
            )
            transformation = PandasTransformation.from_proto(
                user_defined_function_proto=backwards_compatible_udf,
            )
        else:
            raise Exception("At least one transformation type needs to be provided")

        on_demand_feature_view_obj = cls(
            name=on_demand_feature_view_proto.spec.name,
            schema=[
                Field(
                    name=feature.name,
                    dtype=from_value_type(ValueType(feature.value_type)),
                )
                for feature in on_demand_feature_view_proto.spec.features
            ],
            sources=sources,
            feature_transformation=transformation,
            mode=on_demand_feature_view_proto.spec.mode,
            description=on_demand_feature_view_proto.spec.description,
            tags=dict(on_demand_feature_view_proto.spec.tags),
            owner=on_demand_feature_view_proto.spec.owner,
        )

        # FeatureViewProjections are not saved in the OnDemandFeatureView proto.
        # Create the default projection.
        on_demand_feature_view_obj.projection = FeatureViewProjection.from_definition(
            on_demand_feature_view_obj
        )

        if on_demand_feature_view_proto.meta.HasField("created_timestamp"):
            on_demand_feature_view_obj.created_timestamp = (
                on_demand_feature_view_proto.meta.created_timestamp.ToDatetime()
            )
        if on_demand_feature_view_proto.meta.HasField("last_updated_timestamp"):
            on_demand_feature_view_obj.last_updated_timestamp = (
                on_demand_feature_view_proto.meta.last_updated_timestamp.ToDatetime()
            )

        return on_demand_feature_view_obj

    def get_request_data_schema(self) -> Dict[str, ValueType]:
        schema: Dict[str, ValueType] = {}
        for request_source in self.source_request_sources.values():
            if isinstance(request_source.schema, List):
                new_schema = {}
                for field in request_source.schema:
                    new_schema[field.name] = field.dtype.to_value_type()
                schema.update(new_schema)
            elif isinstance(request_source.schema, Dict):
                schema.update(request_source.schema)
            else:
                raise Exception(
                    f"Request source schema is not correct type: ${str(type(request_source.schema))}"
                )
        return schema

    def _get_projected_feature_name(self, feature: str) -> str:
        return f"{self.projection.name_to_use()}__{feature}"

    def transform_arrow(
        self,
        pa_table: pyarrow.Table,
        full_feature_names: bool = False,
    ) -> pyarrow.Table:
        if not isinstance(pa_table, pyarrow.Table):
            raise TypeError("transform_arrow only accepts pyarrow.Table")
        columns_to_cleanup = []
        for source_fv_projection in self.source_feature_view_projections.values():
            for feature in source_fv_projection.features:
                full_feature_ref = f"{source_fv_projection.name}__{feature.name}"
                if full_feature_ref in pa_table.column_names:
                    # Make sure the partial feature name is always present
                    pa_table = pa_table.append_column(
                        feature.name, pa_table[full_feature_ref]
                    )
                    # pa_table[feature.name] = pa_table[full_feature_ref]
                    columns_to_cleanup.append(feature.name)
                elif feature.name in pa_table.column_names:
                    # Make sure the full feature name is always present
                    # pa_table[full_feature_ref] = pa_table[feature.name]
                    pa_table = pa_table.append_column(
                        full_feature_ref, pa_table[feature.name]
                    )
                    columns_to_cleanup.append(full_feature_ref)

        df_with_transformed_features: pyarrow.Table = (
            self.feature_transformation.transform_arrow(pa_table)
        )

        # Work out whether the correct columns names are used.
        rename_columns: Dict[str, str] = {}
        for feature in self.features:
            short_name = feature.name
            long_name = self._get_projected_feature_name(feature.name)
            if (
                short_name in df_with_transformed_features.column_names
                and full_feature_names
            ):
                rename_columns[short_name] = long_name
            elif not full_feature_names:
                rename_columns[long_name] = short_name

        # Cleanup extra columns used for transformation
        for col in columns_to_cleanup:
            if col in df_with_transformed_features.column_names:
                df_with_transformed_features = df_with_transformed_features.dtop(col)
        return df_with_transformed_features.rename_columns(
            [
                rename_columns.get(c, c)
                for c in df_with_transformed_features.column_names
            ]
        )

    def get_transformed_features_df(
        self,
        df_with_features: pd.DataFrame,
        full_feature_names: bool = False,
    ) -> pd.DataFrame:
        # Apply on demand transformations
        if not isinstance(df_with_features, pd.DataFrame):
            raise TypeError("get_transformed_features_df only accepts pd.DataFrame")
        columns_to_cleanup = []
        for source_fv_projection in self.source_feature_view_projections.values():
            for feature in source_fv_projection.features:
                full_feature_ref = f"{source_fv_projection.name}__{feature.name}"
                if full_feature_ref in df_with_features.keys():
                    # Make sure the partial feature name is always present
                    df_with_features[feature.name] = df_with_features[full_feature_ref]
                    columns_to_cleanup.append(feature.name)
                elif feature.name in df_with_features.keys():
                    # Make sure the full feature name is always present
                    df_with_features[full_feature_ref] = df_with_features[feature.name]
                    columns_to_cleanup.append(full_feature_ref)

        # Compute transformed values and apply to each result row
        df_with_transformed_features: pd.DataFrame = (
            self.feature_transformation.transform(df_with_features)
        )

        # Work out whether the correct columns names are used.
        rename_columns: Dict[str, str] = {}
        for feature in self.features:
            short_name = feature.name
            long_name = self._get_projected_feature_name(feature.name)
            if (
                short_name in df_with_transformed_features.columns
                and full_feature_names
            ):
                rename_columns[short_name] = long_name
            elif not full_feature_names:
                # Long name must be in dataframe.
                rename_columns[long_name] = short_name

        # Cleanup extra columns used for transformation
        df_with_features.drop(columns=columns_to_cleanup, inplace=True)
        return df_with_transformed_features.rename(columns=rename_columns)

    def get_transformed_features_dict(
        self,
        feature_dict: Dict[str, Any],  # type: ignore
    ) -> Dict[str, Any]:
        # we need a mapping from full feature name to short and back to do a renaming
        # The simplest thing to do is to make the full reference, copy the columns with the short reference
        # and rerun
        columns_to_cleanup: List[str] = []
        for source_fv_projection in self.source_feature_view_projections.values():
            for feature in source_fv_projection.features:
                full_feature_ref = f"{source_fv_projection.name}__{feature.name}"
                if full_feature_ref in feature_dict.keys():
                    # Make sure the partial feature name is always present
                    feature_dict[feature.name] = feature_dict[full_feature_ref]
                    columns_to_cleanup.append(str(feature.name))
                elif feature.name in feature_dict.keys():
                    # Make sure the full feature name is always present
                    feature_dict[full_feature_ref] = feature_dict[feature.name]
                    columns_to_cleanup.append(str(full_feature_ref))

        output_dict: Dict[str, Any] = self.feature_transformation.transform(
            feature_dict
        )
        for feature_name in columns_to_cleanup:
            del output_dict[feature_name]
        return output_dict

    def get_transformed_features(
        self,
        features: Union[Dict[str, Any], pd.DataFrame],
        full_feature_names: bool = False,
    ) -> Union[Dict[str, Any], pd.DataFrame]:
        # TODO: classic inheritance pattern....maybe fix this
        if self.mode == "python" and isinstance(features, Dict):
            # note full_feature_names is not needed for the dictionary
            return self.get_transformed_features_dict(
                feature_dict=features,
            )
        elif self.mode in {"pandas", "substrait"} and isinstance(
            features, pd.DataFrame
        ):
            return self.get_transformed_features_df(
                df_with_features=features,
                full_feature_names=full_feature_names,
            )
        else:
            raise Exception(
                f'Invalid OnDemandFeatureMode: {self.mode}. Expected one of "pandas" or "python".'
            )

    def infer_features(self) -> None:
        inferred_features = self.feature_transformation.infer_features(
            self._construct_random_input()
        )

        if self.features:
            missing_features = []
            for specified_feature in self.features:
                if specified_feature not in inferred_features:
                    missing_features.append(specified_feature)
            if missing_features:
                raise SpecifiedFeaturesNotPresentError(
                    missing_features, inferred_features, self.name
                )
        else:
            self.features = inferred_features

        if not self.features:
            raise RegistryInferenceFailure(
                "OnDemandFeatureView",
                f"Could not infer Features for the feature view '{self.name}'.",
            )

    def _construct_random_input(self) -> Dict[str, List[Any]]:
        rand_dict_value: Dict[ValueType, List[Any]] = {
            ValueType.BYTES: [str.encode("hello world")],
            ValueType.STRING: ["hello world"],
            ValueType.INT32: [1],
            ValueType.INT64: [1],
            ValueType.DOUBLE: [1.0],
            ValueType.FLOAT: [1.0],
            ValueType.BOOL: [True],
            ValueType.UNIX_TIMESTAMP: [datetime.utcnow()],
            ValueType.BYTES_LIST: [[str.encode("hello world")]],
            ValueType.STRING_LIST: [["hello world"]],
            ValueType.INT32_LIST: [[1]],
            ValueType.INT64_LIST: [[1]],
            ValueType.DOUBLE_LIST: [[1.0]],
            ValueType.FLOAT_LIST: [[1.0]],
            ValueType.BOOL_LIST: [[True]],
            ValueType.UNIX_TIMESTAMP_LIST: [[datetime.utcnow()]],
        }

        feature_dict = {}
        for feature_view_projection in self.source_feature_view_projections.values():
            for feature in feature_view_projection.features:
                feature_dict[f"{feature_view_projection.name}__{feature.name}"] = (
                    rand_dict_value.get(feature.dtype.to_value_type(), [None])
                )
                feature_dict[f"{feature.name}"] = rand_dict_value.get(
                    feature.dtype.to_value_type(), [None]
                )
        for request_data in self.source_request_sources.values():
            for field in request_data.schema:
                feature_dict[f"{field.name}"] = rand_dict_value.get(
                    field.dtype.to_value_type(), [None]
                )

        return feature_dict

    @staticmethod
    def get_requested_odfvs(
        feature_refs, project, registry
    ) -> List["OnDemandFeatureView"]:
        all_on_demand_feature_views = registry.list_on_demand_feature_views(
            project, allow_cache=True
        )
        requested_on_demand_feature_views: List[OnDemandFeatureView] = []
        for odfv in all_on_demand_feature_views:
            for feature in odfv.features:
                if f"{odfv.name}:{feature.name}" in feature_refs:
                    requested_on_demand_feature_views.append(odfv)
                    break
        return requested_on_demand_feature_views


def on_demand_feature_view(
    *,
    schema: List[Field],
    sources: List[
        Union[
            FeatureView,
            RequestSource,
            FeatureViewProjection,
        ]
    ],
    mode: str = "pandas",
    description: str = "",
    tags: Optional[Dict[str, str]] = None,
    owner: str = "",
):
    """
    Creates an OnDemandFeatureView object with the given user function as udf.

    Args:
        schema: The list of features in the output of the on demand feature view, after
            the transformation has been applied.
        sources: A map from input source names to the actual input sources, which may be
            feature views, or request data sources. These sources serve as inputs to the udf,
            which will refer to them by name.
        mode: The mode of execution (e.g,. Pandas or Python Native)
        description (optional): A human-readable description.
        tags (optional): A dictionary of key-value pairs to store arbitrary metadata.
        owner (optional): The owner of the on demand feature view, typically the email
            of the primary maintainer.
    """

    def mainify(obj) -> None:
        # Needed to allow dill to properly serialize the udf. Otherwise, clients will need to have a file with the same
        # name as the original file defining the ODFV.
        if obj.__module__ != "__main__":
            obj.__module__ = "__main__"

    def decorator(user_function):
        return_annotation = inspect.signature(user_function).return_annotation
        udf_string = dill.source.getsource(user_function)
        mainify(user_function)
        if mode == "pandas":
            if return_annotation not in (inspect._empty, pd.DataFrame):
                raise TypeError(
                    f"return signature for {user_function} is {return_annotation} but should be pd.DataFrame"
                )
            transformation = PandasTransformation(user_function, udf_string)
        elif mode == "python":
            if return_annotation not in (inspect._empty, Dict[str, Any]):
                raise TypeError(
                    f"return signature for {user_function} is {return_annotation} but should be Dict[str, Any]"
                )
            transformation = PythonTransformation(user_function, udf_string)
        elif mode == "substrait":
            from ibis.expr.types.relations import Table

            if return_annotation not in (inspect._empty, Table):
                raise TypeError(
                    f"return signature for {user_function} is {return_annotation} but should be ibis.expr.types.relations.Table"
                )
            transformation = SubstraitTransformation.from_ibis(user_function, sources)

        on_demand_feature_view_obj = OnDemandFeatureView(
            name=user_function.__name__,
            sources=sources,
            schema=schema,
            feature_transformation=transformation,
            mode=mode,
            description=description,
            tags=tags,
            owner=owner,
        )
        functools.update_wrapper(
            wrapper=on_demand_feature_view_obj, wrapped=user_function
        )
        return on_demand_feature_view_obj

    return decorator


def feature_view_to_batch_feature_view(fv: FeatureView) -> BatchFeatureView:
    bfv = BatchFeatureView(
        name=fv.name,
        entities=fv.entities,
        ttl=fv.ttl,
        tags=fv.tags,
        online=fv.online,
        owner=fv.owner,
        schema=fv.schema,
        source=fv.batch_source,
    )

    bfv.features = copy.copy(fv.features)
    bfv.entities = copy.copy(fv.entities)
    return bfv


def _empty_odfv_udf_fn(x: Any) -> Any:
    # just an identity mapping, otherwise we risk tripping some downstream tests
    return x
