package enea.lecservice.entities;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.type.TypeReference;
import enea.lecservice.models.Configuration;
import enea.lecservice.utils.JacksonUtils;
import lombok.*;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.ColumnDefault;

import javax.persistence.*;

@Entity
@Builder
@Getter @Setter
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "lec_page")
public class Page extends BaseEntity {
    protected String name;
    protected String slug;
    protected String lang;
    protected String path;
    @JsonIgnore
    @Column(name = "configuration", columnDefinition="TEXT")
    protected String configurationText;
    protected int showOrder;
    
   // @JsonInclude(JsonInclude.Include.NON_NULL)
    protected Integer communityId;
    protected boolean visible;
    protected boolean defaultPage;
    protected boolean dynamic;
    //@ColumnDefault("f")
    protected boolean showInMenu = false;
//    @Enumerated(EnumType.STRING)
//    protected PermissionEnum role;
    @Column(name = "permission_id")
    protected String permissionId;
    @JsonIgnore
    @ManyToOne
    @JoinColumn(name = "permission_id", referencedColumnName = "id", insertable = false, updatable = false)
    protected Permission permission;

    @Transient
    protected Configuration configuration;

    public Configuration getConfiguration() {
        if (configuration == null && StringUtils.isNotBlank(configurationText))
            configuration = JacksonUtils.fromJson(configurationText, new TypeReference<Configuration>() {});
        return configuration;
    }

    public void setConfigurationTextFromConfiguration(Configuration configuration) {
        this.configuration = configuration;
        this.configurationText = JacksonUtils.toJson(configuration);
    }
}
