package com.alttd.altitudeweb.controllers.chat;

import com.alttd.altitudeweb.api.ChatInfoApi;
import com.alttd.altitudeweb.model.PartyNameDto;
import com.alttd.altitudeweb.model.PlayerNameDto;
import com.alttd.altitudeweb.services.chat.ChatInfoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@Slf4j
@RestController
@RequiredArgsConstructor
public class ChatInfoController implements ChatInfoApi {

    private final ChatInfoService chatInfoService;

    @Override
    public ResponseEntity<PlayerNameDto> getName(UUID uuid) {
        return chatInfoService.getPlayerName(uuid)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    @Override
    public ResponseEntity<PartyNameDto> getPartyName(String id) {
        int partyId;
        try {
            partyId = Integer.parseInt(id);
        } catch (NumberFormatException e) {
            return ResponseEntity.badRequest().build();
        }
        return chatInfoService.getPartyName(partyId)
                .map(partyName -> {
                    PartyNameDto partyNameDto = new PartyNameDto();
                    partyNameDto.setName(partyName);
                    return partyNameDto;
                })
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

}
