package com.viettel.campaign.service.impl;

import com.viettel.campaign.model.Campaign;
import com.viettel.campaign.repository.CampaignRepository;
import com.viettel.campaign.service.CampaignService;
import com.viettel.campaign.web.dto.CampaignDTO;
import com.viettel.campaign.web.dto.ResultDTO;
import com.viettel.campaign.web.dto.request_dto.CampaignRequestDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

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

@Service
public class CampaignServiceImpl implements CampaignService {

    @Autowired
    CampaignRepository campaignRepository;

    @Override
    public Map searchCampaignExecute(int page, int pageSize, String sort, String agentId) {
        Map result = new HashMap();

        Pageable pageable = PageRequest.of(page, pageSize, Sort.by(sort));
        List<CampaignDTO> lst = campaignRepository.searchCampaignExecute(agentId, pageable);
        List<CampaignDTO> count = campaignRepository.searchCampaignExecute(agentId, null);

        result.put("totalItem", count.size());
        result.put("data", lst);

        return result;
    }

    @Override
    public ResultDTO search(CampaignRequestDTO requestDto) {
        return campaignRepository.search(requestDto);
    }

    @Override
    public ResultDTO findByCampaignCode(CampaignRequestDTO requestDTO) {
        return campaignRepository.findByCampaignCode(requestDTO);
    }
}
