package vn.azteam.tpf.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vn.azteam.tpf.domain.Casbin;
import vn.azteam.tpf.repository.CasbinRepository;
import vn.azteam.tpf.repository.search.CasbinSearchRepository;
import vn.azteam.tpf.service.CasbinService;
import vn.azteam.tpf.service.dto.CasbinDTO;
import vn.azteam.tpf.service.mapper.CasbinMapper;


import java.util.Optional;

import static org.elasticsearch.index.query.QueryBuilders.queryStringQuery;

/**
 * Service Implementation for managing Casbin.
 */
@Service
@Transactional
public class CasbinServiceImpl implements CasbinService {

    private final Logger log = LoggerFactory.getLogger(CasbinServiceImpl.class);

    private final CasbinRepository casbinRepository;

    private final CasbinMapper casbinMapper;

    private final CasbinSearchRepository casbinSearchRepository;

    public CasbinServiceImpl(CasbinRepository casbinRepository, CasbinMapper casbinMapper, CasbinSearchRepository casbinSearchRepository) {
        this.casbinRepository = casbinRepository;
        this.casbinMapper = casbinMapper;
        this.casbinSearchRepository = casbinSearchRepository;
    }

    /**
     * Save a casbin.
     *
     * @param casbinDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public CasbinDTO save(CasbinDTO casbinDTO) {
        log.debug("Request to save Casbin : {}", casbinDTO);

        Casbin casbin = casbinMapper.toEntity(casbinDTO);
        casbin = casbinRepository.save(casbin);
        CasbinDTO result = casbinMapper.toDto(casbin);
        casbinRepository.save(casbin);
        return result;
    }

    /**
     * Get all the casbin.
     *
     * @param pageable the pagination information
     * @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<CasbinDTO> findAll(Pageable pageable) {
        log.debug("Request to get all Casbin");
        return casbinSearchRepository.findAll(pageable)
            .map(casbinMapper::toDto);
    }


    /**
     * Get one permission by id.
     *
     * @param id the id of the entity
     * @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public Optional<CasbinDTO> findOne(Long id) {
        log.debug("Request to get Permission : {}", id);
        return casbinRepository.findById(id)
            .map(casbinMapper::toDto);
    }

    /**
     * Delete the casbin by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete Casbin : {}", id);
        casbinRepository.deleteById(id);
        casbinSearchRepository.deleteById(id);
    }

    /**
     * Delete the casbin by role id.
     *
     * @param id the id of the role
     */
    @Override
    public void deleteByRoleId(Long id) {
        log.debug("Request to delete Casbin by roleId : {}", id);
        casbinRepository.deleteCasbinByTbRoleId(id);
        casbinSearchRepository.deleteByTbRoleId(id);
    }

    /**
     * Search for the casbin corresponding to the query.
     *
     * @param query the query of the search
     * @param pageable the pagination information
     * @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<CasbinDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of Casbin for query {}", query);
        return casbinSearchRepository.search(queryStringQuery(query), pageable)
            .map(casbinMapper::toDto);
    }
}
