package sql

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

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

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

// NewCopyrightClaimRepository creates a new CopyrightClaimRepository.
func NewCopyrightClaimRepository(db *gorm.DB, cfg *config.Config) domain.CopyrightClaimRepository {
	return &copyrightClaimRepository{
		BaseRepository: NewBaseRepositoryImpl[domain.CopyrightClaim](db, cfg),
		db:             db,
		tracer:         otel.Tracer("copyright_claim.repository"),
	}
}

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

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