package com.pablotj.ai.chat.application.dto;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import java.time.Instant;
import java.util.Map;

/**
 * Data Transfer Object for conversation messages.
 */
@Schema(description = "A message within a conversation")
public record ConversationMessageDto(

        @Schema(description = "Unique message identifier", example = "msg-123e4567-e89b-12d3-a456-426614174000")
        @NotBlank
        String messageId,

        @Schema(description = "Conversation identifier this message belongs to", example = "123e4567-e89b-12d3-a456-426614174000")
        @NotBlank
        String conversationId,

        @Schema(description = "Message role", example = "USER", allowableValues = {"USER", "ASSISTANT", "SYSTEM"})
        @NotBlank
        String role,

        @Schema(description = "Message content text", example = "Hello, how can you help me today?")
        @NotBlank
        String content,

        @Schema(description = "Message creation timestamp")
        @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy HH:mm:ss", timezone = "Europe/Madrid")
        @NotNull
        Instant createdAt,

        @Schema(description = "Additional message metadata")
        Map<String, Object> metadata
) {

    public ConversationMessageDto {
        if (metadata == null) {
            metadata = Map.of();
        }
    }

    public boolean isFromUser() {
        return "USER".equals(role);
    }

    public boolean isFromAssistant() {
        return "ASSISTANT".equals(role);
    }

    public boolean hasMetadata() {
        return !metadata.isEmpty();
    }
}
