package com.ffii.fpsms.modules.pickOrder.web

import com.ffii.core.response.RecordsRes
import com.ffii.core.utils.CriteriaArgsBuilder
import com.ffii.core.utils.PagingUtils
import com.ffii.fpsms.modules.master.web.models.MessageResponse
import com.ffii.fpsms.modules.pickOrder.entity.PickOrderRepository
import com.ffii.fpsms.modules.pickOrder.entity.projection.PickOrderInfo
import com.ffii.fpsms.modules.pickOrder.service.PickOrderService
import com.ffii.fpsms.modules.pickOrder.web.models.*
import com.ffii.fpsms.modules.pickOrder.web.models.ConsoPickOrderRequest
import com.ffii.fpsms.modules.pickOrder.web.models.ConsoPickOrderResponse
import com.ffii.fpsms.modules.pickOrder.web.models.ReleaseConsoPickOrderRequest
import com.ffii.fpsms.modules.pickOrder.web.models.SearchPickOrderRequest
import jakarta.servlet.http.HttpServletRequest
import jakarta.validation.Valid
import org.springframework.data.domain.Page
import org.springframework.data.domain.PageRequest
import org.springframework.data.domain.Pageable
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.ModelAttribute
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.time.DateTimeException
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

@RestController
@RequestMapping("/pickOrder")
class PickOrderController(
    private val pickOrderService: PickOrderService,
    private val pickOrderRepository: PickOrderRepository,
) {
    @GetMapping("/list")
    fun allPickOrders(): List<PickOrderInfo> {
        return pickOrderService.allPickOrders();
    }

    @GetMapping("/getRecordByPage")
    fun allPickOrdersByPage(@ModelAttribute request: SearchPickOrderRequest): RecordsRes<PickOrderInfo> {
        return pickOrderService.allPickOrdersByPage(request);
    }
    @GetMapping("/getRecordByPage-conso")
    fun allConsoPickOrdersByPage(request: HttpServletRequest): RecordsRes<Map<String, Any>> {
        val criteriaArgs = CriteriaArgsBuilder.withRequest(request)
            .addStringLike("consoCode")
            .addString("status")
            .build()
        val pageSize = request.getParameter("pageSize")?.toIntOrNull() ?: 10 // Default to 10 if not provided
        val pageNum = request.getParameter("pageNum")?.toIntOrNull() ?: 1 // Default to 1 if not provided
        val fullList = pickOrderService.getConsoPickOrderList(criteriaArgs)
        val paginatedList = PagingUtils.getPaginatedList(fullList,pageSize, pageNum)
        return RecordsRes(paginatedList, fullList.size)
    }

    @GetMapping("/get-pickorder-line-byPage")
    fun getPickOrderLine(request: HttpServletRequest): RecordsRes<Map<String, Any>> {
        val criteriaArgs = CriteriaArgsBuilder.withRequest(request)
            .addString("consoCode")
            .build()
        val pageSize = request.getParameter("pageSize")?.toIntOrNull() ?: 10 // Default to 10 if not provided
        val pageNum = request.getParameter("pageNum")?.toIntOrNull() ?: 1 // Default to 1 if not provided
        val fullList = pickOrderService.getPickOrderLine(criteriaArgs)
        val paginatedList = PagingUtils.getPaginatedList(fullList,pageSize, pageNum)
        return RecordsRes(paginatedList, fullList.size)
    }

    // Consolidating Pick Orders
    @PostMapping("/conso")
    fun consoPickOrders(@Valid @RequestBody request: ConsoPickOrderRequest): List<PickOrderInfo> {
        return pickOrderService.consoPickOrders(request)
    }

    @PostMapping("/deconso")
    fun deconsoPickOrders(@Valid @RequestBody request: ConsoPickOrderRequest): List<PickOrderInfo> {
        return pickOrderService.deconsoPickOrders(request)
    }

    @GetMapping("/consoDetail/{consoCode}")
    fun consoPickOrderDetail(@PathVariable consoCode: String): ConsoPickOrderResponse {
        return pickOrderService.consoPickOrderDetail(consoCode);
    }

    // Release Pick Order
    @GetMapping("/pre-release-info/{consoCode}")
    fun releaseConsoPickOrderInfo(@PathVariable consoCode: String): ReleasePickOrderInfoResponse {
        return pickOrderService.releaseConsoPickOrderInfo(consoCode);
    }

    @PostMapping("/releaseConso")
    fun releaseConsoPickOrderAction(@Valid @RequestBody request: ReleaseConsoPickOrderRequest): ReleasePickOrderInfoResponse {
        return pickOrderService.releaseConsoPickOrderAction(request)
    }

    // Start Pick Order
    @GetMapping("/pickConso/{consoCode}")
    fun pickConsoPickOrderInfo(@PathVariable consoCode: String): ReleasePickOrderInfoResponse {
        return pickOrderService.releaseConsoPickOrderInfo(consoCode);
    }

    @PostMapping("/consoPickOrder/complete/{consoCode}")
    fun completeConsoPickOrders(@PathVariable consoCode: String): MessageResponse {
        return pickOrderService.completeStockOut(consoCode)
    }

    @PostMapping("/create")
    fun create(@Valid @RequestBody request: SavePickOrderRequest): MessageResponse {
        return pickOrderService.create(request)
    }
}