package com.ffii.fpsms.modules.master.web

import com.ffii.core.exception.NotFoundException
import com.ffii.core.utils.JwtTokenUtil
import com.ffii.fpsms.modules.master.entity.QcItem
import com.ffii.fpsms.modules.master.entity.QcItemRepository
import com.ffii.fpsms.modules.master.service.QcItemService
import com.ffii.fpsms.modules.master.web.models.SaveQcItemRequest
import com.ffii.fpsms.modules.master.web.models.SaveQcItemResponse
import com.ffii.fpsms.modules.qc.entity.projection.QcCategoryInfo
import com.ffii.fpsms.modules.qc.entity.projection.QcItemInfo
import jakarta.validation.Valid
import jakarta.validation.constraints.NotBlank
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/qcItems")
class QcItemController(
    private val qcItemService: QcItemService,
    private val qcItemRepository: QcItemRepository,
) {

    var logger: Logger = LoggerFactory.getLogger(JwtTokenUtil::class.java)
    @GetMapping
    fun allQcItems(): List<QcItem> {
        return qcItemService.allQcItems()
    }

    @DeleteMapping("/{id}")
//    @ResponseStatus(HttpStatus.NO_CONTENT)
    fun deleteQcItem(@PathVariable id: Long): List<QcItem> {
        return qcItemService.markDeleted(id)
    }

    @GetMapping("/qcItemDetails/{id}")
    fun qcItemDetails(@PathVariable id: Long): QcItem {
        logger.info(qcItemService.findQcItemById(id)?.code)
        return qcItemService.findQcItemById(id) ?: throw NotFoundException()
    }

    @GetMapping("/template")
    fun qcItemsByTemplate(@RequestParam itemId: Long, @RequestParam(defaultValue = "IQC") type: String): List<QcItemInfo> {
        return qcItemService.findQcItemsByTemplate(itemId, type) ?: throw NotFoundException()
    }

    @PostMapping("/save")
    fun saveQcItem(@Valid @RequestBody request: SaveQcItemRequest): SaveQcItemResponse {
        return qcItemService.saveQcItem(request)
    }
}

data class GetQcItemRequest(
    @field:NotBlank(message = "Item Id cannot be empty")
    val itemId: Long,
    @field:NotBlank(message = "Type cannot be empty")
    val type: String,
)