package com.ffii.fpsms.modules.stock.entity

import com.ffii.core.support.AbstractRepository
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.data.jpa.repository.Query
import org.springframework.data.repository.query.Param
import org.springframework.stereotype.Repository
import java.time.LocalDateTime
@Repository
interface StockLedgerRepository: AbstractRepository<StockLedger, Long> {
    
    @Query("""
        SELECT sl FROM StockLedger sl
        LEFT JOIN sl.stockInLine sil
        LEFT JOIN sl.stockOutLine sol
        LEFT JOIN sl.inventory inv
        LEFT JOIN inv.item i
        WHERE sl.deleted = false
          AND (:itemCode IS NULL OR sl.itemCode LIKE CONCAT('%', :itemCode, '%'))
          AND (:itemName IS NULL OR i.name LIKE CONCAT('%', :itemName, '%'))
          AND (:type IS NULL OR sl.type = :type)
          AND (:startDateTime IS NULL OR sl.created >= :startDateTime)
          AND (:endDateExclusive IS NULL OR sl.created < :endDateExclusive)
    """)
    fun findStockTransactions(
        @Param("itemCode") itemCode: String?,
        @Param("itemName") itemName: String?,
        @Param("type") type: String?,
        @Param("startDateTime") startDateTime: LocalDateTime?,
        @Param("endDateExclusive") endDateExclusive: LocalDateTime?,
        pageable: Pageable
    ): Page<StockLedger>
    
    @Query("""
        SELECT COUNT(sl) FROM StockLedger sl
        LEFT JOIN sl.inventory inv
        LEFT JOIN inv.item i
        WHERE sl.deleted = false
          AND (:itemCode IS NULL OR sl.itemCode LIKE CONCAT('%', :itemCode, '%'))
          AND (:itemName IS NULL OR i.name LIKE CONCAT('%', :itemName, '%'))
          AND (:type IS NULL OR sl.type = :type)
          AND (:startDateTime IS NULL OR sl.created >= :startDateTime)
          AND (:endDateExclusive IS NULL OR sl.created < :endDateExclusive)
    """)
    fun countStockTransactions(
        @Param("itemCode") itemCode: String?,
        @Param("itemName") itemName: String?,
        @Param("type") type: String?,
        @Param("startDateTime") startDateTime: LocalDateTime?,
        @Param("endDateExclusive") endDateExclusive: LocalDateTime?
    ): Long


    @Query("""
    SELECT sl FROM StockLedger sl
    WHERE sl.itemId = :itemId AND sl.deleted = false
    ORDER BY sl.date DESC, sl.id DESC
""")
fun findLatestByItemId(@Param("itemId") itemId: Long): List<StockLedger>
fun findFirstByItemIdAndDeletedFalseOrderByDateDescIdDesc(itemId: Long): StockLedger?
}