package com.ffii.fpsms.modules.master.service

import com.ffii.core.support.AbstractBaseEntityService
import com.ffii.fpsms.modules.master.entity.QcCategory
import com.ffii.fpsms.modules.master.entity.QcCategoryRepository
import com.ffii.fpsms.modules.master.entity.projections.QcCategoryCombo
import com.ffii.fpsms.modules.qc.entity.projection.QcCategoryInfo
import org.springframework.stereotype.Service

@Service
open class QcCategoryService(
    private val qcCategoryRepository: QcCategoryRepository,
    private val itemsService: ItemsService
) {
    open fun allQcCategories(): List<QcCategory> {
        return qcCategoryRepository.findAllByDeletedIsFalse()
    }

    open fun getQcCategoryCombo(): List<QcCategoryCombo> {
        return qcCategoryRepository.findQcCategoryComboByDeletedIsFalse();
    }

    open fun getQcCategoryInfoByMapping(itemId : Long, type: String): QcCategoryInfo? {
        var result = qcCategoryRepository.findQcCategoryInfoByItemIdAndType(itemId, type)
        if (result == null) {   // Temporarily fix for missing QC template (Auto find template from similar items)
            val args = mapOf(
                "itemId" to itemId,
                "qcType" to type
            )
            val similarItemIds = itemsService.getItemsIdWithSameCategoryForQc(args);
            result = qcCategoryRepository.findQcCategoryInfoByItemIdAndType(similarItemIds[0].toLong(), type)
        }
        return result;
    }
}