package mapper

import (
	"campaign-pool-be/app/database/entity"
	"campaign-pool-be/app/module/chat/response"
)

// Chat Session Mapper
func ChatSessionResponseMapper(chatSession *entity.ChatSessions) *response.ChatSessionResponse {
	result := &response.ChatSessionResponse{
		ID:        chatSession.ID,
		Name:      chatSession.Name,
		Type:      chatSession.Type,
		CreatedBy: chatSession.CreatedBy,
		CreatedAt: chatSession.CreatedAt,
		UpdatedAt: chatSession.UpdatedAt,
	}

	if chatSession.Creator != nil {
		result.Creator = &response.UserBasicInfo{
			ID:       chatSession.Creator.ID,
			Username: chatSession.Creator.Username,
			Fullname: chatSession.Creator.Fullname,
			Email:    chatSession.Creator.Email,
		}
	}

	// Map participants
	if len(chatSession.Participants) > 0 {
		for _, participant := range chatSession.Participants {
			if participant.IsActive {
				result.Participants = append(result.Participants, ChatParticipantResponseMapper(participant))
			}
		}
	}

	// Map last message
	if len(chatSession.Messages) > 0 {
		// Find the latest message that is not deleted
		var lastMessage *entity.ChatMessages
		for _, message := range chatSession.Messages {
			if !message.IsDeleted && (lastMessage == nil || message.CreatedAt.After(lastMessage.CreatedAt)) {
				lastMessage = message
			}
		}
		if lastMessage != nil {
			result.LastMessage = ChatMessageResponseMapper(lastMessage)
		}
	}

	return result
}

// Chat Message Mapper
func ChatMessageResponseMapper(chatMessage *entity.ChatMessages) *response.ChatMessageResponse {
	result := &response.ChatMessageResponse{
		ID:            chatMessage.ID,
		ChatSessionID: chatMessage.ChatSessionID,
		SenderID:      chatMessage.SenderID,
		Message:       chatMessage.Message,
		MessageType:   chatMessage.MessageType,
		IsEdited:      chatMessage.IsEdited,
		EditedAt:      chatMessage.EditedAt,
		IsDeleted:     chatMessage.IsDeleted,
		DeletedAt:     chatMessage.DeletedAt,
		CreatedAt:     chatMessage.CreatedAt,
		UpdatedAt:     chatMessage.UpdatedAt,
	}

	if chatMessage.Sender != nil {
		result.Sender = &response.UserBasicInfo{
			ID:       chatMessage.Sender.ID,
			Username: chatMessage.Sender.Username,
			Fullname: chatMessage.Sender.Fullname,
			Email:    chatMessage.Sender.Email,
		}
	}

	return result
}

// Chat Participant Mapper
func ChatParticipantResponseMapper(chatParticipant *entity.ChatParticipants) *response.ChatParticipantResponse {
	result := &response.ChatParticipantResponse{
		ID:            chatParticipant.ID,
		ChatSessionID: chatParticipant.ChatSessionID,
		UserID:        chatParticipant.UserID,
		JoinedAt:      chatParticipant.JoinedAt,
		LeftAt:        chatParticipant.LeftAt,
		IsActive:      chatParticipant.IsActive,
		CreatedAt:     chatParticipant.CreatedAt,
		UpdatedAt:     chatParticipant.UpdatedAt,
	}

	if chatParticipant.User != nil {
		result.User = &response.UserBasicInfo{
			ID:       chatParticipant.User.ID,
			Username: chatParticipant.User.Username,
			Fullname: chatParticipant.User.Fullname,
			Email:    chatParticipant.User.Email,
		}
	}

	return result
}
