package enrichment

import (
	"context"
	"tercul/internal/domain"
)

// Service is the main entrypoint for the enrichment functionality.
// It orchestrates different enrichers for various domain entities.
type Service struct {
	AuthorEnrichers []AuthorEnricher
	WorkEnrichers   []WorkEnricher
}

// NewService creates a new enrichment Service.
func NewService() *Service {
	service := &Service{
		AuthorEnrichers: []AuthorEnricher{},
		WorkEnrichers:   []WorkEnricher{},
	}
	service.RegisterAuthorEnricher(NewOpenLibraryAuthorEnricher())
	return service
}

// AuthorEnricher defines the interface for enriching author data.
type AuthorEnricher interface {
	Enrich(ctx context.Context, author *domain.Author) error
	Name() string
}

// WorkEnricher defines the interface for enriching work data.
type WorkEnricher interface {
	Enrich(ctx context.Context, work *domain.Work) error
	Name() string
}

// RegisterAuthorEnricher adds a new author enricher to the service.
func (s *Service) RegisterAuthorEnricher(enricher AuthorEnricher) {
	s.AuthorEnrichers = append(s.AuthorEnrichers, enricher)
}

// RegisterWorkEnricher adds a new work enricher to the service.
func (s *Service) RegisterWorkEnricher(enricher WorkEnricher) {
	s.WorkEnrichers = append(s.WorkEnrichers, enricher)
}

// EnrichAuthor iterates through registered author enrichers and applies them.
func (s *Service) EnrichAuthor(ctx context.Context, author *domain.Author) error {
	for _, enricher := range s.AuthorEnrichers {
		if err := enricher.Enrich(ctx, author); err != nil {
			// In a real implementation, we might want to log errors but continue.
			return err
		}
	}
	return nil
}

// EnrichWork iterates through registered work enrichers and applies them.
func (s *Service) EnrichWork(ctx context.Context, work *domain.Work) error {
	for _, enricher := range s.WorkEnrichers {
		if err := enricher.Enrich(ctx, work); err != nil {
			return err
		}
	}
	return nil
}