package com.ffii.fpsms.modules.purchaseOrder.entity

import com.fasterxml.jackson.annotation.JsonManagedReference
import com.ffii.core.entity.BaseEntity
import com.ffii.fpsms.modules.master.entity.Items
import com.ffii.fpsms.m18.entity.M18DataLog
import com.ffii.fpsms.modules.master.entity.BomMaterial
import com.ffii.fpsms.modules.master.entity.Currency
import com.ffii.fpsms.modules.master.entity.UomConversion
import com.ffii.fpsms.modules.purchaseOrder.enums.PurchaseOrderLineStatus
import com.ffii.fpsms.modules.purchaseOrder.enums.PurchaseOrderLineStatusConverter
import com.ffii.fpsms.modules.stock.entity.StockInLine
import jakarta.persistence.*
import jakarta.validation.constraints.NotNull
import jakarta.validation.constraints.Size
import java.math.BigDecimal

@Entity
@Table(name = "purchase_order_line")
open class PurchaseOrderLine : BaseEntity<Long>() {
    @NotNull
    @ManyToOne
    @JoinColumn(name = "itemId", nullable = false)
    open var item: Items? = null

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

    @NotNull
    @ManyToOne
    @JoinColumn(name = "purchaseOrderId", nullable = false)
    open var purchaseOrder: PurchaseOrder? = null

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

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

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

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

    @Convert(converter = PurchaseOrderLineStatusConverter::class)
    @NotNull
    @Column(name = "status", nullable = false, length = 10)
    open var status: PurchaseOrderLineStatus? = null

    @ManyToOne
    @JoinColumn(name = "m18DataLogId", nullable = false)
    open var m18DataLog: M18DataLog? = null

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

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

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

    @ManyToOne
    @JoinColumn(name = "uomIdM18")
    open var uomM18: UomConversion? = null

    @JsonManagedReference
    @OneToMany(mappedBy = "purchaseOrderLine", cascade = [CascadeType.ALL], orphanRemoval = true)
    open var stockInLines: MutableList<StockInLine> = mutableListOf()
}