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

import com.ffii.core.exception.NotFoundException
import com.ffii.fpsms.modules.master.entity.QcCategory
import com.ffii.fpsms.modules.master.entity.QcItem
import com.ffii.fpsms.modules.master.entity.projections.QcCategoryCombo
import com.ffii.fpsms.modules.master.service.QcCategoryService
import com.ffii.fpsms.modules.master.web.models.SaveQcCategoryRequest
import com.ffii.fpsms.modules.master.web.models.SaveQcCategoryResponse
import com.ffii.fpsms.modules.qc.entity.projection.QcCategoryInfo
import com.ffii.fpsms.modules.qc.entity.projection.QcItemInfo
import jakarta.validation.Valid
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/qcCategories")
class QcCategoryController(
    private val qcCategoryService: QcCategoryService
) {
    @GetMapping
    fun allQcCategories(): List<QcCategory> {
        return qcCategoryService.allQcCategories()
    }

    @DeleteMapping("/{id}")
    fun deleteQcItem(@PathVariable id: Long): List<QcCategory> {
        return qcCategoryService.markDeleted(id)
    }

    @GetMapping("/details/{id}")
    fun qcCategoryDetails(@PathVariable id: Long): QcCategory {
        return qcCategoryService.findQcCategoryById(id) ?: throw NotFoundException()
    }

    @PostMapping("/save")
    fun saveQcCategory(@Valid @RequestBody request: SaveQcCategoryRequest): SaveQcCategoryResponse {
        return qcCategoryService.saveQcCategory(request)
    }

    @GetMapping("/combo")
    fun getQcCategoryCombo(): List<QcCategoryCombo> {
        return qcCategoryService.getQcCategoryCombo();
    }

    @GetMapping("/{id}/items")
    fun getQcItemsByCategoryId(@PathVariable id: Long): List<QcItemInfo> {
        return qcCategoryService.getQcItemsByCategoryId(id)
    }

    @GetMapping("/items")
    fun getQcCategoryByTemplate(@RequestParam itemId: Long, @RequestParam(defaultValue = "IQC") type: String): QcCategoryInfo {
        return qcCategoryService.getQcCategoryInfoByMapping(itemId, type) ?: throw NotFoundException()
    }
}