package com.bio.bio_backend.domain.base.authgroup.service;

import com.bio.bio_backend.domain.base.authgroup.dto.AuthGroupDto;
import com.bio.bio_backend.domain.base.authgroup.entity.AuthGroup;
import com.bio.bio_backend.domain.base.authgroup.repository.AuthGroupRepository;
import com.bio.bio_backend.domain.base.authgroup.mapper.AuthGroupMapper;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;

import java.util.List;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@Slf4j
@Transactional(readOnly = true)
public class AuthGroupServiceImpl implements AuthGroupService {

    private final AuthGroupRepository repository;
    private final AuthGroupMapper mapper;

    @Override
    @Transactional
    public AuthGroupDto create(AuthGroupDto dto) {
        AuthGroup entity = mapper.toEntity(dto);
        return mapper.toDto(repository.save(entity));
    }

    @Override
    public AuthGroupDto getById(Long id) {
        return repository.findById(id).map(mapper::toDto).orElse(null);
    }

    @Override
    public List<AuthGroupDto> getAll() {
        return repository.findAll().stream().map(mapper::toDto).collect(Collectors.toList());
    }

    @Override
    @Transactional
    public AuthGroupDto update(Long id, AuthGroupDto dto) {
        AuthGroup entity = repository.findById(id).orElseThrow();
        // TODO: update fields
        return mapper.toDto(repository.save(entity));
    }

    @Override
    @Transactional
    public void delete(Long id) {
        repository.deleteById(id);
    }
}
