package uk.ac.ebi.quickgo.ontology.common;

import uk.ac.ebi.quickgo.ontology.common.document.OntologyDocument;
import uk.ac.ebi.quickgo.ontology.common.document.OntologyFields;

import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.solr.repository.Query;
import org.springframework.data.solr.repository.SolrCrudRepository;

/**
 * Ontology repository interface exposing methods for performing searches over its contents.
 * <p>
 * See here how to change:
 * https://github.com/spring-projects/spring-data-solr/blob/master/README.md
 *
 * Created 11/11/15
 * @author Edd
 */
public interface OntologyRepository extends SolrCrudRepository<OntologyDocument, String> {
    String QUERY_ONTOLOGY_TYPE_AND_ID = OntologyFields.ONTOLOGY_TYPE + ":?0 AND " + OntologyFields.ID + ":(?1)";

    // complete
    @Query(QUERY_ONTOLOGY_TYPE_AND_ID) List<OntologyDocument> findCompleteByTermId(String idType, List<String> ids);

    // core
    @Query(value = QUERY_ONTOLOGY_TYPE_AND_ID,
            fields = {OntologyFields.ID, OntologyFields.NAME, OntologyFields.IS_OBSOLETE, OntologyFields.COMMENT,
                    OntologyFields.ASPECT, OntologyFields.ANCESTOR,
                    OntologyFields.USAGE, OntologyFields.SYNONYM, OntologyFields.DEFINITION,
                    OntologyFields.DEFINITION_XREFS})
    List<OntologyDocument> findCoreAttrByTermId(String idType, List<String> ids);

    // history
    @Query(value = QUERY_ONTOLOGY_TYPE_AND_ID,
            fields = {
                    OntologyFields.ID, OntologyFields.NAME, OntologyFields.IS_OBSOLETE, OntologyFields.COMMENT,
                    OntologyFields.DEFINITION, OntologyFields.HISTORY}) List<OntologyDocument> findHistoryByTermId(
            String idType, List<String> ids);

    // cross-references
    @Query(value = QUERY_ONTOLOGY_TYPE_AND_ID,
            fields = {
                    OntologyFields.ID, OntologyFields.NAME, OntologyFields.IS_OBSOLETE, OntologyFields.COMMENT,
                    OntologyFields.DEFINITION, OntologyFields.XREF}) List<OntologyDocument> findXRefsByTermId(
            String idType, List<String> ids);

    // taxonomy constraints and blacklist
    @Query(value = QUERY_ONTOLOGY_TYPE_AND_ID,
            fields = {
                    OntologyFields.ID, OntologyFields.NAME, OntologyFields.IS_OBSOLETE, OntologyFields.COMMENT,
                    OntologyFields.DEFINITION, OntologyFields.TAXON_CONSTRAINT, OntologyFields.BLACKLIST})
    List<OntologyDocument> findTaxonConstraintsByTermId(String idType, List<String> ids);

    // cross-ontology relations
    @Query(value = QUERY_ONTOLOGY_TYPE_AND_ID,
            fields = {
                    OntologyFields.ID, OntologyFields.NAME, OntologyFields.IS_OBSOLETE, OntologyFields.COMMENT,
                    OntologyFields.DEFINITION, OntologyFields.XRELATION})
    List<OntologyDocument> findXOntologyRelationsByTermId(String idType, List<String> ids);

    // annotation guidelines
    @Query(value = QUERY_ONTOLOGY_TYPE_AND_ID,
            fields = {
                    OntologyFields.ID, OntologyFields.NAME, OntologyFields.IS_OBSOLETE, OntologyFields.COMMENT,
                    OntologyFields.DEFINITION, OntologyFields.ANNOTATION_GUIDELINE})
    List<OntologyDocument> findAnnotationGuidelinesByTermId(String idType, List<String> ids);

    Page<OntologyDocument> findAllByOntologyType(String type, Pageable pageable);
}
@@ -834,12 +834,30 @@
 :?0 
+%22 +%0A            %22
 AND 
+(
 %22 + 
@@ -882,16 +882,61 @@
 + %22:(?1)
+ OR %22 + OntologyFields.SECONDARY_ID + %22:(?1))
 %22;%0A%0A    

Allow id searches to look in primary and secondary ids fields.