// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchdefinition.processing;

import com.yahoo.config.application.api.DeployLogger;
import com.yahoo.document.TensorDataType;
import com.yahoo.searchdefinition.RankProfileRegistry;
import com.yahoo.searchdefinition.document.Attribute;
import com.yahoo.document.DataType;
import com.yahoo.searchdefinition.document.SDField;
import com.yahoo.searchdefinition.Search;
import com.yahoo.vespa.documentmodel.DocumentSummary;
import com.yahoo.vespa.documentmodel.SummaryField;
import com.yahoo.vespa.model.container.search.QueryProfiles;

import java.util.HashMap;
import java.util.Map;

/**
 * This Processor checks to make sure all fields with the same name have the same {@link DataType}. This check
 * explicitly disregards whether a field is an index field, an attribute or a summary field. This is a requirement if we
 * hope to move to a model where index fields, attributes and summary fields share a common field class.
 *
 * @author Simon Thoresen
 */
public class ValidateFieldTypes extends Processor {

    public ValidateFieldTypes(Search search, DeployLogger deployLogger, RankProfileRegistry rankProfileRegistry, QueryProfiles queryProfiles) {
        super(search, deployLogger, rankProfileRegistry, queryProfiles);
    }

    @Override
    public void process() {
        String searchName = search.getName();
        Map<String, DataType> seenFields = new HashMap<>();
        for (SDField field : search.allConcreteFields()) {
            checkFieldType(searchName, "index field", field.getName(), field.getDataType(), seenFields);
            for (Map.Entry<String, Attribute> entry : field.getAttributes().entrySet()) {
                checkFieldType(searchName, "attribute", entry.getKey(), entry.getValue().getDataType(), seenFields);
            }
        }
        for (DocumentSummary summary : search.getSummaries().values()) {
            for (SummaryField field : summary.getSummaryFields()) {
                checkFieldType(searchName, "summary field", field.getName(), field.getDataType(), seenFields);
            }
        }
    }

    private void checkFieldType(String searchName, String fieldDesc, String fieldName, DataType fieldType,
                                Map<String, DataType> seenFields) {
        DataType seenType = seenFields.get(fieldName);
        if (seenType == null) {
            seenFields.put(fieldName, fieldType);
        } else if ( ! compatibleTypes(seenType, fieldType)) {
            throw newProcessException(searchName, fieldName, "Incompatible types. Expected " + 
                                                             seenType.getName() + " for " + fieldDesc +
                                                             " '" + fieldName + "', got " + fieldType.getName() + ".");
        }
    }

    private boolean compatibleTypes(DataType seenType, DataType fieldType) {
        // legacy tag field type compatibility; probably not needed any more (Oct 2016)
        if ("tag".equals(seenType.getName())) {
            return "tag".equals(fieldType.getName()) || "WeightedSet<string>".equals(fieldType.getName());
        }
        if ("tag".equals(fieldType.getName())) {
            return "tag".equals(seenType.getName()) || "WeightedSet<string>".equals(seenType.getName());
        }
        if (seenType instanceof TensorDataType && fieldType instanceof TensorDataType) {
            return fieldType.isAssignableFrom(seenType); // TODO: Just do this for all types
        }
        return seenType.equals(fieldType);
    }
    
}

@@ -209,16 +209,52 @@
 Logger;%0A
+import com.yahoo.document.DataType;%0A
 import c
@@ -279,32 +279,32 @@
 TensorDataType;%0A
-
 import com.yahoo
@@ -338,24 +338,66 @@
 leRegistry;%0A
+import com.yahoo.searchdefinition.Search;%0A
 import com.y
@@ -442,138 +442,8 @@
 te;%0A
-import com.yahoo.document.DataType;%0Aimport com.yahoo.searchdefinition.document.SDField;%0Aimport com.yahoo.searchdefinition.Search;%0A
 impo
@@ -1465,56 +1465,43 @@
     
-for (SDField field : search.allConcreteFields())
+search.allFields().forEach(field -%3E
  %7B%0A 
@@ -1826,32 +1826,34 @@
      %7D%0A        %7D
+);
 %0A        for (Do
@@ -2823,32 +2823,32 @@
        %7D%0A    %7D%0A%0A
-
     private bool
@@ -2843,16 +2843,23 @@
 private 
+static 
 boolean 

Include imported fields when validating field types