package com.viettel.campaign.service.impl;

import com.viettel.campaign.model.ccms_full.Scenario;
import com.viettel.campaign.model.ccms_full.ScenarioQuestion;
import com.viettel.campaign.repository.ccms_full.ScenarioQuestionRepository;
import com.viettel.campaign.service.ScenarioQuestionService;
import com.viettel.campaign.utils.Constants;
import com.viettel.campaign.web.dto.ResultDTO;
import com.viettel.campaign.web.dto.ScenarioQuestionDTO;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author anhvd_itsol
 */

@Service
public class ScenarioQuestionServiceImpl implements ScenarioQuestionService {
    private static final Logger logger = LogManager.getLogger(ScenarioQuestionServiceImpl.class);

    @Autowired
    ScenarioQuestionRepository scenarioQuestionRepository;

    @Autowired
    ModelMapper modelMapper;

    @Override
    public ResultDTO findByScenarioIdAndCampaignIdAndCompanySiteId(Long scenarioId, Long campaignId, Long companySiteId) {
        ResultDTO resultDTO = new ResultDTO();
        try {
            List<ScenarioQuestion> lst = scenarioQuestionRepository.findScenarioQuestionsByScenarioIdAndCampaignIdAndCompanySiteIdAndStatus(scenarioId, campaignId, companySiteId, (short)1);
            resultDTO.setData(lst);
            resultDTO.setErrorCode(Constants.ApiErrorCode.SUCCESS);
            resultDTO.setDescription(Constants.ApiErrorDesc.SUCCESS);
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            resultDTO.setErrorCode(Constants.ApiErrorCode.ERROR);
            resultDTO.setDescription(Constants.ApiErrorDesc.ERROR);
        }
        return resultDTO;
    }

    @Override
    public ResultDTO add(ScenarioQuestionDTO scenarioQuestionDTO) {
        ResultDTO resultDTO = new ResultDTO();
        try {
            ScenarioQuestion scenarioQuestion = modelMapper.map(scenarioQuestionDTO, ScenarioQuestion.class);
            scenarioQuestionRepository.save(scenarioQuestion);
            resultDTO.setData(scenarioQuestion);
            resultDTO.setErrorCode(Constants.ApiErrorCode.SUCCESS);
            resultDTO.setDescription(Constants.ApiErrorDesc.SUCCESS);
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            resultDTO.setErrorCode(Constants.ApiErrorCode.ERROR);
            resultDTO.setDescription(Constants.ApiErrorDesc.ERROR);
        }
        return null;
    }
}
