package com.viettel.campaign.service.impl;

import com.viettel.campaign.model.ccms_full.ScenarioAnswer;
import com.viettel.campaign.repository.ccms_full.ScenarioAnswerRepository;
import com.viettel.campaign.service.ScenarioAnswerService;
import com.viettel.campaign.utils.Constants;
import com.viettel.campaign.web.dto.ResultDTO;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author anhvd_itsol
 */

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

    @Autowired
    ScenarioAnswerRepository scenarioAnswerRepository;

    @Override
    public ResultDTO findByScenarioQuestionCompany(Long scenarioQuestionId, Long companySiteId) {
        ResultDTO resultDTO = new ResultDTO();
        try {
            List<ScenarioAnswer> lst = scenarioAnswerRepository.findByScenarioQuestionIdAndCompanySiteIdAndStatus(scenarioQuestionId, 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 Long getMaxAnswerOrderId(Long scenarioQuestionId, Long companySiteId) {
        try {
            return scenarioAnswerRepository.getMaxAnswerOrderId(scenarioQuestionId, companySiteId);
        }catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
        }
        return null;
    }
}
