package com.ffii.fpsms.modules.pickOrder.entity

import com.fasterxml.jackson.annotation.JsonManagedReference
import com.ffii.core.entity.BaseEntity
import com.ffii.fpsms.modules.deliveryOrder.entity.DeliveryOrder
import com.ffii.fpsms.modules.jobOrder.entity.JobOrder
import com.ffii.fpsms.modules.master.entity.BomMaterial
import com.ffii.fpsms.modules.pickOrder.enums.PickOrderStatus
import com.ffii.fpsms.modules.pickOrder.enums.PickOrderStatusConverter
import com.ffii.fpsms.modules.pickOrder.enums.PickOrderType
import com.ffii.fpsms.modules.pickOrder.enums.PickOrderTypeConverter
import com.ffii.fpsms.modules.user.entity.User
import jakarta.persistence.*
import jakarta.validation.constraints.NotNull
import jakarta.validation.constraints.Size
import org.hibernate.annotations.Formula
import java.time.Instant
import java.time.LocalDateTime

@Entity
@Table(name = "pick_order")
open class PickOrder: BaseEntity<Long>() {
    @Size(max = 255)
    @NotNull
    @Column(name = "code", nullable = false)
    open var code: String? = null

    @Size(max = 255)
    @Column(name = "consoCode")
    open var consoCode: String? = null

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

    @ManyToOne
    @JoinColumn(name = "doId")
    open var deliveryOrder: DeliveryOrder? = null

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

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

    @Convert(converter = PickOrderTypeConverter::class)
    @Column(name = "type", length = 30)
    open var type: PickOrderType? = null

    @NotNull
    @Convert(converter = PickOrderStatusConverter::class)
    @Column(name = "status", nullable = false, length = 30)
    open var status: PickOrderStatus? = null

    @ManyToOne
    @JoinColumn(name = "releasedBy", referencedColumnName = "id")
    open var releasedBy: User? = null

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

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

    @ManyToOne
    @JoinColumn(name = "assignTo", referencedColumnName = "id")
    open var assignTo: User? = null

      @Column(name = "totalLines")
   open var totalLines: Int? = null

    @Column(name = "submittedLines")
    open var submittedLines: Int? = null
}