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

import com.ffii.core.entity.BaseEntity
import com.ffii.fpsms.modules.master.entity.Shop
import com.ffii.fpsms.m18.entity.M18DataLog
import com.ffii.fpsms.modules.master.entity.Currency
import com.ffii.fpsms.modules.purchaseOrder.enums.PurchaseOrderStatus
import com.ffii.fpsms.modules.purchaseOrder.enums.PurchaseOrderStatusConverter
import com.ffii.fpsms.modules.purchaseOrder.enums.PurchaseOrderType
import com.ffii.fpsms.modules.purchaseOrder.enums.PurchaseOrderTypeConverter
import jakarta.persistence.*
import jakarta.validation.constraints.NotNull
import jakarta.validation.constraints.Size
import java.time.LocalDateTime

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

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

    @ManyToOne
    @JoinColumn(name = "shopId")
    open var shop: Shop? = 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 = PurchaseOrderStatusConverter::class)
    open var status: PurchaseOrderStatus? = null

    @NotNull
    @Column(name = "type", nullable = true, length = 20)
    @Convert(converter = PurchaseOrderTypeConverter::class)
    open var type: PurchaseOrderType? = null

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

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

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

    /** M18 PO header [createUid] from sync; used for GRN rules (see [com.ffii.fpsms.m18.M18GrnRules]). */
    @Column(name = "m18CreatedUId")
    open var m18CreatedUId: Long? = null
}