package com.ffii.fpsms.modules.master.entity

import com.ffii.core.support.AbstractRepository
import com.ffii.fpsms.modules.master.entity.projections.QcCategoryCombo
import com.ffii.fpsms.modules.qc.entity.projection.QcCategoryInfo
import org.springframework.data.jpa.repository.Query
import org.springframework.stereotype.Repository

@Repository
interface QcCategoryRepository : AbstractRepository<QcCategory, Long> {
    fun findAllByDeletedIsFalse(): List<QcCategory>;
    fun findByIdAndDeletedIsFalse(id: Long): QcCategory?;

    fun findQcCategoryComboByDeletedIsFalse(): List<QcCategoryCombo>;

//    @Query(
//        value = """
//            SELECT qcc.* FROM qc_category qcc
//            LEFT JOIN items_qc_category_mapping map ON map.qcCategoryId = qcc.id
//            WHERE map.type = :type AND map.itemId = :itemId AND qcc.deleted = false
//            ORDER BY qcc.id
//            LIMIT 1
//        """,
//        nativeQuery = true
//    )
    @Query(
        """
            SELECT qcc FROM QcCategory qcc
            JOIN ItemsQcCategoryMapping map ON qcc.id = map.qcCategoryId
            WHERE map.itemId = :itemId AND map.type = :type AND qcc.deleted = false
        """
    )
    fun findQcCategoryInfoByItemIdAndType(itemId: Long, type: String): QcCategoryInfo?;

    @Query(
        """
        SELECT qcc FROM QcCategory qcc
        WHERE qcc.isDefault = :isDefault AND qcc.deleted = false
        ORDER BY qcc.id ASC
    """
    )
    fun findQcCategoryInfoByIsDefault(isDefault: Boolean): List<QcCategoryInfo>;

//    fun findByItemIdAndType(itemId: Long, type: String): QcCategory?;
}