package br.gov.sigefp.rh.service;

import br.gov.sigefp.rh.api.dto.CareerRegimeDTO;
import br.gov.sigefp.rh.domain.CareerRegime;
import br.gov.sigefp.rh.repository.CareerRegimeRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class CareerRegimeService {

    private final CareerRegimeRepository repository;

    public List<CareerRegimeDTO> findAll() {
        return repository.findAll().stream()
                .map(this::toDTO)
                .collect(Collectors.toList());
    }

    @Transactional
    public CareerRegimeDTO create(CareerRegimeDTO dto) {
        CareerRegime entity = CareerRegime.builder()
                .code(dto.getCode())
                .name(dto.getName())
                .description(dto.getDescription())
                .build();
        return toDTO(repository.save(entity));
    }

    @Transactional
    public CareerRegimeDTO update(java.util.UUID id, CareerRegimeDTO dto) {
        CareerRegime entity = repository.findById(id)
                .orElseThrow(() -> new RuntimeException("Regime não encontrado"));
        entity.setCode(dto.getCode());
        entity.setName(dto.getName());
        entity.setDescription(dto.getDescription());
        return toDTO(repository.save(entity));
    }

    @Transactional
    public void delete(java.util.UUID id) {
        repository.deleteById(id);
    }

    private CareerRegimeDTO toDTO(CareerRegime entity) {
        return CareerRegimeDTO.builder()
                .id(entity.getId())
                .code(entity.getCode())
                .name(entity.getName())
                .description(entity.getDescription())
                .build();
    }
}
