package com.eshore.gringotts.web.domain.confirmation.entity;

import com.eshore.gringotts.core.Constants;
import com.eshore.gringotts.web.domain.base.entity.SimpleEntity;
import com.eshore.gringotts.web.domain.resource.entity.DataResource;
import com.eshore.gringotts.web.domain.upload.entity.DataFile;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.ConstraintMode;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.ForeignKey;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.NamedAttributeNode;
import javax.persistence.NamedEntityGraph;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.hibernate.annotations.DynamicUpdate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

/**
 * 确权
 *
 * @author lanyuanxiaoyao
 * @date 2024-11-26
 */
@Getter
@Setter
@ToString
@Entity
@DynamicUpdate
@EntityListeners(AuditingEntityListener.class)
@Table(name = Constants.TABLE_PREFIX + "confirmation")
@NamedEntityGraph(name = "confirmation.list", attributeNodes = {
        @NamedAttributeNode(value = "target"),
        @NamedAttributeNode(value = "createdUser"),
})
@NamedEntityGraph(name = "confirmation.detail", attributeNodes = {
        @NamedAttributeNode(value = "target"),
        @NamedAttributeNode(value = "evidences"),
        @NamedAttributeNode(value = "createdUser"),
        @NamedAttributeNode(value = "modifiedUser"),
})
public class Confirmation extends SimpleEntity {
    @OneToOne(fetch = FetchType.EAGER)
    @JoinColumn(nullable = false, foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
    private DataResource target;
    private String description;
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinTable(foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT), inverseForeignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
    @ToString.Exclude
    private Set<DataFile> evidences;
    @Column(nullable = false)
    @Enumerated(EnumType.STRING)
    private State state = State.DRAFT;

    public enum State {
        /**
         * 草稿
         */
        DRAFT,
        /**
         * 审查中
         */
        CHECKING,
        /**
         * 正常
         */
        NORMAL,
        /**
         * 驳回
         */
        REJECT,
    }
}
