package com.alttd.altitudeweb.controllers.chat;

import com.alttd.altitudeweb.database.chat.ChatLogDao;
import com.alttd.altitudeweb.model.ChatMessageDto;
import lombok.experimental.UtilityClass;

import java.time.Instant;
import java.util.UUID;

@UtilityClass
public class ChatMessageMapper {

    public ChatMessage fromDto(ChatMessageDto dto) {
        return ChatMessage.builder()
                .uuid(dto.getUuid())
                .timestamp(dto.getTimestamp().toInstant())//TODO [Stijn] [2026-07-18]: Check this works, might be sending instant instead
                .server(dto.getServer())
                .messageJson(dto.getMessage())
                .notBlocked(!dto.getBlocked())
                .build();
    }

    public ChatMessage fromDao(ChatLogDao dao) {
        return ChatMessage.builder()
                .uuid(UUID.fromString(dao.getUuid()))
                .timestamp(Instant.ofEpochMilli(dao.getTimeStamp()))
                .server(dao.getServer())
                .messageJson(dao.getMiniMessage())
                .notBlocked(!dao.isBlocked())
                .build();
    }

    public static String toJson(ChatMessage chatMessage) {
        return "{\"uuid\":\"" + chatMessage.getUuid() + "\",\"timestamp\":\"" + chatMessage.getTimestamp() + "\",\"server\":\"" + chatMessage.getServer() + "\",\"messageJson\":\"" + chatMessage.getMessageJson() + "\",\"notBlocked\":\"" + chatMessage.isNotBlocked() + "\"}";
    }
}
