package com.arms.egovframework.javaservice.esframework.repository.common;

import com.arms.api.issue.almapi.model.entity.AlmIssueEntity;
import com.arms.egovframework.javaservice.esframework.esquery.SimpleQuery;
import com.arms.egovframework.javaservice.esframework.esquery.filter.RangeQueryFilter;
import com.arms.egovframework.javaservice.esframework.model.dto.esquery.CompositeGroupDTO;
import com.arms.egovframework.javaservice.esframework.model.dto.esquery.CompositeSourceDTO;
import com.arms.egovframework.javaservice.esframework.model.dto.esquery.DocFiledType;
import com.arms.egovframework.javaservice.esframework.model.dto.esquery.SubGroupFieldDTO;
import com.arms.egovframework.javaservice.esframework.model.dto.request.AggregationRequestDTO;
import com.arms.egovframework.javaservice.esframework.model.dto.request.CompositeRequestDTO;
import com.arms.egovframework.javaservice.esframework.model.vo.CompositeAggregationResultWrapper;
import com.arms.egovframework.javaservice.esframework.model.vo.DocumentAggregations;
import com.arms.egovframework.javaservice.esframework.model.vo.DocumentBucket;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;
import java.util.Map;

@SpringBootTest(properties = {
        "spring.profiles.active=dev"
})
public class CompositeAggregationQueryFactoryTest {

    @Autowired
    EsCommonRepositoryWrapper<AlmIssueEntity> esCommonRepositoryWrapper;

    @Test
    @DisplayName("Composite 집계 쿼리 조회 테스트")
    void testCompositeAggregation() {
        // given
        CompositeRequestDTO requestDTO = CompositeRequestDTO.builder()
                .addSource(
                        CompositeSourceDTO.builder()
                                .field("updated")
                                .fieldAlias("monthly")
                                .isAscending(false)
                                .field(DocFiledType.HistogramMonth)
                                .build(),
                        CompositeSourceDTO.builder()
                                .field("jira_server_id")
                                .fieldAlias("serverId")
                                .isAscending(false)
                                .build(),
                        CompositeSourceDTO.builder()
                                .field("project.project_key.keyword")
                                .fieldAlias("project")
                                .isAscending(false)
                                .build(),
                        CompositeSourceDTO.builder()
                                .field("issuetype.issuetype_id.keyword")
                                .fieldAlias("issuetypeId")
                                .isAscending(false)
                                .build(),
                        CompositeSourceDTO.builder()
                                .field("status.status_id.keyword")
                                .fieldAlias("statusId")
                                .isAscending(false)
                                .build()
                )
                .build();

        SimpleQuery<CompositeGroupDTO> simpleQuery = SimpleQuery.composite(requestDTO)
                .andTermQueryFilter("linkedIssuePdServiceIds", 29L)
                .andTermsQueryFilter("linkedIssuePdServiceVersions", List.of( 91L,92L,93L,94L,99L))
                .andRangeQueryFilter(RangeQueryFilter.of("updated")
                        .betweenDate("2026-01-01", null))
                .andTermsQueryFilter("assignee.assignee_accountId.keyword", List.of("root"));

        // when
        DocumentAggregations documentAggregations = esCommonRepositoryWrapper.compositeAggregationRecent(simpleQuery);

        // then
        System.out.println(documentAggregations);
    }

    @Test
    @DisplayName("Composite 집계 쿼리 페이지네이션 테스트")
    void testCompositeAggregationWithAfterKey() {
        // given
        CompositeRequestDTO requestDTO = CompositeRequestDTO.builder()
                .addSource(
                        CompositeSourceDTO.builder()
                                .fieldAlias("update_date")
                                .field("updated")
                                .isAscending(false)
                                .build(),
                        CompositeSourceDTO.builder()
                                .fieldAlias("recent_id")
                                .field("recent_id")
                                .isAscending(false)
                                .build()
                )
                .afterKey(Map.of(
                        "update_date", "1761226751860",
                        "recent_id", "936493109998294575_ARMSTEST_ARMSTEST-91"
                ))
                .hitSize(1)
                .sortField("updated")
                .sortAscending(false)
                .includeFields("key")
                .build();

        SimpleQuery<CompositeGroupDTO> simpleQuery = SimpleQuery.composite(requestDTO)
                .andTermQueryFilter("recent", true);

        // when
        CompositeAggregationResultWrapper<AlmIssueEntity> result = esCommonRepositoryWrapper.compositeHits(simpleQuery);

        // then
        System.out.println("toHitDocs: " + result.toHitDocs());
        System.out.println("toDocs: " + result.toDocs());
        System.out.println("AfterKey: " + result.getAfterKey());
    }

    @Test
    void test(){
        DocumentAggregations documentAggregations = esCommonRepositoryWrapper.aggregate(
                SimpleQuery.aggregation(
                        AggregationRequestDTO.builder()
                                .mainField("assignee.assignee_emailAddress.keyword")
                                .mainFieldAlias("emailAddress")
                                .addGroup(
                                        SubGroupFieldDTO.builder()
                                                .subFieldAlias("jiraServerId")
                                                .subField("jira_server_id")
                                                .build()
                                        ,
                                        SubGroupFieldDTO.builder()
                                                .subFieldAlias("displayName")
                                                .subField("assignee.assignee_displayName.keyword")
                                                .build()
                                        ,
                                        SubGroupFieldDTO.builder()
                                                .subFieldAlias("accountId")
                                                .subField("assignee.assignee_accountId.keyword")
                                                .build()
                                )
                                .build()
                ).andExistsQueryFilter("assignee")
        );

        System.out.println(documentAggregations);
    }
}
