package dp.kotul.templategroupsbackend.model.entity

import com.fasterxml.jackson.annotation.JsonIgnore
import dp.kotul.solrapimodel.solrsearch.TemplateType
import javax.persistence.CascadeType
import javax.persistence.Column
import javax.persistence.Entity
import javax.persistence.EnumType
import javax.persistence.Enumerated
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

@Entity
class Template(
        @Id
        @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "templateSeq")
        val templateId: Int = 0,

        @JsonIgnore
        @ManyToOne(fetch = FetchType.LAZY)
        @JoinColumn(name = "templateGroupId")
        val templateGroup: TemplateGroup,

        @Column(nullable = false)
        val templateExtId: String,

        @Column(nullable = false)
        @Enumerated(EnumType.STRING)
        val templateType: TemplateType,

        @OneToMany(mappedBy = "template", cascade = [CascadeType.ALL], fetch = FetchType.LAZY)
        val templateVariables: MutableList<TemplateVariable> = mutableListOf(),

        val sortOrder: Int?
) {
    fun getType() = templateType.value
}
