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

import com.ffii.core.exception.NotFoundException
import com.ffii.fpsms.modules.master.entity.Items
import com.ffii.fpsms.modules.master.service.QcItemAllService
import com.ffii.fpsms.modules.master.web.models.*
import com.ffii.fpsms.modules.qc.entity.projection.QcItemInfo
import jakarta.validation.Valid
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/qcItemAll")
class QcItemAllController(
    private val qcItemAllService: QcItemAllService
) {
    @GetMapping("/categoriesWithItemCountsAndType")
    fun getAllQcCategoriesWithItemCountsAndType(): List<QcCategoryWithItemCountAndType> {
        return qcItemAllService.getAllQcCategoriesWithItemCountsAndType()
    }

    @GetMapping("/categoryType/{qcCategoryId}")
fun getCategoryType(@PathVariable qcCategoryId: Long): CategoryTypeResponse {
    val type = qcItemAllService.getCategoryType(qcCategoryId)
    return CategoryTypeResponse(type = type)
}

    @PutMapping("/categoryType")
    fun updateCategoryType(
        @RequestParam qcCategoryId: Long,
        @RequestParam type: String
    ) {
        qcItemAllService.updateCategoryType(qcCategoryId, type)
    }
    // Get item count by qc category
    @GetMapping("/itemCount/{qcCategoryId}")
    fun getItemCountByQcCategory(@PathVariable qcCategoryId: Long): Long {
        return qcItemAllService.getItemCountByQcCategory(qcCategoryId)
    }
    
    // Get qc item count by qc category
    @GetMapping("/qcItemCount/{qcCategoryId}")
    fun getQcItemCountByQcCategory(@PathVariable qcCategoryId: Long): Long {
        return qcItemAllService.getQcItemCountByQcCategory(qcCategoryId)
    }
    
    // Get all qc categories with item counts (batch operation for performance)
    @GetMapping("/categoriesWithItemCounts")
    fun getAllQcCategoriesWithItemCounts(): List<QcCategoryWithItemCount> {
        return qcItemAllService.getAllQcCategoriesWithItemCounts()
    }
    
    // Get all qc categories with qc item counts (batch operation for performance)
    @GetMapping("/categoriesWithQcItemCounts")
    fun getAllQcCategoriesWithQcItemCounts(): List<QcCategoryWithQcItemCount> {
        return qcItemAllService.getAllQcCategoriesWithQcItemCounts()
    }
    
    // Check if can delete qc category
    @GetMapping("/canDeleteQcCategory/{id}")
    fun canDeleteQcCategory(@PathVariable id: Long): Boolean {
        return qcItemAllService.canDeleteQcCategory(id)
    }
    
    // Check if can delete qc item
    @GetMapping("/canDeleteQcItem/{id}")
    fun canDeleteQcItem(@PathVariable id: Long): Boolean {
        return qcItemAllService.canDeleteQcItem(id)
    }
    
    // Get item and qc category mappings
    @GetMapping("/itemMappings")
    fun getItemQcCategoryMappings(
        @RequestParam(required = false) qcCategoryId: Long?,
        @RequestParam(required = false) itemId: Long?
    ): List<ItemQcCategoryMappingInfo> {
        return qcItemAllService.getItemQcCategoryMappings(qcCategoryId, itemId)
    }
    
    // Save item and qc category mapping
    @PostMapping("/itemMapping")
    fun saveItemQcCategoryMapping(
        @RequestParam itemId: Long,
        @RequestParam qcCategoryId: Long,
        @RequestParam type: String
    ): ItemQcCategoryMappingInfo {
        return qcItemAllService.saveItemQcCategoryMapping(itemId, qcCategoryId, type)
    }
    
    // Delete item and qc category mapping
    @DeleteMapping("/itemMapping/{mappingId}")
    fun deleteItemQcCategoryMapping(@PathVariable mappingId: Long) {
        qcItemAllService.deleteItemQcCategoryMapping(mappingId)
    }
    
    // Get qc category and qc item mappings
    @GetMapping("/qcItemMappings/{qcCategoryId}")
    fun getQcCategoryQcItemMappings(@PathVariable qcCategoryId: Long): List<QcItemInfo> {
        return qcItemAllService.getQcCategoryQcItemMappings(qcCategoryId)
    }
    
    // Save qc category and qc item mapping
    @PostMapping("/qcItemMapping")
    fun saveQcCategoryQcItemMapping(
        @RequestParam qcCategoryId: Long,
        @RequestParam qcItemId: Long,
        @RequestParam order: Int,
        @RequestParam(required = false) description: String?
    ): QcItemInfo {
        qcItemAllService.saveQcCategoryQcItemMapping(qcCategoryId, qcItemId, order, description)
        // Reload to get the projection (avoids circular reference issues)
        val mappings = qcItemAllService.getQcCategoryQcItemMappings(qcCategoryId)
        return mappings.firstOrNull { it.qcItemId == qcItemId } ?: throw NotFoundException()
    }
    
    // Delete qc category and qc item mapping
    @DeleteMapping("/qcItemMapping/{mappingId}")
    fun deleteQcCategoryQcItemMapping(@PathVariable mappingId: Long) {
        qcItemAllService.deleteQcCategoryQcItemMapping(mappingId)
    }
    
    // Save qc category with validation
    @PostMapping("/saveQcCategory")
    fun saveQcCategoryWithValidation(@Valid @RequestBody request: SaveQcCategoryRequest): SaveQcCategoryResponse {
        return qcItemAllService.saveQcCategoryWithValidation(request)
    }
    
    // Delete qc category with validation
    @DeleteMapping("/deleteQcCategory/{id}")
    fun deleteQcCategoryWithValidation(@PathVariable id: Long): DeleteResponse {
        return qcItemAllService.deleteQcCategoryWithValidation(id)
    }
    
    // Save qc item with validation
    @PostMapping("/saveQcItem")
    fun saveQcItemWithValidation(@Valid @RequestBody request: SaveQcItemRequest): SaveQcItemResponse {
        return qcItemAllService.saveQcItemWithValidation(request)
    }
    
    // Delete qc item with validation
    @DeleteMapping("/deleteQcItem/{id}")
    fun deleteQcItemWithValidation(@PathVariable id: Long): DeleteResponse {
        return qcItemAllService.deleteQcItemWithValidation(id)
    }
    
    // Get item by code (for Tab 0 - validate item code input)
    @GetMapping("/itemByCode/{code}")
    fun getItemByCode(@PathVariable code: String): Items? {
        return qcItemAllService.getItemByCode(code)
    }
}

