package com.ffii.fpsms.modules.deliveryOrder.entity

import com.fasterxml.jackson.annotation.JsonManagedReference
import com.ffii.core.entity.BaseEntity
import com.ffii.fpsms.m18.entity.M18DataLog
import com.ffii.fpsms.modules.deliveryOrder.enums.DeliveryOrderStatus
import com.ffii.fpsms.modules.deliveryOrder.enums.DeliveryOrderStatusConverter
import com.ffii.fpsms.modules.master.entity.Currency
import com.ffii.fpsms.modules.master.entity.Shop
import com.ffii.fpsms.modules.purchaseOrder.entity.PurchaseOrder
import com.ffii.fpsms.modules.user.entity.User
import jakarta.persistence.*
import jakarta.validation.constraints.NotNull
import jakarta.validation.constraints.Size
import java.time.LocalDateTime

@Entity
@Table(name = "delivery_order")
open class DeliveryOrder: BaseEntity<Long>() {
    @Size(max = 100)
    @Column(name = "code", length = 100)
    open var code: String? = null

    @ManyToOne
    @JoinColumn(name = "supplierId")
    open var supplier: Shop? = null

    @ManyToOne
    @JoinColumn(name = "shopId")
    open var shop: Shop? = null

    @ManyToOne
    @JoinColumn(name = "currencyId")
    open var currency: Currency? = null

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

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

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

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

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

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

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

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

    /** 加單：由 M18「加單」專用同步標記；一般 DO 為 false */
    @Column(name = "isExtra", nullable = false)
    open var isExtra: Boolean = false
}