package com.hepl.tunefortwo.repository;

import java.util.List;
import java.util.Optional;

import org.springframework.data.mongodb.repository.Aggregation;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.hepl.tunefortwo.entity.MasterPayment;

@Repository
public interface MasterPaymentRepository extends MongoRepository<MasterPayment, String> {
	@Query("{ 'orderId': ?0 }")
	List<MasterPayment> findByOrderId(String orderId);
	
	 @Query("{ 'userName': ?0 }")
	 List<MasterPayment> findByUserName(String userName);
	 
	 
	 @Query("{ 'userName': { $regex: ?0, $options: 'i' } }")
	 List<MasterPayment> findByUserNamePartialMatch(String userName);
	 
	 @Query("{ 'formId': { $regex: ?0, $options: 'i' } }")
	    List<MasterPayment> findByFormIdPartialMatch(String formId);
	 
	 @Query(value = "{ 'orderId': ?0, 'payments': { $elemMatch: { 'status': ?1 } } }", fields = "{ 'payments.$': 1 }")
	 List<MasterPayment> findByPaymentsStatus(String orderId, String status);
	 
	// Method to find payments where the status is either "captured" or "failed"
	    @Query("{ 'payments.status': { $in: ['captured', 'failed'] } }")
	    List<MasterPayment> findByPaymentStatusCapturedOrFailed();
	 
	    @Query("{ 'payments.status': 'failed' }")
	    List<MasterPayment> findByPaymentStatus();
	    
	    @Query("{ 'payments.paymentId': ?0 }")
	    Optional<MasterPayment> findByPaymentId(String paymentId);
}
