package com.alttd.altitudeweb.services.chat;

import com.alttd.altitudeweb.database.Databases;
import com.alttd.altitudeweb.database.chat.NicknameMapper;
import com.alttd.altitudeweb.database.chat.PartyMapper;
import com.alttd.altitudeweb.database.luckperms.UUIDUsernameMapper;
import com.alttd.altitudeweb.model.PlayerNameDto;
import com.alttd.altitudeweb.setup.Connection;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Optional;
import java.util.UUID;

@Slf4j
@Service
@RequiredArgsConstructor
public class ChatInfoService {

    public Optional<PlayerNameDto> getPlayerName(UUID uuid) {
        String username = Connection.getConnection(Databases.LUCK_PERMS)
                .runQueryWithResult(sqlSession -> sqlSession.getMapper(UUIDUsernameMapper.class).getUsernameFromUUID(uuid.toString()));

        if (username == null) {
            return Optional.empty();
        }

        Optional<String> nickname = Connection.getConnection(Databases.CHAT)
                .runQueryWithResult(sqlSession -> sqlSession.getMapper(NicknameMapper.class).getNicknameFromUUID(uuid.toString()));

        PlayerNameDto playerNameDto = new PlayerNameDto();
        playerNameDto.setName(username);
        playerNameDto.setNickname(nickname.orElse(null));

        return Optional.of(playerNameDto);
    }

    public Optional<String> getPartyName(int id) {
        return Connection.getConnection(Databases.CHAT)
                .runQueryWithResult(sqlSession -> sqlSession.getMapper(PartyMapper.class).getPartyName(id));
    }

}
