/*
 *
 * Copyright 2019 Netflix, Inc.
 *
 * Licensed 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.
 *
 */
package com.netflix.spinnaker.kork.manageddelivery.model;

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeName;
import java.util.List;
import java.util.Map;
import lombok.Data;

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonSubTypes({
  @JsonSubTypes.Type(value = ResourceEvent.ResourceCreated.class, name = "ResourceCreated"),
  @JsonSubTypes.Type(value = ResourceEvent.ResourceUpdated.class, name = "ResourceUpdated"),
  @JsonSubTypes.Type(value = ResourceEvent.ResourceMissing.class, name = "ResourceMissing"),
  @JsonSubTypes.Type(
      value = ResourceEvent.ResourceActuationLaunched.class,
      name = "ResourceActuationLaunched"),
  @JsonSubTypes.Type(
      value = ResourceEvent.ResourceDeltaDetected.class,
      name = "ResourceDeltaDetected"),
  @JsonSubTypes.Type(
      value = ResourceEvent.ResourceDeltaResolved.class,
      name = "ResourceDeltaResolved")
})
@Data
public class ResourceEvent {
  String uid;
  String apiVersion;
  String kind;
  String name;
  String timestamp;

  /** A new resource was registered for management. */
  @Data
  @JsonTypeName("ResourceCreated")
  public static class ResourceCreated extends ResourceEvent {}

  /** A managed resource does not currently exist in the cloud. */
  @Data
  @JsonTypeName("ResourceMissing")
  public static class ResourceMissing extends ResourceEvent {}

  /**
   * The desired and actual states of a managed resource now match where previously there was a
   * delta (or the resource did not exist).
   */
  @Data
  @JsonTypeName("ResourceDeltaResolved")
  public static class ResourceDeltaResolved extends ResourceEvent {}

  /**
   * A difference between the desired and actual state of a managed resource was detected.
   *
   * <p>[delta] The difference between the "base" spec (desired) and "working" spec (actual).
   */
  @Data
  @JsonTypeName("ResourceDeltaDetected")
  public static class ResourceDeltaDetected extends ResourceEvent {
    Map<String, Object> delta;
  }

  /**
   * A task or tasks were launched to resolve a mismatch between desired and actual state of a
   * managed resource.
   */
  @Data
  @JsonTypeName("ResourceActuationLaunched")
  public static class ResourceActuationLaunched extends ResourceEvent {
    String plugin;
    List<String> tasks;
  }

  /**
   * The desired state of a resource was updated.
   *
   * <p>[delta] The difference between the "base" spec (previous version) and "working" spec (the
   * updated version).
   */
  @Data
  @JsonTypeName("ResourceUpdated")
  public static class ResourceUpdated extends ResourceEvent {
    Map<String, Object> delta;
  }
}

@@ -1232,24 +1232,117 @@
 eMissing%22),%0A
+  @JsonSubTypes.Type(value = ResourceEvent.ResourceDeleted.class, name = %22ResourceDeleted%22),%0A
   @JsonSubTy
@@ -2139,32 +2139,537 @@
  ResourceEvent %7B
+%0A    ResourceState state;%0A  %7D%0A%0A  /** A managed resource was deleted. */%0A  @Data%0A  @JsonTypeName(%22ResourceDeleted%22)%0A  public static class ResourceDeleted extends ResourceEvent %7B%7D%0A%0A  /**%0A   * The desired state of a resource was updated.%0A   *%0A   * %3Cp%3E%5Bdelta%5D The difference between the %22base%22 spec (previous version) and %22working%22 spec (the%0A   * updated version).%0A   */%0A  @Data%0A  @JsonTypeName(%22ResourceUpdated%22)%0A  public static class ResourceUpdated extends ResourceEvent %7B%0A    Map%3CString, Object%3E delta;%0A  
 %7D%0A%0A  /**%0A   * Th
@@ -2915,32 +2915,60 @@
  ResourceEvent %7B
+%0A    ResourceState state;%0A  
 %7D%0A%0A  /**%0A   * A 
@@ -3296,32 +3296,57 @@
  Object%3E delta;%0A
+    ResourceState state;%0A
   %7D%0A%0A  /**%0A   * 
@@ -3617,22 +3617,20 @@
    List%3C
-String
+Task
 %3E tasks;
@@ -3641,332 +3641,126 @@
 %0A%0A  
-/**%0A   * The desired state of a resource was updated.%0A   *%0A   * %3Cp%3E%5Bdelta%5D The difference between the %22base%22 spec (previous version) and %22working%22 spec (the%0A   * updated version).%0A   */%0A  @Data%0A  @JsonTypeName(%22ResourceUpdated%22)%0A  public static class ResourceUpdated extends ResourceEvent %7B%0A    Map%3CString, Object%3E delta;%0A  %7D
+public static class Task %7B%0A    String id;%0A    String name;%0A  %7D%0A%7D%0A%0Aenum ResourceState %7B%0A  Ok,%0A  Diff,%0A  Missing,%0A  Error
 %0A%7D%0A

update resource history events (#361)