package com.caliverse.admin.dynamodb.service;

import com.caliverse.admin.domain.datacomponent.MetaDataHandler;
import com.caliverse.admin.domain.entity.EFilterOperator;
import com.caliverse.admin.domain.entity.ITEMLARGETYPE;
import com.caliverse.admin.domain.entity.STATUSTYPE;
import com.caliverse.admin.domain.entity.metadata.MetaQuestData;
import com.caliverse.admin.domain.response.UsersResponse;
import com.caliverse.admin.dynamodb.domain.atrrib.ItemAttrib;
import com.caliverse.admin.dynamodb.domain.atrrib.QuestAttrib;
import com.caliverse.admin.dynamodb.domain.doc.ItemDoc;
import com.caliverse.admin.dynamodb.domain.doc.QuestDoc;
import com.caliverse.admin.dynamodb.dto.PageResult;
import com.caliverse.admin.dynamodb.entity.KeyParam;
import com.caliverse.admin.dynamodb.repository.QuestRepository;
import com.caliverse.admin.global.common.constants.DynamoDBConstants;
import com.caliverse.admin.global.common.utils.CommonUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;

import java.util.*;

@Slf4j
@Service
@RequiredArgsConstructor
public class DynamodbQuestService {
    private final QuestRepository questRepository;
    private final MetaDataHandler metaDataHandler;

    public List<UsersResponse.QuestInfo> getQuestItems(String guid){
        List<UsersResponse.QuestInfo> questInfoList = new ArrayList<>();

        Map<String, AttributeValue> pageKey = null;
        do {
            PageResult<QuestDoc> questList = questRepository.getQuestListWithPaging(guid, "", DynamoDBConstants.ATTRIB_QUEST, null,"",  pageKey, false);

            questList.getItems().forEach(quest -> {
                QuestAttrib attrib = quest.getAttribValue();

                Integer questId = attrib.getQuestId();
                Integer currentTaskNo = attrib.getCurrentTaskNum();
                List<MetaQuestData> metaQuests = metaDataHandler.getMetaQuestData(questId);

                List<UsersResponse.Quest> detailQuests = metaQuests.stream()
                        .map(metaData -> UsersResponse.Quest.builder()
                                .questId(metaData.getQuestId())
                                .taskNo(metaData.getTaskNum())
                                .questName(metaDataHandler.getTextStringData(metaData.getTaskName()))
                                .counter(metaData.getCounter())
                                .status(Objects.equals(currentTaskNo, metaData.getTaskNum()) && attrib.getIsComplete().equals(0) ? "RUNNING" : currentTaskNo > metaData.getTaskNum() || attrib.getIsComplete().equals(1) ? "COMPLETE" : "WAIT" )
                                .build())
                        .sorted(Comparator.comparing(UsersResponse.Quest::getTaskNo))
                        .toList();

                String taskName = metaQuests.stream()
                        .filter(attr -> attr.getTaskNum().equals(currentTaskNo))
                        .map(MetaQuestData::getTaskName)
                        .findFirst().orElse(null);

                UsersResponse.QuestInfo questInfo = UsersResponse.QuestInfo.builder()
                        .questKey(quest.getSK())
                        .questId(questId)
                        .questName(metaDataHandler.getTextStringData(taskName))
                        .status(attrib.getIsComplete())
                        .assignTime(attrib.getQuestAssignTime())
                        .type(attrib.getQuestType().name())
                        .startTime(attrib.getTaskStartTime())
                        .completeTime(attrib.getQuestCompleteTime())
                        .currentTaskNum(attrib.getCurrentTaskNum())
                        .detailQuest(detailQuests)
                        .build();

                questInfoList.add(questInfo);
            });

            pageKey = questList.getLastEvaluatedKey();
        } while (pageKey != null);

        return questInfoList;
    }

}
