package com.ffii.fpsms.modules.purchaseOrder.web

import com.ffii.core.response.RecordsRes
import com.ffii.core.support.JdbcDao
import com.ffii.core.utils.CriteriaArgsBuilder
import com.ffii.core.utils.PagingUtils
import com.ffii.fpsms.modules.master.entity.Items
import com.ffii.fpsms.modules.master.service.ItemsService
import com.ffii.fpsms.modules.master.web.models.MessageResponse
import com.ffii.fpsms.modules.purchaseOrder.entity.PurchaseOrder
import com.ffii.fpsms.modules.purchaseOrder.entity.projections.PurchaseOrderDataClass
import com.ffii.fpsms.modules.purchaseOrder.entity.projections.PurchaseOrderInfo
import com.ffii.fpsms.modules.purchaseOrder.service.PurchaseOrderService
import com.ffii.fpsms.modules.purchaseOrder.web.model.PagingRequest
import com.ffii.fpsms.modules.stock.web.model.SaveStockInLineRequest
import jakarta.servlet.http.HttpServletRequest
import org.springframework.data.domain.Page
import org.springframework.data.domain.PageRequest
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/po")
class PurchaseOrderController(
    private val purchaseOrderService: PurchaseOrderService
) {
    @GetMapping("/list")
    fun getPoList(
        request: HttpServletRequest
//            @RequestParam(required = false) pageNum: Int,
//            @RequestParam(required = false) pageSize: Int
    ): RecordsRes<PurchaseOrderDataClass> {
//        println("request")
//        println(request)
        val criteriaArgs = CriteriaArgsBuilder.withRequest(request)
            .addStringLike("code")
            .addString("status")
            .addBoolean("escalated")
            .build()
        println(criteriaArgs)
        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 = purchaseOrderService.getPoList(criteriaArgs)
        val paginatedList = PagingUtils.getPaginatedList(fullList,pageSize, pageNum)
        return RecordsRes(paginatedList, fullList.size)
    }

    @GetMapping("/testing")
    fun testing(request: HttpServletRequest) {
        val criteriaArgs = CriteriaArgsBuilder.withRequest(request)
            .addStringLike("code")
            .addStringLike("name")
            .addBoolean("escalated")
            .build()
            println("criteriaArgs")
            println(criteriaArgs)
    }

    @GetMapping("/detail/{id}") // purchaseOrderId
    fun getDetailedPo(@PathVariable id: Long): Map<String, Any> {
        return purchaseOrderService.getDetailedPo(id)
    }

    @PostMapping("/start/{id}") // purchaseOrderId
    fun startPo(@PathVariable id: Long): MessageResponse {
        return purchaseOrderService.startPo(id)
    }
    @PostMapping("/check/{id}") // purchaseOrderId
    fun checkPolAndCompletePo(@PathVariable id: Long): MessageResponse {
        return purchaseOrderService.checkPolAndCompletePo(id)
    }

}