package com.ffii.fpsms.modules.stock.entity

import com.fasterxml.jackson.annotation.JsonBackReference
import com.fasterxml.jackson.annotation.JsonManagedReference
import com.ffii.core.entity.BaseEntity
import com.ffii.fpsms.modules.jobOrder.entity.JobOrder
import com.ffii.fpsms.modules.master.entity.Items
import com.ffii.fpsms.modules.purchaseOrder.entity.PurchaseOrder
import com.ffii.fpsms.modules.purchaseOrder.entity.PurchaseOrderLine
import com.ffii.fpsms.modules.user.entity.User
import jakarta.persistence.*
import jakarta.validation.constraints.NotNull
import jakarta.validation.constraints.Size
import java.math.BigDecimal
import java.time.Instant
import java.time.LocalDate
import java.time.LocalDateTime

@Entity
@Table(name = "stock_in_line")
open class StockInLine : 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

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

    @JsonBackReference
    @ManyToOne
    @JoinColumn(name = "purchaseOrderLineId")
    open var purchaseOrderLine: PurchaseOrderLine? = null

    @JsonBackReference
    @ManyToOne
    @JoinColumn(name = "jobOrderId")
    open var jobOrder: JobOrder? = null


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

    @ManyToOne
    @JoinColumn(name = "stockTakeLineId")
    open var stockTakeLine: StockTakeLine? = null

    @NotNull
    @ManyToOne
    @JoinColumn(name = "stockInId", nullable = false)
    open var stockIn: StockIn? = null

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

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

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

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

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

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

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

    @Column(name = "expiryDate")
    open var expiryDate: LocalDate? = null

    @NotNull
    @Column(name = "status")
    open var status: String? = null

    @ManyToOne
    @JoinColumn(name = "userId")
    open var user: User? = null

    @ManyToOne
    @JoinColumn(name = "inventoryLotId")
    open var inventoryLot: InventoryLot? = null

    @OneToOne
    @JoinColumn(name = "inventoryLotLineId")
    open var inventoryLotLine: InventoryLotLine? = null

    @Column(name = "lotNo")
    open var lotNo: String? = null

    @Column(name = "productLotNo")
    open var productLotNo: String? = null

    @Column(name = "dnNo")
    open var dnNo: String? = null

    @Column(name = "invoiceNo")
    open var invoiceNo: String? = null

    @Column(name = "remarks")
    open var remarks: String? = null

    @Column(name = "type")
    open var type: String? = null

    @JsonManagedReference
    @OneToMany(mappedBy = "stockInLine", cascade = [CascadeType.ALL], orphanRemoval = true)
    open var escalationLog: MutableList<EscalationLog>? = mutableListOf()

    @JsonBackReference
    @ManyToOne
    @JoinColumn(name = "stockTransferId")
    open var stockTransferRecord: StockTransferRecord? = null
    fun getReceivedQtyForPol(): BigDecimal? = 
    purchaseOrderLine?.stockInLines?.sumOf { it.acceptedQty ?: BigDecimal.ZERO }

    /**
     * Total received qty (in M18 unit) for the same Purchase Order Line.
     * Used by QC UI so it shows PO qtyM18 / receivedQty in M18 terms.
     */
    fun getReceivedQtyM18ForPol(): BigDecimal? =
        purchaseOrderLine?.stockInLines?.sumOf { sil ->
            sil.acceptedQtyM18 ?: BigDecimal.ZERO
        }
}