package com.nebula.erp.sales.repository;

import com.nebula.erp.sales.model.CreditNote;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
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 CreditRepository extends JpaRepository<CreditNote, Long>, JpaSpecificationExecutor<CreditNote> {

    // Find credit note with sale id or sale return id
    @Query("SELECT s FROM CreditNote s WHERE s.sales_return_id =:sales_return_id AND s.tenant = :tenantName")
    List<CreditNote> findBySaleReturn(@Param("sales_return_id") Long SalesReturnId, @Param("tenantName") String tenantName);

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

}