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

import com.ffii.fpsms.modules.pickOrder.entity.PickOrderLineRepository
import com.ffii.fpsms.modules.pickOrder.entity.PickOrderRepository
import com.ffii.fpsms.modules.pickOrder.enums.PickOrderStatus
import com.ffii.fpsms.modules.purchaseOrder.service.PurchaseOrderLineService
import com.ffii.fpsms.modules.stock.service.SuggestedPickLotService
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.RequestMapping
import org.springframework.web.bind.annotation.RestController
import com.ffii.fpsms.modules.master.web.models.MessageResponse
import com.ffii.fpsms.modules.pickOrder.entity.PickOrder
import com.ffii.fpsms.modules.stock.web.model.SuggestedPickLotForPolRequest
import kotlin.jvm.optionals.getOrDefault
import kotlin.jvm.optionals.getOrNull
import org.springframework.web.bind.annotation.RequestBody
import com.ffii.fpsms.modules.stock.web.model.PickAnotherLotRequest
import com.ffii.fpsms.modules.stock.web.model.UpdateSuggestedLotLineIdRequest
@RequestMapping("/suggestedPickLot")
@RestController
class SuggestedPickLotController(
    val suggestedPickLotService: SuggestedPickLotService,
    val pickOrderLineService: PurchaseOrderLineService,
    val pickOrderRepository: PickOrderRepository
) {


    @GetMapping("/test/{pickOrderId}")
    fun test(@PathVariable pickOrderId: Long): Any {
        val test1 = pickOrderRepository.findById(pickOrderId).getOrDefault(PickOrder())

        return suggestedPickLotService.suggestionForPickOrderLines(SuggestedPickLotForPolRequest(pickOrderLines = test1.pickOrderLines))
    }

@PostMapping("/resuggest/{pickOrderId}")
fun resuggestPickOrder(@PathVariable pickOrderId: Long): MessageResponse {
    return suggestedPickLotService.resuggestPickOrder(pickOrderId)
}
@PostMapping("/recordFailLot")
fun recordFailInventoryLotLine(@RequestBody request: PickAnotherLotRequest): MessageResponse {
    return suggestedPickLotService.recordFailInventoryLotLine(request)
}
@PostMapping("/update-suggested-lot/{suggestedPickLotId}")
fun updateSuggestedLotLineId(
    @PathVariable suggestedPickLotId: Long,
    @RequestBody request: UpdateSuggestedLotLineIdRequest
): MessageResponse {
    return suggestedPickLotService.updateSuggestedLotLineId(suggestedPickLotId, request.newLotLineId)
}
}