package com.ffii.fpsms.modules.stock.web.model

import jakarta.validation.constraints.NotBlank
import jakarta.validation.constraints.NotNull
import java.time.LocalDate
import java.time.LocalDateTime
import java.math.BigDecimal
enum class StockOutStatus(val status: String) {
    PENDING("pending"),
    COMPLETE("completed"),
//    CANCELLED("cancelled")
}
enum class StockOutLineStatus(val status: String) {
    PENDING("pending"),
    DETERMINE1("determine1"), // qc failed qty?
    LOT_CHANGE_APPROVAL("lot-change"), // just a flag for frontend
    CHECKED("checked"),
    PARTIALLY_COMPLETE("partially_completed"),
    REJECTED("rejected"),
    COMPLETE("completed"), // == picked
}
data class SaveStockOutRequest( // not usable
    val id: Long?,
    val type: String, // delivery || pick || etc
    val deliveryOrderCode: String?,
    val pickOrderCode: String?,
    val consoCode: String?,
    val completeDate: LocalDateTime?,
    val status: StockOutStatus?,
    val handler: Long?,
    val targetOutletId: Long?,
    val remarks: String?,
    val stockOutLine: List<UpdateStockOutLineRequest>
)
data class CreateStockOutLineRequest(
    val consoCode: String,
    val pickOrderLineId: Long,
    val inventoryLotLineId: Long,
    val qty: Double,
)
data class UpdateStockOutLineRequest(
    val id: Long,
    val itemId: Long,
    val qty: Double,
    val pickOrderLineId: Long,
    val status: String,

    val inventoryLotLineId: Long?,
    val pickTime: LocalDateTime?,
//    val pickerId: Long?
)
data class CreateStockOutLineWithoutConsoRequest(
    val pickOrderLineId: Long,
    val inventoryLotLineId: Long,
    val stockOutId: Long, 
    val qty: Double,
)
data class UpdateStockOutLineStatusRequest(
    val id: Long,
    val status: String,
    val qty: Double? = null,
    val remarks: String? = null,
    val skipLedgerWrite: Boolean? = false,
    val skipInventoryWrite: Boolean? = false,
    /** When true (batch submit path): skip per-line POL/PO rollup, conso completion scan, duplicate BagLotLine; caller runs these once at end. */
    val deferAggregatePickOrderEffects: Boolean? = false
)
data class UpdateStockOutLineStatusByQRCodeAndLotNoRequest(
    val pickOrderLineId: Long,
    val inventoryLotNo: String,
    val stockInLineId: Long? = null,
    val stockOutLineId: Long,
    val itemId: Long,
    val status: String
)
data class QrPickSubmitLineRequest(
    val stockOutLineId: Long,
    val pickOrderLineId: Long,
    val inventoryLotLineId: Long?,   
    val requiredQty: BigDecimal?,    
    val actualPickQty: BigDecimal?,  
    val stockOutLineStatus: String?, 
    val pickOrderConsoCode: String?, 
    val noLot: Boolean = false     
)
 
data class QrPickBatchSubmitRequest(
    val userId: Long,
    val lines: List<QrPickSubmitLineRequest>
)
data class BatchScanLineRequest(
    val pickOrderLineId: Long,
    val inventoryLotLineId: Long?, // 如果有 lot，提供 lotId；如果没有则为 null
    val pickOrderConsoCode: String,
    val lotNo: String?, // 用于日志和验证
    val itemId: Long,
    val itemCode: String,
    val stockOutLineId: Long? = null // ✅ 新增：如果已有 stockOutLineId，直接使用
)

data class BatchScanRequest(
    val userId: Long,
    val lines: List<BatchScanLineRequest>
)
data class BatchStockOutRequest(
    val type: String,                // "Expiry" / "Miss" / "Bad"
    val handler: Long?,
    val lines: List<BatchStockOutLineRequest>
)

data class BatchStockOutLineRequest(
    val inventoryLotLineId: Long,
    val qty: Double
)

data class BatchStockOutResult(
    val stockOutId: Long?,
    val processedCount: Int,
    val skippedCount: Int = 0
)