package com.ffii.fpsms.m18.entity

import com.ffii.core.entity.BaseEntity
import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.Table
import jakarta.validation.constraints.NotNull
import jakarta.validation.constraints.Size

/**
 * Logs the result of creating a Goods Receipt Note (AN) in M18.
 * One row per [stock_in_line] included in that GRN (same POL can have multiple SILs / receipts);
 * retry logic uses [stock_in_line_id] to avoid duplicate GRNs per delivery batch.
 */
@Entity
@Table(name = "m18_goods_receipt_note_log")
open class M18GoodsReceiptNoteLog : BaseEntity<Long>() {

    /** M18 Goods Receipt Note record id (from M18 API response). */
    @NotNull
    @Column(name = "m18_record_id", nullable = false)
    open var m18RecordId: Long? = null

    /** M18 AN document code (from GET /root/api/read/an, data.mainan.code). */
    @Size(max = 60)
    @Column(name = "grn_code", length = 60)
    open var grnCode: String? = null

    /** Stock-in line this GRN line was created from. */
    @NotNull
    @Column(name = "stock_in_line_id", nullable = false)
    open var stockInLineId: Long? = null

    /** Purchase order line. */
    @NotNull
    @Column(name = "purchase_order_line_id", nullable = false)
    open var purchaseOrderLineId: Long? = null

    /** Purchase order. */
    @NotNull
    @Column(name = "purchase_order_id", nullable = false)
    open var purchaseOrderId: Long? = null

    /** Purchase order code (e.g. PO-xxx). */
    @Size(max = 60)
    @Column(name = "po_code", length = 60)
    open var poCode: String? = null

    /** Whether M18 API returned success. */
    @NotNull
    @Column(name = "status", nullable = false)
    open var status: Boolean? = null

    /** Optional message from M18 (e.g. error or warning). */
    @Size(max = 1000)
    @Column(name = "message", length = 1000)
    open var message: String? = null

    /** Request JSON sent to M18 GRN API (for debugging/audit). */
    @Column(name = "request_json", columnDefinition = "LONGTEXT")
    open var requestJson: String? = null
}
