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

import com.ffii.core.support.AbstractRepository
import com.ffii.fpsms.modules.master.entity.projections.WarehouseCombo
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.data.jpa.repository.Query
import org.springframework.stereotype.Repository
import java.io.Serializable

@Repository
interface WarehouseRepository : AbstractRepository<Warehouse, Long> {
    fun findWarehouseComboByDeletedFalse(): List<WarehouseCombo>;

    fun findByIdAndDeletedIsFalse(id: Serializable): Warehouse?;

    fun findByCodeAndDeletedIsFalse(code: String): Warehouse?;

    fun findAllByDeletedIsFalse(): List<Warehouse>;
    fun findAllByStockTakeSectionAndDeletedIsFalse(stockTakeSection: String): List<Warehouse>;
    fun findDistinctStockTakeSectionsByDeletedIsFalse(): List<String>;
    fun findAllByIdIn(ids: List<Long>): List<Warehouse>;
    fun findAllByCodeAndDeletedIsFalse(code: String): List<Warehouse>

    @Query(
        """
        SELECT COUNT(w) FROM Warehouse w
        WHERE w.deleted = false
        AND (w.stockTakeSection IS NULL OR TRIM(w.stockTakeSection) = '')
        """
    )
    fun countMissingStockTakeSection(): Long

    @Query(
        """
        SELECT w FROM Warehouse w
        WHERE w.deleted = false
        AND (w.stockTakeSection IS NULL OR TRIM(w.stockTakeSection) = '')
        """
    )
    fun findMissingStockTakeSection(pageable: Pageable): Page<Warehouse>
}