package com.ffii.fpsms.modules.master.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.entity.EquipmentDetail
import com.ffii.fpsms.modules.master.service.EquipmentDetailService
import jakarta.servlet.http.HttpServletRequest
import jakarta.validation.Valid
import org.springframework.web.bind.annotation.*
import java.util.Collections.emptyList
import com.ffii.fpsms.modules.master.web.models.NewEquipmentDetailRequest
import com.ffii.fpsms.modules.master.web.models.UpdateMaintenanceRequest

@RestController
@RequestMapping("/EquipmentDetail")
class EquipmentDetailController(
    private val equipmentDetailService: EquipmentDetailService
) {
    @GetMapping
    fun allEquipmentDetail(): List<EquipmentDetail> {
        return equipmentDetailService.allEquipmentDetails()
    }

//    @GetMapping("/getRecordByPage")
//    fun getAllItemsByPage(@RequestBody filterRequest: HttpServletRequest): RecordsRes<Map<String, Any>> {
//        val pageSize = filterRequest.getParameter("pageSize").toString().toInt(); // Default to 10 if not provided
//        val pageNumber = filterRequest.getParameter("pageNum").toString().toInt(); // Default to 0 if not provided
//        val criteriaArgs = CriteriaArgsBuilder.withRequest(filterRequest)
//            .addStringLike("name")
//            .build();
//
//        val fullList = itemsService.getItemsByPage(criteriaArgs)?.subList(pageSize*pageNumber+1, pageSize*pageNumber+pageSize)
//        val outputList = fullList?.subList(pageSize*pageNumber+1, pageSize*pageNumber+pageSize)
//
//        return RecordsRes(outputList as List<Map<String, Any>>?, fullList?.size)
//    }

@GetMapping("/getRecordByPage")
fun getAllEquipmentDetailByPage(
    request: HttpServletRequest
): RecordsRes<Map<String, Any>> {
    val criteriaArgs = CriteriaArgsBuilder.withRequest(request)
        .addStringLike("name")
        .addStringLike("code")
        .addStringLike("description")
        .addStringLike("id")
        .addStringLike("equipmentCode")
        .addBoolean("repairAndMaintenanceStatus")
        .build()

    val pageSize = request.getParameter("pageSize")?.toIntOrNull() ?: 10
    val pageNum = request.getParameter("pageNum")?.toIntOrNull() ?: 1

    val fullList = equipmentDetailService.getEquipmentDetailsByPage(criteriaArgs) ?: emptyList()
    val paginatedList = PagingUtils.getPaginatedList(fullList, pageSize, pageNum)

    return RecordsRes(paginatedList as List<Map<String, Any>>, fullList.size)
}

    @GetMapping("/details/{id}")
    fun getEquipmentDetail(@PathVariable id: Long): EquipmentDetail? {
        return equipmentDetailService.findById(id)
    }

    @GetMapping("/byEquipmentId/{equipmentId}")
    fun getEquipmentDetailsByEquipmentId(@PathVariable equipmentId: Long): RecordsRes<Map<String, Any>> {
        val fullList = equipmentDetailService.getEquipmentDetailsByEquipmentId(equipmentId) ?: emptyList()
        return RecordsRes(fullList as List<Map<String, Any>>, fullList.size)
    }

    @GetMapping("/byDescriptionIncludingDeleted/{description}")
    fun getEquipmentDetailsByDescriptionIncludingDeleted(@PathVariable description: String): RecordsRes<Map<String, Any>> {
        val fullList = equipmentDetailService.getEquipmentDetailsByDescriptionIncludingDeleted(description) ?: emptyList()
        return RecordsRes(fullList as List<Map<String, Any>>, fullList.size)
    }

    @PutMapping("/update/{id}")
    fun updateMaintenanceAndRepair(
        @PathVariable id: Long,
        @RequestBody request: UpdateMaintenanceRequest
    ): EquipmentDetail {
        return equipmentDetailService.updateMaintenanceAndRepair(id, request)
    }
    @PostMapping("/save")
    fun saveEquipmentDetail(@Valid @RequestBody equipmentDetail: NewEquipmentDetailRequest): EquipmentDetail {
        return equipmentDetailService.saveEquipmentDetail(equipmentDetail)
    }

    @DeleteMapping("/delete/{id}")
    fun deleteEquipmentDetail(@PathVariable id: Long) {
        equipmentDetailService.deleteEquipmentDetail(id)
    }

}