package alien4cloud.paas.model;

import java.util.Date;
import java.util.Objects;

import org.elasticsearch.annotation.*;
import org.elasticsearch.annotation.query.TermFilter;
import org.elasticsearch.annotation.query.TermsFacet;
import org.elasticsearch.mapping.IndexType;

import com.fasterxml.jackson.annotation.JsonInclude;

import alien4cloud.dao.EsGeneratedId;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
 * This represents a log entry from orchestrator
 */
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ESObject
@JsonInclude(JsonInclude.Include.NON_NULL)
public class PaaSDeploymentLog {
    /**
     * Unique id of the log. Note that this field value is generated by elasticsearch. Should always be null before you write it to elastic search.
     */
    @EsGeneratedId
    private String id;

    /**
     * Deployment id is unique for a deployment
     * It corresponds to {@link alien4cloud.model.deployment.Deployment#getId}
     * This field is mandatory
     */
    @TermFilter
    @StringField(indexType = IndexType.not_analyzed, includeInAll = false)
    private String deploymentId;

    /**
     * Deployment PaaS id is unique for an application on a given environment (for ex : Alien-Prod)
     * It corresponds to {@link alien4cloud.model.deployment.Deployment#getOrchestratorDeploymentId}
     * This field is mandatory
     */
    @TermFilter
    @StringField(indexType = IndexType.not_analyzed, includeInAll = false)
    private String deploymentPaaSId;

    /**
     * Log's level
     * This field is mandatory
     */
    @TermFilter
    @TermsFacet
    @StringField(indexType = IndexType.not_analyzed, includeInAll = false)
    private PaaSDeploymentLogLevel level;

    /**
     * The type of the log entry given by the PaaS
     * For cloudify it can be a4c_workflow_event, Task sent, Task started etc ...
     * For puccini it can be operation_output, operation_info, plugin, provider, general ...
     * This field is mandatory
     */
    @TermFilter
    @TermsFacet
    @StringField(indexType = IndexType.not_analyzed, includeInAll = false)
    private String type;

    /**
     * Log's timestamp
     * This field is mandatory
     */
    @TermFilter
    @DateField
    @TimeStamp(format = "", index = IndexType.not_analyzed)
    private Date timestamp;

    /**
     * The timestamp raw value as it was provided by the orchestrator.
     */
    @TermFilter
    @NumberField(index = IndexType.not_analyzed, includeInAll = false)
    private String rawtimestamp;

    /**
     * Id of the workflow that generated the log
     * This field is optional
     */
    @TermFilter
    @TermsFacet
    @StringField(indexType = IndexType.not_analyzed, includeInAll = false)
    private String workflowId;

    /**
     * Id of the execution that generated the log
     * This field is optional
     */
    @TermFilter
    @TermsFacet
    @StringField(indexType = IndexType.not_analyzed, includeInAll = false)
    private String executionId;

    /**
     * Id of the task that generated the log
     * This field is optional
     */
    @TermFilter
    @TermsFacet
    @StringField(indexType = IndexType.not_analyzed, includeInAll = false)
    private String taskId;

    /**
     * Id of the node that generated the log
     * This field is optional
     */
    @TermFilter
    @TermsFacet
    @StringField(indexType = IndexType.not_analyzed, includeInAll = false)
    private String nodeId;

    /**
     * Id of the instance that generated the log
     * This field is optional
     */
    @TermFilter
    @TermsFacet
    @StringField(indexType = IndexType.not_analyzed, includeInAll = false)
    private String instanceId;

    /**
     * Interface on the node that generated the log
     * This field is optional
     */
    @TermFilter
    @TermsFacet
    @StringField(indexType = IndexType.not_analyzed, includeInAll = false)
    private String interfaceName;

    /**
     * Operation inside the interface on the node that generated the log
     * This field is optional
     */
    @TermFilter
    @TermsFacet
    @StringField(indexType = IndexType.not_analyzed, includeInAll = false)
    private String operationName;

    /**
     * Finally the log's content in free text
     * This field is mandatory
     */
    @StringField
    private String content;

    @Override
    public String toString() {
        return "PaaSDeploymentLog{" + toCompactString() + '}';
    }

    public String toCompactString() {
        return "deploymentPaaSId='" + deploymentPaaSId + '\'' + ", level=" + level + ", type='" + type + '\'' + ", timestamp="
                + timestamp + ", workflowId='" + workflowId + '\'' + ", executionId='" + executionId + '\'' + ", taskId='" + taskId + '\'' + ", nodeId='" + nodeId + '\'' + ", instanceId='"
                + instanceId + '\'' + ", interfaceName='" + interfaceName + '\'' + ", operationName='" + operationName + '\'' + ", content='" + content;
    }

    public final static String FORMAT = "[%s][%s][%s][%s][%s][%s][%s][%s][%s][%s][%s]%s\n";

    private String formatEmptyString(String str) {
        return Objects.toString(str, "");
    }
    
    public String toFormattedString() {
        return String.format(FORMAT, formatEmptyString(timestamp.toString()), formatEmptyString(level.name().toUpperCase()), formatEmptyString(deploymentPaaSId), formatEmptyString(workflowId), formatEmptyString(executionId), formatEmptyString(taskId), formatEmptyString(nodeId), formatEmptyString(instanceId), formatEmptyString(interfaceName), formatEmptyString(operationName), formatEmptyString(type), formatEmptyString(content));
    }
    
}

@@ -2477,14 +2477,14 @@
    @
-Number
+String
 Fiel
@@ -2486,24 +2486,28 @@
 gField(index
+Type
  = IndexType

Fix issue in ES mapping