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

import com.ffii.fpsms.modules.pickOrder.web.models.ConsoPickOrderRequest
import com.ffii.fpsms.modules.stock.entity.InventoryLotLineRepository
import com.ffii.fpsms.modules.stock.entity.StockInLineRepository
import com.ffii.fpsms.modules.stock.web.model.LotLineInfo
import jakarta.validation.Valid
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
import java.math.BigDecimal

@RequestMapping("/inventoryLotLine")
@RestController
class InventoryLotLineController (
    private val inventoryLotLineRepository: InventoryLotLineRepository,
    private val stockInLineRepository: StockInLineRepository,

){
//    @PostMapping("/test")
//    fun test(@Valid @RequestBody request: ConsoPickOrderRequest) :Any {
//        return inventoryLotLineRepository.findInventoryLotLineInfoByInventoryLotItemIdIn(request.ids)
//    }
    @PostMapping("/test")
    fun test(@Valid @RequestBody request: ConsoPickOrderRequest): Any {
        return inventoryLotLineRepository.findCurrentInventoryByItems(request.ids)
    }

    @GetMapping("/lot-detail/{stockInLineId}")
    fun getLotDetail(@Valid @PathVariable stockInLineId: Long): LotLineInfo {
        val stockInLine = stockInLineRepository.findById(stockInLineId).orElseThrow()
        val inventoryLotLine = stockInLine.inventoryLotLine!!
        val zero = BigDecimal.ZERO
        return LotLineInfo(
            inventoryLotLineId = inventoryLotLine.id!!,
            itemId = inventoryLotLine.inventoryLot!!.item!!.id!!,
            itemNo = inventoryLotLine.inventoryLot!!.item!!.code!!,
            itemName = inventoryLotLine.inventoryLot!!.item!!.name!!,
            lotNo = stockInLine.lotNo!!,
            remainingQty = (inventoryLotLine.inQty ?: zero)
                .minus(inventoryLotLine.outQty ?: zero)
                .minus(inventoryLotLine.holdQty ?: zero),
            uom = inventoryLotLine.stockUom!!.uom!!.udfudesc!!
        )
    }
}