package sql

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

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

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

// NewMonetizationRepository creates a new MonetizationRepository.
func NewMonetizationRepository(db *gorm.DB, cfg *config.Config) domain.MonetizationRepository {
	return &monetizationRepository{
		BaseRepository: NewBaseRepositoryImpl[domain.Monetization](db, cfg),
		db:             db,
		tracer:         otel.Tracer("monetization.repository"),
	}
}

func (r *monetizationRepository) AddMonetizationToWork(ctx context.Context, workID uint, monetizationID uint) error {
	ctx, span := r.tracer.Start(ctx, "AddMonetizationToWork")
	defer span.End()
	workRecord := &domain.Work{TranslatableModel: domain.TranslatableModel{BaseModel: domain.BaseModel{ID: workID}}}
	monetization := &domain.Monetization{BaseModel: domain.BaseModel{ID: monetizationID}}
	return r.db.WithContext(ctx).Model(workRecord).Association("Monetizations").Append(monetization)
}

func (r *monetizationRepository) RemoveMonetizationFromWork(ctx context.Context, workID uint, monetizationID uint) error {
	ctx, span := r.tracer.Start(ctx, "RemoveMonetizationFromWork")
	defer span.End()
	workRecord := &domain.Work{TranslatableModel: domain.TranslatableModel{BaseModel: domain.BaseModel{ID: workID}}}
	monetization := &domain.Monetization{BaseModel: domain.BaseModel{ID: monetizationID}}
	return r.db.WithContext(ctx).Model(workRecord).Association("Monetizations").Delete(monetization)
}

func (r *monetizationRepository) AddMonetizationToAuthor(ctx context.Context, authorID uint, monetizationID uint) error {
	ctx, span := r.tracer.Start(ctx, "AddMonetizationToAuthor")
	defer span.End()
	author := &domain.Author{TranslatableModel: domain.TranslatableModel{BaseModel: domain.BaseModel{ID: authorID}}}
	monetization := &domain.Monetization{BaseModel: domain.BaseModel{ID: monetizationID}}
	return r.db.WithContext(ctx).Model(author).Association("Monetizations").Append(monetization)
}

func (r *monetizationRepository) RemoveMonetizationFromAuthor(ctx context.Context, authorID uint, monetizationID uint) error {
	ctx, span := r.tracer.Start(ctx, "RemoveMonetizationFromAuthor")
	defer span.End()
	author := &domain.Author{TranslatableModel: domain.TranslatableModel{BaseModel: domain.BaseModel{ID: authorID}}}
	monetization := &domain.Monetization{BaseModel: domain.BaseModel{ID: monetizationID}}
	return r.db.WithContext(ctx).Model(author).Association("Monetizations").Delete(monetization)
}

func (r *monetizationRepository) AddMonetizationToBook(ctx context.Context, bookID uint, monetizationID uint) error {
	ctx, span := r.tracer.Start(ctx, "AddMonetizationToBook")
	defer span.End()
	book := &domain.Book{TranslatableModel: domain.TranslatableModel{BaseModel: domain.BaseModel{ID: bookID}}}
	monetization := &domain.Monetization{BaseModel: domain.BaseModel{ID: monetizationID}}
	return r.db.WithContext(ctx).Model(book).Association("Monetizations").Append(monetization)
}

func (r *monetizationRepository) RemoveMonetizationFromBook(ctx context.Context, bookID uint, monetizationID uint) error {
	ctx, span := r.tracer.Start(ctx, "RemoveMonetizationFromBook")
	defer span.End()
	book := &domain.Book{TranslatableModel: domain.TranslatableModel{BaseModel: domain.BaseModel{ID: bookID}}}
	monetization := &domain.Monetization{BaseModel: domain.BaseModel{ID: monetizationID}}
	return r.db.WithContext(ctx).Model(book).Association("Monetizations").Delete(monetization)
}

func (r *monetizationRepository) AddMonetizationToPublisher(ctx context.Context, publisherID uint, monetizationID uint) error {
	ctx, span := r.tracer.Start(ctx, "AddMonetizationToPublisher")
	defer span.End()
	publisher := &domain.Publisher{TranslatableModel: domain.TranslatableModel{BaseModel: domain.BaseModel{ID: publisherID}}}
	monetization := &domain.Monetization{BaseModel: domain.BaseModel{ID: monetizationID}}
	return r.db.WithContext(ctx).Model(publisher).Association("Monetizations").Append(monetization)
}

func (r *monetizationRepository) RemoveMonetizationFromPublisher(ctx context.Context, publisherID uint, monetizationID uint) error {
	ctx, span := r.tracer.Start(ctx, "RemoveMonetizationFromPublisher")
	defer span.End()
	publisher := &domain.Publisher{TranslatableModel: domain.TranslatableModel{BaseModel: domain.BaseModel{ID: publisherID}}}
	monetization := &domain.Monetization{BaseModel: domain.BaseModel{ID: monetizationID}}
	return r.db.WithContext(ctx).Model(publisher).Association("Monetizations").Delete(monetization)
}

func (r *monetizationRepository) AddMonetizationToSource(ctx context.Context, sourceID uint, monetizationID uint) error {
	ctx, span := r.tracer.Start(ctx, "AddMonetizationToSource")
	defer span.End()
	source := &domain.Source{TranslatableModel: domain.TranslatableModel{BaseModel: domain.BaseModel{ID: sourceID}}}
	monetization := &domain.Monetization{BaseModel: domain.BaseModel{ID: monetizationID}}
	return r.db.WithContext(ctx).Model(source).Association("Monetizations").Append(monetization)
}

func (r *monetizationRepository) RemoveMonetizationFromSource(ctx context.Context, sourceID uint, monetizationID uint) error {
	ctx, span := r.tracer.Start(ctx, "RemoveMonetizationFromSource")
	defer span.End()
	source := &domain.Source{TranslatableModel: domain.TranslatableModel{BaseModel: domain.BaseModel{ID: sourceID}}}
	monetization := &domain.Monetization{BaseModel: domain.BaseModel{ID: monetizationID}}
	return r.db.WithContext(ctx).Model(source).Association("Monetizations").Delete(monetization)
}
