package com.betriebsratkanzlei.legalconsenthub.application_form

import com.betriebsratkanzlei.legalconsenthub.form_element.FormElementSection
import com.betriebsratkanzlei.legalconsenthub.user.User
import com.betriebsratkanzlei.legalconsenthub_api.model.ApplicationFormStatus
import jakarta.persistence.JoinColumn
import jakarta.persistence.ManyToOne
import jakarta.persistence.CascadeType
import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.EntityListeners
import jakarta.persistence.Enumerated
import jakarta.persistence.EnumType
import jakarta.persistence.GeneratedValue
import jakarta.persistence.Id
import jakarta.persistence.OneToMany

import org.springframework.data.annotation.CreatedDate
import org.springframework.data.annotation.LastModifiedDate
import org.springframework.data.jpa.domain.support.AuditingEntityListener
import java.time.LocalDateTime
import java.util.UUID

@Entity
@EntityListeners(AuditingEntityListener::class)
class ApplicationForm(
    @Id
    @GeneratedValue
    var id: UUID? = null,

    @Column(nullable = false)
    var name: String = "",

    @OneToMany(mappedBy = "applicationForm", cascade = [CascadeType.ALL], orphanRemoval = true)
    var formElementSections: MutableList<FormElementSection> = mutableListOf(),

    @Column(nullable = false)
    var isTemplate: Boolean,

    var organizationId: String = "",

    @Enumerated(EnumType.STRING)
    @Column(nullable = false)
    var status: ApplicationFormStatus = ApplicationFormStatus.DRAFT,

    @ManyToOne
    @JoinColumn(name = "created_by_id", nullable = false)
    var createdBy: User,

    @ManyToOne
    @JoinColumn(name = "last_modified_by_id", nullable = false)
    var lastModifiedBy: User,

    @CreatedDate
    @Column(nullable = false)
    var createdAt: LocalDateTime? = null,

    @LastModifiedDate
    @Column(nullable = false)
    var modifiedAt: LocalDateTime? = null
)
