package io.delphiplatform.api.v3.rdb.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

import io.delphiplatform.api.util.model.ModelUtils;
import io.delphiplatform.api.v3.model.Region;
import io.delphiplatform.api.v3.rdb.entity.RegionEntity;
import io.delphiplatform.api.v3.rdb.repository.RegionRepository;

@Service
@Transactional(readOnly = true)
public class RegionService extends EntityService<RegionEntity> {

    private final RegionRepository regionRepository;

    @Autowired
    public RegionService(RegionRepository regionRepository) {
        super(regionRepository);
        this.regionRepository = regionRepository;
    }

    public Region findOne(String regionId) {
        return regionRepository.findById(regionId).map(Region::from).orElseThrow(NOT_FOUND);
    }

    public List<Region> find(Pageable pageable) {
        return ModelUtils.map(findEntities(pageable), Region::from);
    }

    public List<Region> findAllById(List<String> ids) {
        return regionRepository.findAllById(ids).stream()
            .map(Region::from)
            .collect(Collectors.toList());
    }
}
