package repositories

import (
	"context"
	"fmt"
	"veza-backend-api/internal/models"

	"github.com/google/uuid"
	"gorm.io/gorm"
)

type ChatMessageRepository struct {
	db *gorm.DB
}

func NewChatMessageRepository(db *gorm.DB) *ChatMessageRepository {
	return &ChatMessageRepository{db: db}
}

func (r *ChatMessageRepository) GetConversationMessages(ctx context.Context, conversationID uuid.UUID, limit, offset int) ([]models.ChatMessage, error) {
	var messages []models.ChatMessage
	// Note: ChatMessage.ConversationID is mapped to column "room_id" in DB
	err := r.db.WithContext(ctx).
		Where("room_id = ? AND is_deleted = ?", conversationID, false).
		Order("created_at DESC").
		Limit(limit).
		Offset(offset).
		Find(&messages).Error
	if err != nil {
		return nil, fmt.Errorf("failed to get conversation messages: %w", err)
	}
	return messages, nil
}
