# SPDX-License-Identifier: Apache-2.0
# The OpenSearch Contributors require contributions made to
# this file be licensed under the Apache-2.0 license or a
# compatible open source license.
# Any modifications Copyright OpenSearch Contributors. See
# GitHub history for details.

#  Licensed to Elasticsearch B.V. under one or more contributor
#  license agreements. See the NOTICE file distributed with
#  this work for additional information regarding copyright
#  ownership. Elasticsearch B.V. licenses this file to you under
#  the Apache License, Version 2.0 (the "License"); you may
#  not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
# 	http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing,
#  software distributed under the License is distributed on an
#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
#  KIND, either express or implied.  See the License for the
#  specific language governing permissions and limitations
#  under the License.

# File called _pytest for PyCharm compatability
import pytest

from opensearch_py_ml.field_mappings import FieldMappings
from tests import ECOMMERCE_INDEX_NAME, OPENSEARCH_TEST_CLIENT
from tests.common import TestData


class TestAggregatables(TestData):
    @pytest.mark.filterwarnings("ignore:Aggregations not supported")
    def test_ecommerce_all_aggregatables(self):
        oml_field_mappings = FieldMappings(
            client=OPENSEARCH_TEST_CLIENT, index_pattern=ECOMMERCE_INDEX_NAME
        )

        aggregatables = oml_field_mappings.aggregatable_field_names()

        expected = {
            "category.keyword": "category",
            "currency": "currency",
            "customer_birth_date": "customer_birth_date",
            "customer_first_name.keyword": "customer_first_name",
            "customer_full_name.keyword": "customer_full_name",
            "customer_id": "customer_id",
            "customer_last_name.keyword": "customer_last_name",
            "customer_phone": "customer_phone",
            "day_of_week": "day_of_week",
            "day_of_week_i": "day_of_week_i",
            "email": "email",
            "geoip.city_name": "geoip.city_name",
            "geoip.continent_name": "geoip.continent_name",
            "geoip.country_iso_code": "geoip.country_iso_code",
            "geoip.location": "geoip.location",
            "geoip.region_name": "geoip.region_name",
            "manufacturer.keyword": "manufacturer",
            "order_date": "order_date",
            "order_id": "order_id",
            "products._id.keyword": "products._id",
            "products.base_price": "products.base_price",
            "products.base_unit_price": "products.base_unit_price",
            "products.category.keyword": "products.category",
            "products.created_on": "products.created_on",
            "products.discount_amount": "products.discount_amount",
            "products.discount_percentage": "products.discount_percentage",
            "products.manufacturer.keyword": "products.manufacturer",
            "products.min_price": "products.min_price",
            "products.price": "products.price",
            "products.product_id": "products.product_id",
            "products.product_name.keyword": "products.product_name",
            "products.quantity": "products.quantity",
            "products.sku": "products.sku",
            "products.tax_amount": "products.tax_amount",
            "products.taxful_price": "products.taxful_price",
            "products.taxless_price": "products.taxless_price",
            "products.unit_discount_amount": "products.unit_discount_amount",
            "sku": "sku",
            "taxful_total_price": "taxful_total_price",
            "taxless_total_price": "taxless_total_price",
            "total_quantity": "total_quantity",
            "total_unique_products": "total_unique_products",
            "type": "type",
            "user": "user",
        }

        assert expected == aggregatables

    def test_ecommerce_selected_aggregatables(self):
        expected = {
            "category.keyword": "category",
            "currency": "currency",
            "customer_birth_date": "customer_birth_date",
            "customer_first_name.keyword": "customer_first_name",
            "type": "type",
            "user": "user",
        }

        oml_field_mappings = FieldMappings(
            client=OPENSEARCH_TEST_CLIENT,
            index_pattern=ECOMMERCE_INDEX_NAME,
            display_names=expected.values(),
        )

        aggregatables = oml_field_mappings.aggregatable_field_names()

        assert expected == aggregatables

    def test_ecommerce_single_aggregatable_field(self):
        oml_field_mappings = FieldMappings(
            client=OPENSEARCH_TEST_CLIENT, index_pattern=ECOMMERCE_INDEX_NAME
        )

        assert "user" == oml_field_mappings.aggregatable_field_name("user")

    def test_ecommerce_single_keyword_aggregatable_field(self):
        oml_field_mappings = FieldMappings(
            client=OPENSEARCH_TEST_CLIENT, index_pattern=ECOMMERCE_INDEX_NAME
        )

        assert (
            "customer_first_name.keyword"
            == oml_field_mappings.aggregatable_field_name("customer_first_name")
        )

    def test_ecommerce_single_non_existant_field(self):
        oml_field_mappings = FieldMappings(
            client=OPENSEARCH_TEST_CLIENT, index_pattern=ECOMMERCE_INDEX_NAME
        )

        with pytest.raises(KeyError):
            oml_field_mappings.aggregatable_field_name("non_existant")

    @pytest.mark.skip(
        reason="opensearch treats all fields in ecommerce df as aggregatable"
    )
    @pytest.mark.filterwarnings("ignore:Aggregations not supported")
    def test_ecommerce_single_non_aggregatable_field(self):
        oml_field_mappings = FieldMappings(
            client=OPENSEARCH_TEST_CLIENT, index_pattern=ECOMMERCE_INDEX_NAME
        )

        assert None is oml_field_mappings.aggregatable_field_name("customer_gender")