// FPSMS-backend/src/main/java/com/ffii/fpsms/modules/stock/entity/PickExecutionIssueRepository.kt
package com.ffii.fpsms.modules.pickOrder.entity 

import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Modifying
import org.springframework.data.jpa.repository.Query
import org.springframework.data.repository.query.Param
import org.springframework.stereotype.Repository
import com.ffii.fpsms.modules.pickOrder.enums.PickExecutionIssueEnum
import java.time.LocalDate
import org.springframework.transaction.annotation.Transactional
@Repository
interface PickExecutionIssueRepository : JpaRepository<PickExecutionIssue, Long> {
    fun findByPickOrderIdAndDeletedFalse(pickOrderId: Long): List<PickExecutionIssue>
    @Query(
        """
        SELECT p FROM PickExecutionIssue p
        WHERE p.deleted = false
          AND p.pickOrderId IN :pickOrderIds
        """
    )
    fun findAllByPickOrderIdInAndDeletedFalse(@Param("pickOrderIds") pickOrderIds: List<Long>): List<PickExecutionIssue>
    fun findByPickOrderLineIdAndDeletedFalse(pickOrderLineId: Long): List<PickExecutionIssue>
    fun findByLotIdAndDeletedFalse(lotId: Long): List<PickExecutionIssue>
    fun findByPickOrderLineIdAndLotIdAndDeletedFalse(
        pickOrderLineId: Long, 
        lotId: Long
    ): List<PickExecutionIssue>
    @Query("""
    SELECT p.issueNo 
    FROM PickExecutionIssue p 
    WHERE p.issueNo LIKE CONCAT('SKO-', :yearMonth, '-%')
    AND p.deleted = false
    ORDER BY p.issueNo DESC
    LIMIT 1
""")
fun findLatestIssueNoByYearMonth(@Param("yearMonth") yearMonth: String): String?

fun findByDeletedFalse(): List<PickExecutionIssue>

// 只返回 Job Order 的 issues (joPickOrderId 不为空)
@Query("""
    SELECT p FROM PickExecutionIssue p 
    WHERE p.joPickOrderId IS NOT NULL 
    AND p.deleted = false
    ORDER BY p.created DESC
""")
fun findJoIssues(): List<PickExecutionIssue>

// 只返回 Delivery Order 的 issues (doPickOrderId 不为空)
@Query("""
    SELECT p FROM PickExecutionIssue p 
    WHERE p.doPickOrderId IS NOT NULL 
    AND p.deleted = false
    ORDER BY p.created DESC
""")
fun findDoIssues(): List<PickExecutionIssue>

// 只返回 Material 的 issues (两者都为空)
@Query("""
    SELECT p FROM PickExecutionIssue p 
    WHERE p.joPickOrderId IS NULL 
    AND p.doPickOrderId IS NULL 
    AND p.deleted = false
    ORDER BY p.created DESC
""")
fun findMaterialIssues(): List<PickExecutionIssue>

    @Query("""
    SELECT p FROM PickExecutionIssue p 
    WHERE p.badItemQty IS NOT NULL 
    AND p.badItemQty > 0
    AND p.deleted = false
    AND p.handleStatus IN (:statuses)
    ORDER BY p.created DESC
""")
    fun getBadItemList_statusIn(@Param("statuses") statuses: List<PickExecutionIssueEnum>): List<PickExecutionIssue>

    @Query("""
     SELECT p FROM PickExecutionIssue p
    WHERE (p.badItemQty IS NOT NULL AND p.badItemQty > 0)
       OR (p.missQty IS NOT NULL AND p.missQty > 0)
      AND p.deleted = false
    ORDER BY p.created DESC
""")
    fun getBadItemList(): List<PickExecutionIssue>
    fun findByPickOrderId(pickOrderId: Long): List<PickExecutionIssue>

    @Query("""
    SELECT p FROM PickExecutionIssue p
    WHERE p.issueCategory = :issueCategory
    AND p.missQty > 0
    AND p.deleted = false
    ORDER BY p.created DESC
""")
fun findMissItemList(@Param("issueCategory") issueCategory: IssueCategory): List<PickExecutionIssue>

@Query("""
    SELECT p FROM PickExecutionIssue p
    WHERE p.issueCategory = :issueCategory
    AND p.badItemQty > 0
    AND p.deleted = false
    ORDER BY p.created DESC
""")
fun findBadItemListByCategory(@Param("issueCategory") issueCategory: IssueCategory): List<PickExecutionIssue>

@Query("""
    SELECT p FROM PickExecutionIssue p
    WHERE p.issueCategory = :issueCategory
    AND p.badItemQty > 0
    AND p.deleted = false
    ORDER BY p.created DESC
""")
fun findBadItemOnlyList(@Param("issueCategory") issueCategory: IssueCategory): List<PickExecutionIssue>

@Modifying
@Transactional
@Query("""
    UPDATE PickExecutionIssue p 
    SET p.handleStatus = :handleStatus, 
        p.handleDate = :handleDate, 
        p.handledBy = :handledBy
    WHERE p.id = :issueId
""")
fun updateHandleStatus(
    @Param("issueId") issueId: Long,
    @Param("handleStatus") handleStatus: HandleStatus,
    @Param("handleDate") handleDate: LocalDate,
    @Param("handledBy") handledBy: Long?
): Int
}


