package com.realnet.DynamicDashbard.entities;

import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonManagedReference;

import lombok.Data;

@Data
@Entity
@Table(name = "ui_components")
public class UiComponent {
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "chart_type_id", nullable = false)
	@JsonBackReference
	private ChartType chartType;

	@Column(name = "component_name", nullable = false, length = 100)
	private String componentName;

	@Column(name = "component_type", length = 50)
	private String componentType;

	@Column(name = "display_label", length = 100)
	private String displayLabel;

	@Column(name = "placeholder", columnDefinition = "TEXT")
	private String placeholder;

	@Column(name = "is_required")
	private Boolean isRequired = false;

	@Column(name = "sort_order")
	private Integer sortOrder;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "created_at")
	@CreatedDate
	private Date createdAt;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "updated_at")
	@LastModifiedDate
	private Date updatedAt;

	@OneToMany(mappedBy = "component", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
	@JsonManagedReference
	private List<ComponentProperty> componentProperties;

//	@PrePersist
//	protected void onCreate() {
//		createdAt = LocalDateTime.now();
//		updatedAt = LocalDateTime.now();
//	}
//
//	@PreUpdate
//	protected void onUpdate() {
//		updatedAt = LocalDateTime.now();
//	}

	// Constructors
	public UiComponent() {
	}

	public UiComponent(ChartType chartType, String componentName, String componentType) {
		this.chartType = chartType;
		this.componentName = componentName;
		this.componentType = componentType;
	}

}