package es.uv.dagarcos.master.service;

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

import es.uv.dagarcos.master.dto.MasterEventDto;
import es.uv.dagarcos.master.repository.MasterEventRepository;

@Service
public class EventDashboardService {

    @Autowired
    private MasterEventRepository eventRepository;

    public Page<MasterEventDto> getEvents(String shard, String player, String action, Pageable pageable) {

        String shardFilter = shard.isBlank() ? null : shard;
        String playerFilter = player.isBlank() ? null : player;
        String actionFilter = action.isBlank() ? null : action;

        return eventRepository.findFiltered(shardFilter, playerFilter, actionFilter, pageable)
                              .map(e -> new MasterEventDto(
                                    e.getTimestamp(),
                                    e.getShard().getExternalId(),
                                    e.getPlayer().getExternalPlayerId(),
                                    e.getAction(),
                                    e.getPointsDelta()
                              ));
    }
}