package com.nebula.erp.purchase.repository;

import com.nebula.erp.purchase.model.DebitNote;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Optional;

@Repository
public interface DebitRepository extends JpaRepository<DebitNote, Long>, JpaSpecificationExecutor<DebitNote> {

    // Find credit note with sale id or sale return id
    @Query("SELECT d FROM DebitNote d WHERE (d.grn_id = :grn_id OR d.purchase_return_id =:purchase_return_id) AND d.tenant = :tenantName")
    List<DebitNote> findByPurchaseOrPurchaseReturn(@Param("grn_id") Long GrnId, @Param("purchase_return_id") Long PurchaseReturnId, @Param("tenantName") String tenantName);

    // Custom method to find by id and tenant
    @Query("SELECT d FROM DebitNote d WHERE d.id = :id AND d.tenant = :tenantName")
    Optional<DebitNote> findByIdAndTenant(@Param("id") Long id, @Param("tenantName") String tenantName);

}