/*
 * Copyright 2019 Google LLC
 *
 * 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
 *
 *     https://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.google.android.gnd.persistence.local.room;

import static androidx.room.ForeignKey.CASCADE;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.room.Embedded;
import androidx.room.Entity;
import androidx.room.ForeignKey;
import androidx.room.Index;
import androidx.room.PrimaryKey;
import com.google.android.gnd.persistence.shared.FeatureMutation;

/**
 * Defines how Room persists feature mutations for remote sync in the local db. By default, Room
 * uses the name of object fields and their respective types to determine database column names and
 * types.
 */
@Entity(
    tableName = "featureMutation",
    foreignKeys =
        @ForeignKey(
            entity = FeatureEntity.class,
            parentColumns = "id",
            childColumns = "featureId",
            onDelete = CASCADE),
    indices = {@Index("featureId")})
public class FeatureMutationEntity {

  @PrimaryKey(autoGenerate = true)
  public int id;

  @NonNull public String featureId;

  @NonNull public MutationEntityType type;

  /** Non-null if the feature's location was updated, null if unchanged. */
  @Nullable @Embedded public Coordinates newLocation;

  static FeatureMutationEntity fromMutation(FeatureMutation m) {
    FeatureMutationEntity me = new FeatureMutationEntity();
    me.featureId = m.getFeatureId();
    me.newLocation = m.getNewLocation().map(Coordinates::fromPoint).orElse(null);
    me.type = MutationEntityType.fromMutationType(m.getType());
    return me;
  }
}

@@ -986,16 +986,112 @@
 utation;
+%0Aimport com.google.auto.value.AutoValue;%0Aimport com.google.auto.value.AutoValue.CopyAnnotations;
 %0A%0A/**%0A *
@@ -1299,16 +1299,27 @@
 es.%0A */%0A
+@AutoValue%0A
 @Entity(
@@ -1586,16 +1586,25 @@
 %0Apublic 
+abstract 
 class Fe
@@ -1626,16 +1626,35 @@
 tity %7B%0A%0A
+  @CopyAnnotations%0A
   @Prima
@@ -1693,16 +1693,49 @@
 lic 
-int id;%0A
+abstract int getId();%0A%0A  @CopyAnnotations
 %0A  @
@@ -1741,24 +1741,26 @@
 @NonNull
+%0A 
  public 
 String f
@@ -1755,24 +1755,36 @@
 lic 
+abstract 
 String 
-f
+getF
 eatureId
 ;%0A%0A 
@@ -1779,19 +1779,40 @@
 eatureId
+()
 ;%0A%0A
+  @CopyAnnotations%0A
   @NonNu
@@ -1813,24 +1813,26 @@
 @NonNull
+%0A 
  public 
 Mutation
@@ -1823,16 +1823,25 @@
  public 
+abstract 
 Mutation
@@ -1851,20 +1851,25 @@
 ityType 
-t
+getT
 ype
+()
 ;%0A%0A  /**
@@ -1939,16 +1939,35 @@
 ged. */%0A
+  @CopyAnnotations%0A
   @Nulla
@@ -1969,16 +1969,18 @@
 Nullable
+%0A 
  @Embedd
@@ -1981,24 +1981,26 @@
 Embedded
+%0A 
  public 
 Coordina
@@ -1991,16 +1991,25 @@
  public 
+abstract 
 Coordina
@@ -2012,17 +2012,20 @@
 dinates 
-n
+getN
 ewLocati
@@ -2026,16 +2026,18 @@
 Location
+()
 ;%0A%0A  sta
@@ -2104,38 +2104,14 @@
     
-FeatureMutationEntity me = new
+return
  Fea
@@ -2132,28 +2132,40 @@
 tity
+.builder
 ()
-;
 %0A    
-me.f
+    .setF
 eatureId
  = m
@@ -2160,19 +2160,17 @@
 eatureId
- = 
+(
 m.getFea
@@ -2181,18 +2181,23 @@
 Id()
-;
+)
 %0A    
-me.n
+    .setN
 ewLo
@@ -2202,19 +2202,17 @@
 Location
- = 
+(
 m.getNew
@@ -2266,24 +2266,27 @@
 ull)
-;
+)
 %0A    
-me.type = 
+    .setType(
 Muta
@@ -2333,23 +2333,814 @@
 e())
-;%0A    return me
+)%0A        .build();%0A  %7D%0A%0A  // Auto-generated boilerplate.%0A%0A  public static FeatureMutationEntity create(%0A      int id, String featureId, MutationEntityType type, Coordinates newLocation) %7B%0A    return builder()%0A        .setId(id)%0A        .setFeatureId(featureId)%0A        .setType(type)%0A        .setNewLocation(newLocation)%0A        .build();%0A  %7D%0A%0A  public static Builder builder() %7B%0A    return new AutoValue_FeatureMutationEntity.Builder();%0A  %7D%0A%0A  @AutoValue.Builder%0A  public abstract static class Builder %7B%0A%0A    public abstract Builder setId(int newId);%0A%0A    public abstract Builder setFeatureId(String newFeatureId);%0A%0A    public abstract Builder setType(MutationEntityType newType);%0A%0A    public abstract Builder setNewLocation(Coordinates newNewLocation);%0A%0A    public abstract FeatureMutationEntity build()
 ;%0A  

Implement FeatureMutationEntity as AutoValue