// StockTakeRecordReponse.kt
package com.ffii.fpsms.modules.stock.web.model
import com.fasterxml.jackson.annotation.JsonFormat
import java.time.LocalDate
import java.math.BigDecimal
import java.time.LocalDateTime
import com.ffii.core.response.RecordsRes

data class LatestStockTakeRoundMetaResponse(
    val stockTakeRoundId: Long?,
    @JsonFormat(pattern = "yyyy-MM-dd")
    val planStartDate: LocalDate?,
)

data class AllPickedStockTakeListReponse(
    val id: Long,
    val stockTakeSession: String,
    @JsonFormat(pattern = "yyyy-MM-dd")
    val lastStockTakeDate: LocalDate?,
    val status: String,
    val currentStockTakeItemNumber: Int,
    val totalInventoryLotNumber: Int,
    @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")
    val startTime: LocalDateTime?,
    @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")
    val endTime: LocalDateTime?,
    val stockTakeId: Long,
    val stockTakeRoundId: Long?,
    val stockTakerName: String?,
    val approverName: String?,
    val TotalItemNumber: Int,
    val ReStockTakeTrueFalse: Boolean,
    @JsonFormat(pattern = "yyyy-MM-dd")
    val planStartDate: LocalDate?,
    val stockTakeSectionDescription: String?,
    val warehouseArea: String?,
    val storeId: String?,
)
data class InventoryLotDetailResponse(
    val id: Long,
    val inventoryLotId: Long,
    val itemId: Long,
    val itemCode: String?,
    val itemName: String?,
    val lotNo: String?,
    val expiryDate: LocalDate?,
    val productionDate: java.time.LocalDateTime?,
    val stockInDate: java.time.LocalDateTime?,
    val inQty: BigDecimal?,
    val outQty: BigDecimal?,
    val holdQty: BigDecimal?,
    val availableQty: BigDecimal?,
    val uom: String?,
    val warehouseCode: String?,
    val warehouse: String?,
    val warehouseSlot: String?,
    val warehouseArea: String?,
    val warehouseName: String?,
    val storeId: String?, 
    val varianceQty: BigDecimal? = null,
    val status: String?,
    val remarks: String?,
    val stockTakeRecordStatus: String?,
    val stockTakeRecordId: Long? = null, 
    val firstStockTakeQty: BigDecimal? = null, 
    val secondStockTakeQty: BigDecimal? = null, 
    val firstBadQty: BigDecimal? = null, 
    val secondBadQty: BigDecimal? = null, 
    val approverQty: BigDecimal? = null,
    val approverBadQty: BigDecimal? = null,
    val finalQty: BigDecimal? = null,
    val bookQty: BigDecimal? = null,
    val stockTakeSection: String? = null,
    val stockTakeSectionDescription: String? = null,
    val stockTakerName: String? = null,
    @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")
    val stockTakeEndTime: LocalDateTime? = null,
    @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")
    val approverTime: LocalDateTime? = null,
    val lastSelect: Int? = null,
)
data class InventoryLotLineListRequest(
    val warehouseCode: String
)
data class SaveStockTakeRecordRequest(
    val stockTakeRecordId: Long? = null, // null = 创建，非 null = 更新
    val inventoryLotLineId: Long, // 创建时需要，用于识别 inventory lot line
    val qty: BigDecimal, // QTY（第一次或第二次，后端判断）
    val badQty: BigDecimal,
  //  val stockTakerName: String,
    val remark: String? = null
)

/**
 * 盤點員單筆儲存成功回傳。勿直接回傳 [StockTakeRecord] Entity，否則 Jackson 序列化 warehouse/stockTake 等關聯時
 * 易因 Hibernate 代理／Session 已關閉導致 HTTP 500（controller 的 try/catch 無法攔截序列化階段錯誤）。
 */
data class SaveStockTakeRecordResponse(
    val id: Long?,
    val version: Int?,
    val itemId: Long?,
    val lotId: Long?,
    val inventoryLotId: Long?,
    val warehouseId: Long?,
    val stockTakeId: Long?,
    val stockTakeRoundId: Long?,
    val status: String?,
    val bookQty: BigDecimal?,
    val varianceQty: BigDecimal?,
    val pickerFirstStockTakeQty: BigDecimal?,
    val pickerFirstBadQty: BigDecimal?,
    val pickerSecondStockTakeQty: BigDecimal?,
    val pickerSecondBadQty: BigDecimal?,
)
data class SaveApproverStockTakeRecordRequest(
    val stockTakeRecordId: Long? = null, 
    val qty: BigDecimal, 
    val badQty: BigDecimal,
    val approverId: Long? = null,
    val approverQty: BigDecimal? = null,
    val approverBadQty: BigDecimal? = null,
    val lastSelect: Int? = null,
    //val remark: String? = null
)
data class BatchSaveStockTakeRecordRequest(
    val stockTakeId: Long,
    val stockTakeSection: String,
    val stockTakerId: Long,
    //val stockTakerName: String
)

/** 拣货员批量提交已输入行（与 [saveStockTakeRecord] 相同业务，逐条保存用户 qty/badQty）。 */
data class BatchSavePickerStockTakeInputRequest(
    val stockTakeId: Long,
    val stockTakeSection: String,
    val stockTakerId: Long,
    val records: List<SaveStockTakeRecordRequest>,
)

data class BatchSaveStockTakeRecordResponse(
    val successCount: Int,
    val errorCount: Int,
    val errors: List<String>
)
data class BatchSaveApproverStockTakeRecordRequest(
    val stockTakeId: Long,
    val stockTakeSection: String,
    val approverId: Long,
    val variancePercentTolerance: BigDecimal? = null,
)

data class BatchSaveApproverStockTakeAllRequest(
    val stockTakeId: Long,
    val approverId: Long,
    //val variancePercentTolerance: BigDecimal? = null,
    val itemKeyword: String? = null,
    val warehouseKeyword: String? = null,
    val sectionDescription: String? = null,
    val stockTakeSections: String? = null,
)

data class BatchSaveApproverStockTakeByIdsRequest(
    val stockTakeId: Long,
    val approverId: Long,
    val recordIds: List<Long> = emptyList(),
    /** 與單筆 saveApproverStockTakeRecord 相同；有值時依每筆 qty / lastSelect 保存。 */
    val records: List<SaveApproverStockTakeRecordRequest> = emptyList(),
)

data class BatchSaveApproverStockTakeRecordResponse(
    val successCount: Int,
    val errorCount: Int,
    val errors: List<String>
)
data class SearchStockTransactionRequest(
    val startDate: LocalDate? = null,
    val endDate: LocalDate? = null,
    val itemCode: String? = null,
    val itemName: String? = null,
    val type: String? = null,
    val pageNum: Int = 0,
    val pageSize: Int = 100
)
data class StockTransactionResponse(
    val id: Long,
    val transactionType: String, 
    val itemId: Long,
    val itemCode: String?,
    val itemName: String?,
    val uomId: Long? = null,
    val uomDesc: String? = null,
    val balanceQty: BigDecimal? = null,
    val qty: BigDecimal,
    val type: String?, 
    val date: LocalDate?,
    val status: String,
    val transactionDate: LocalDateTime?, 
    val lotNo: String?,
    val stockInId: Long?,
    val stockOutId: Long?,
    val remarks: String?
)

data class StockTransactionListResponse(
    val records: RecordsRes<StockTransactionResponse>
)