package it.enea.cruise.model.user;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;

import lombok.Getter;
import lombok.Setter;

/**
 * 
 * @author Gianluca D'Agosta Questa classe gestisce la tabella "Communities" del
 *         database con i dati di consumo delle Comunita' energetiche. Essa
 *         permette, con una relazione 1 a molti, di collegare tutti + gli
 *         utenti che parteticpano alla comunita'.
 *
 */

@Entity(name = "community")
@Table(name = "communities")
@Getter
@Setter
@JsonIdentityInfo(
		  generator = ObjectIdGenerators.PropertyGenerator.class, 
		  property = "communityid",
		  scope = User.class)
@NamedQuery(name = "Community.findAllOnlyNameandid", query = "select c.communityid as id, c.communityName as name, c.description as desc from community c")
public class Community {
	/*
	 * Properties of the Community
	 */

	@Id
	@GeneratedValue
	@Column(name = "communityid")
	private Long communityid;

	@JsonInclude(JsonInclude.Include.NON_NULL)
	@Column(name = "communityname")
	private String communityName;

	@JsonInclude(JsonInclude.Include.NON_NULL)
	private String description;
	
	/*
	 * Questo crea il legame con la tabella users_communities. Essendo
	 * uni-direzionale con questa tabella, la relazione e' uno (community) a molti
	 * (users) perche', dal lato della Comunita', essa puo' essere associata a
	 * molteplici utenti.
	 */
	@OneToMany(mappedBy = "community", cascade = CascadeType.ALL, orphanRemoval = true, fetch=FetchType.EAGER)
	@Fetch(value = FetchMode.SUBSELECT)
	//@JsonManagedReference(value = "community-communities")
	@JsonIgnoreProperties({"user", "community"})
	private List<UserCommunity> userCommunities = new ArrayList<>();


	/*
	 * Generic Methods
	 */
	public Community() {
	}

	public Community(String name) {
		this.communityName = name;
	}

	public Community(Long id, String name, String description) {
		this.communityid = id;
		this.communityName = name;
		this.description = description;
	}

	@Override
	public boolean equals(Object o) {
		if (this == o)
			return true;
		if (o == null || getClass() != o.getClass())
			return false;
		Community u = (Community) o;
		return Objects.equals(communityName, u.communityName);
	}

	@Override
	public int hashCode() {
		return Objects.hash(communityName);
	}

	/*
	 * Getters and Setters
	 */

//	public Long getCommunityid() {
//		return communityid;
//	}
//
//	public void setCommunityid(Long communityid) {
//		this.communityid = communityid;
//	}
//
//	public String getCommunityName() {
//		return communityName;
//	}
//
//	public void setCommunityName(String communityName) {
//		this.communityName = communityName;
//	}
//
//	public List<UserCommunity> getUserCommunities() {
//		return userCommunities;
//	}
//
//	public void setUserCommunities(List<UserCommunity> userCommunities) {
//		this.userCommunities = userCommunities;
//	}
//
//	public String getDescription() {
//		return description;
//	}
//
//	public void setDescription(String description) {
//		this.description = description;
//	}

}
