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

import com.pablotj.ai.chat.application.dto.ConversationDto;
import com.pablotj.ai.chat.application.dto.ConversationMessageDto;
import com.pablotj.ai.chat.application.dto.ConversationSummaryDto;
import com.pablotj.ai.chat.domain.model.Conversation;
import com.pablotj.ai.chat.domain.model.ConversationMessage;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;

/**
 * MapStruct mapper for converting between domain models and DTOs.
 */
@Mapper(componentModel = "spring")
public interface ConversationMapper {

    @Mapping(source = "conversationId.uuid", target = "conversationId")
    @Mapping(source = "summary.title", target = "title")
    @Mapping(source = "summary.description", target = "description")
    @Mapping(source = "status", target = "status", qualifiedByName = "statusToString")
    @Mapping(source = "messages", target = "messages")
    @Mapping(expression = "java(conversation.getMessageCount())", target = "messageCount")
    ConversationDto toDto(Conversation conversation);

    @Mapping(source = "conversationId.uuid", target = "conversationId")
    @Mapping(source = "summary.title", target = "title")
    @Mapping(source = "summary.description", target = "description")
    @Mapping(source = "status", target = "status", qualifiedByName = "statusToString")
    @Mapping(expression = "java(conversation.getMessageCount())", target = "messageCount")
    ConversationSummaryDto toSummaryDto(Conversation conversation);

    @Mapping(source = "messageId.uuid", target = "messageId")
    @Mapping(source = "conversationId.uuid", target = "conversationId")
    @Mapping(source = "role", target = "role", qualifiedByName = "roleToString")
    @Mapping(source = "content.text", target = "content")
    @Mapping(source = "metadata.allProperties", target = "metadata")
    ConversationMessageDto toMessageDto(ConversationMessage message);

    @Named("statusToString")
    default String statusToString(com.pablotj.ai.chat.domain.model.ConversationStatus status) {
        return status != null ? status.name() : null;
    }

    @Named("roleToString")
    default String roleToString(com.pablotj.ai.chat.domain.model.MessageRole role) {
        return role != null ? role.name() : null;
    }
}