package com.pingcap.community.blogserver.entity;

import com.fasterxml.jackson.annotation.JsonView;
import lombok.*;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import org.springframework.data.rest.core.annotation.RestResource;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

@Entity(name = "like")
@Table(name = "likes")
@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class Like {

  public interface LikeItemView extends Post.PostItemView, User.UserLoginView {}

  @Embeddable
  @Data
  @NoArgsConstructor
  @AllArgsConstructor
  public static class LikeKey implements Serializable {
    @Column(name = "post_id", nullable = false, updatable = false)
    private Long postID;
    @Column(name = "user_id", nullable = false, updatable = false)
    private Long userID;
  }

  public Like(LikeKey key) {
    this.key = key;
  }

  @EmbeddedId
  private LikeKey key;

  @ManyToOne
  @JoinColumn(name = "post_id", insertable = false, updatable = false)
  @RestResource(exported = false)
  @NotFound(action = NotFoundAction.IGNORE)
  @JsonView(LikeItemView.class)
  private Post post;

  @ManyToOne
  @JoinColumn(name = "user_id", insertable = false, updatable = false)
  @NotFound(action = NotFoundAction.IGNORE)
  @RestResource(exported = false)
  @JsonView(LikeItemView.class)
  private User user;

  @Column(name = "liked_at", updatable = false, columnDefinition = "datetime not null default CURRENT_TIMESTAMP")
  @JsonView(LikeItemView.class)
  private Date likedAt;
}
