package sql

import (
	"context"
	"tercul/internal/domain"
	"tercul/internal/platform/config"

	"go.opentelemetry.io/otel"
	"go.opentelemetry.io/otel/trace"
	"gorm.io/gorm"
)

type likeRepository struct {
	domain.BaseRepository[domain.Like]
	db     *gorm.DB
	tracer trace.Tracer
}

// NewLikeRepository creates a new LikeRepository.
func NewLikeRepository(db *gorm.DB, cfg *config.Config) domain.LikeRepository {
	return &likeRepository{
		BaseRepository: NewBaseRepositoryImpl[domain.Like](db, cfg),
		db:             db,
		tracer:         otel.Tracer("like.repository"),
	}
}

// ListByUserID finds likes by user ID
func (r *likeRepository) ListByUserID(ctx context.Context, userID uint) ([]domain.Like, error) {
	ctx, span := r.tracer.Start(ctx, "ListByUserID")
	defer span.End()
	var likes []domain.Like
	if err := r.db.WithContext(ctx).Where("user_id = ?", userID).Find(&likes).Error; err != nil {
		return nil, err
	}
	return likes, nil
}

// ListByWorkID finds likes by work ID
func (r *likeRepository) ListByWorkID(ctx context.Context, workID uint) ([]domain.Like, error) {
	ctx, span := r.tracer.Start(ctx, "ListByWorkID")
	defer span.End()
	var likes []domain.Like
	if err := r.db.WithContext(ctx).Where("work_id = ?", workID).Find(&likes).Error; err != nil {
		return nil, err
	}
	return likes, nil
}

// ListByTranslationID finds likes by translation ID
func (r *likeRepository) ListByTranslationID(ctx context.Context, translationID uint) ([]domain.Like, error) {
	ctx, span := r.tracer.Start(ctx, "ListByTranslationID")
	defer span.End()
	var likes []domain.Like
	if err := r.db.WithContext(ctx).Where("translation_id = ?", translationID).Find(&likes).Error; err != nil {
		return nil, err
	}
	return likes, nil
}

// ListByCommentID finds likes by comment ID
func (r *likeRepository) ListByCommentID(ctx context.Context, commentID uint) ([]domain.Like, error) {
	ctx, span := r.tracer.Start(ctx, "ListByCommentID")
	defer span.End()
	var likes []domain.Like
	if err := r.db.WithContext(ctx).Where("comment_id = ?", commentID).Find(&likes).Error; err != nil {
		return nil, err
	}
	return likes, nil
}
