package io.reflectoring.coderadar.graph.query.repository;

import io.reflectoring.coderadar.analyzer.domain.Commit;
import io.reflectoring.coderadar.graph.analyzer.domain.CommitEntity;
import java.util.List;
import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface GetCommitsInProjectRepository extends Neo4jRepository<CommitEntity, Long> {

  @Query(
      "MATCH (p:ProjectEntity)-[:CONTAINS]->(f:FileEntity)-[:CHANGED_IN]->(c:CommitEntity) WHERE ID(p) = {0} RETURN c "
          + "UNION MATCH (p:ProjectEntity)-[:CONTAINS]->(m:ModuleEntity)-[:CONTAINS]->(f:FileEntity)-[:CHANGED_IN]->(c:CommitEntity) WHERE ID(p) = {0} RETURN c")
  List<CommitEntity> findByProjectId(Long projectId);

  @Query(
    value =
        "MATCH (p:Project)-[:HAS]->(c:Commit) WHERE ID(p) = {0} RETURN c ORDER BY c.sequenceNumber DESC LIMIT 1"
  )
  Commit findTop1ByProjectIdOrderBySequenceNumberDesc(Long id);
}

@@ -489,23 +489,16 @@
  @Query(
-%0A      
 %22MATCH (
@@ -490,32 +490,33 @@
 @Query(%22MATCH (p
+1
 :ProjectEntity)-
@@ -517,37 +517,38 @@
 tity)-%5B:CONTAINS
+*
 %5D-
-%3E
 (f
+1
 :FileEntity)-%5B:C
@@ -553,32 +553,33 @@
 :CHANGED_IN%5D-%3E(c
+1
 :CommitEntity) W
@@ -581,36 +581,11 @@
 ty) 
-WHERE ID(p) = %7B0%7D RETURN c %22
+%22 +
 %0A   
@@ -595,24 +595,25 @@
     
-+ %22UN
+%22OPT
 ION
+AL
  MATCH (
 p:Pr
@@ -612,100 +612,44 @@
 CH (
-p:ProjectEntity)-%5B:CONTAINS%5D-%3E(m:ModuleEntity)-%5B:CONTAINS%5D-%3E(f:FileEntity)-%5B:CHANGED_IN
+c2:CommitEntity)-%5B:IS_CHILD_OF
 %5D-%3E(c
+1
 :Com
@@ -659,16 +659,31 @@
 Entity) 
+%22 +%0A          %22
 WHERE ID
@@ -680,32 +680,33 @@
  %22WHERE ID(p
+1
 ) = %7B0%7D 
 RETURN c%22)%0A 
@@ -693,24 +693,125 @@
 ) = %7B0%7D 
-RETURN c
+%22 +%0A          %22UNWIND %5Bc1, c2%5D AS c %22 +%0A          %22RETURN DISTINCT c %22 +%0A          %22ORDER BY c.timestamp DESC
 %22)%0A  Lis

Update the query for finding all commits belonging to a project