package com.ffii.fpsms.modules.master.entity

import com.fasterxml.jackson.annotation.JsonBackReference
import com.fasterxml.jackson.annotation.JsonManagedReference
import com.ffii.core.entity.BaseEntity
import com.ffii.fpsms.modules.master.enums.BomStatus
import com.ffii.fpsms.modules.master.enums.BomStatusConverter
import jakarta.persistence.*
import jakarta.validation.constraints.NotNull
import jakarta.validation.constraints.Size
import java.math.BigDecimal
import java.time.Instant
import java.time.LocalDateTime

@Table(name = "bom")
@Entity
open class Bom : BaseEntity<Long>() {
    @Column
    open var isDark: Int? = null

    @Column
    open var isFloat: Int? = null

    @Column
    open var isDense: Int? = null
    @Column
    open var isDrink: Boolean? = false
    @Column
    open var scrapRate: Int? = null
    @Column
    open var allergicSubstances: Int? = null

    @Column
    open var type: String? = null
    @Column
    open var timeSequence: Int? = null
    @Column
    open var complexity: Int? = null
    @JsonBackReference
    @ManyToOne
    @JoinColumn(name = "itemId")
    open var item: Items? = null

    @Size(max = 30)
    @NotNull
    @Column(name = "code", nullable = false, length = 30)
    open var code: String? = null

    @Size(max = 100)
    @NotNull
    @Column(name = "name", nullable = false, length = 100)
    open var name: String? = null

    @Size(max = 100)
    @NotNull
    @Column(name = "description", nullable = false, length = 100)
    open var description: String? = null

    @Column(name = "outputQty", precision = 14, scale = 2)
    open var outputQty: BigDecimal? = null

    @Size(max = 50)
    @Column(name = "outputQtyUom", length = 50)
    open var outputQtyUom: String? = null

    @Column(name = "yield", precision = 14, scale = 2)
    open var yield: BigDecimal? = null

    @ManyToOne
    @JoinColumn(name = "uomId")
    open var uom: UomConversion? = null

    @Column(name = "excelUom", length = 255)
    open var excelUom: String? = null

    @JsonManagedReference
    @OneToMany(mappedBy = "bom", cascade = [CascadeType.ALL], orphanRemoval = true)
    open var bomMaterials: MutableList<BomMaterial> = mutableListOf()

    @JsonManagedReference
    @OneToMany(mappedBy = "bom", cascade = [CascadeType.ALL], orphanRemoval = true)
    open var bomProcesses: MutableList<BomProcess> = mutableListOf()

    @Column(name = "m18Id")
    open var m18Id: Long? = null

    @Column(name = "m18LastModifyDate")
    open var m18LastModifyDate: LocalDateTime? = null

    @Column(name = "baseScore", precision = 14, scale = 2)
    open var baseScore: BigDecimal? = null

    @Column(name = "status", nullable = false, length = 20)
    @Convert(converter = BomStatusConverter::class)
    open var status: BomStatus = BomStatus.ACTIVE
}