package research.loghunter.service;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import research.loghunter.dto.ServerLogPathDto;
import research.loghunter.entity.Server;
import research.loghunter.entity.ServerLogPath;
import research.loghunter.repository.ServerLogPathRepository;
import research.loghunter.repository.ServerRepository;

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

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

    private final ServerLogPathRepository logPathRepository;
    private final ServerRepository serverRepository;

    public List<ServerLogPathDto> findByServerId(Long serverId) {
        return logPathRepository.findByServerId(serverId).stream()
                .map(this::toDto)
                .collect(Collectors.toList());
    }

    public List<ServerLogPathDto> findActiveByServerId(Long serverId) {
        return logPathRepository.findByServerIdAndActiveTrue(serverId).stream()
                .map(this::toDto)
                .collect(Collectors.toList());
    }

    public ServerLogPathDto findById(Long id) {
        return logPathRepository.findById(id)
                .map(this::toDto)
                .orElseThrow(() -> new RuntimeException("LogPath not found: " + id));
    }

    @Transactional
    public ServerLogPathDto create(ServerLogPathDto dto) {
        Server server = serverRepository.findById(dto.getServerId())
                .orElseThrow(() -> new RuntimeException("Server not found: " + dto.getServerId()));

        ServerLogPath logPath = ServerLogPath.builder()
                .server(server)
                .path(dto.getPath())
                .filePattern(dto.getFilePattern())
                .description(dto.getDescription())
                .active(dto.getActive() != null ? dto.getActive() : true)
                .build();

        logPath = logPathRepository.save(logPath);
        return toDto(logPath);
    }

    @Transactional
    public ServerLogPathDto update(Long id, ServerLogPathDto dto) {
        ServerLogPath logPath = logPathRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("LogPath not found: " + id));

        logPath.setPath(dto.getPath());
        logPath.setFilePattern(dto.getFilePattern());
        logPath.setDescription(dto.getDescription());
        logPath.setActive(dto.getActive());

        return toDto(logPathRepository.save(logPath));
    }

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

    private ServerLogPathDto toDto(ServerLogPath logPath) {
        return ServerLogPathDto.builder()
                .id(logPath.getId())
                .serverId(logPath.getServer().getId())
                .path(logPath.getPath())
                .filePattern(logPath.getFilePattern())
                .description(logPath.getDescription())
                .active(logPath.getActive())
                .build();
    }
}
