package org.brapi.test.BrAPITestServer.repository.germ;

import org.brapi.test.BrAPITestServer.model.entity.germ.SeedLotTransactionEntity;
import org.brapi.test.BrAPITestServer.repository.BrAPIRepository;
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;

public interface SeedLotTransactionRepository extends BrAPIRepository<SeedLotTransactionEntity, String> {
	@Query(value = "SELECT t FROM SeedLotTransactionEntity t "
			+ "WHERE (:transactionDbId IS NULL OR t.id = :transactionDbId) "
			+ "AND ((:direction = 'IN' AND t.toSeedLot.id = :seedLotDbId) "
			+ "OR (:direction = 'OUT' AND t.fromSeedLot.id = :seedLotDbId) "
			+ "OR (:direction = 'BOTH' AND (t.toSeedLot.id = :seedLotDbId OR t.fromSeedLot.id = :seedLotDbId)))",
			countQuery = "SELECT COUNT(t) FROM SeedLotTransactionEntity t "
					+ "WHERE (:transactionDbId IS NULL OR t.id = :transactionDbId) "
					+ "AND ((:direction = 'IN' AND t.toSeedLot.id = :seedLotDbId) "
					+ "OR (:direction = 'OUT' AND t.fromSeedLot.id = :seedLotDbId) "
					+ "OR (:direction = 'BOTH' AND (t.toSeedLot.id = :seedLotDbId OR t.fromSeedLot.id = :seedLotDbId)))")
	Page<SeedLotTransactionEntity> findBySeedLotDbId(@Param("seedLotDbId") String seedLotDbId,
			@Param("transactionDbId") String transactionDbId,
			@Param("direction") String direction,
			Pageable pageReq);

}
