package com.alttd.altitudeweb.mappers;

import com.alttd.altitudeweb.database.luckperms.Player;
import com.alttd.altitudeweb.database.proxyplaytime.StaffPt;
import com.alttd.altitudeweb.model.StaffPlaytimeDto;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.*;
import java.util.concurrent.TimeUnit;

@Service
public final class StaffPtToStaffPlaytimeMapper {
    private record PlaytimeInfo(long totalPlaytime, long lastPlayed) {}

    public List<StaffPlaytimeDto> map(List<StaffPt> sessions, List<Player> staffMembers, long from, long to) {
        Map<UUID, PlaytimeInfo> playtimeData = getUuidPlaytimeInfoMap(sessions, from, to);

        List<StaffPlaytimeDto> results = new ArrayList<>(playtimeData.size());
        for (Map.Entry<UUID, PlaytimeInfo> entry : playtimeData.entrySet()) {
            long lastPlayedMillis = entry.getValue().lastPlayed() == Long.MIN_VALUE ? 0L : entry.getValue().lastPlayed();
            StaffPlaytimeDto dto = new StaffPlaytimeDto();
            dto.setStaffMember(staffMembers.stream()
                                       .filter(player -> player.uuid().equals(entry.getKey()))
                                       .map(Player::username)
                                       .findFirst()
                                       .orElse(entry.getKey().toString())
                              );
            dto.setLastPlayed(OffsetDateTime.ofInstant(Instant.ofEpochMilli(lastPlayedMillis), ZoneOffset.UTC));
            dto.setPlaytime((int) TimeUnit.MILLISECONDS.toMinutes(entry.getValue().totalPlaytime()));
            results.add(dto);
        }
        return results;
    }

    private Map<UUID, PlaytimeInfo> getUuidPlaytimeInfoMap(List<StaffPt> sessions, long from, long to) {
        Map<UUID, PlaytimeInfo> playtimeData = new HashMap<>();
        for (StaffPt session : sessions) {
            long overlapStart = Math.max(session.sessionStart(), from);
            long overlapEnd = Math.min(session.sessionEnd(), to);
            if (overlapEnd <= overlapStart) {
                continue;
            }

            PlaytimeInfo info = playtimeData.getOrDefault(session.uuid(), new PlaytimeInfo(0L, Long.MIN_VALUE));
            long totalPlaytime = info.totalPlaytime() + (overlapEnd - overlapStart);
            long lastPlayed = Math.max(info.lastPlayed(), overlapEnd);
            playtimeData.put(session.uuid(), new PlaytimeInfo(totalPlaytime, lastPlayed));
        }
        return playtimeData;
    }
}


