package import_service

import (
	"context"
	"log"
	"strings"
	"time"

	"git.leaktechnologies.dev/stu/Goondex/internal/db"
	"git.leaktechnologies.dev/stu/Goondex/internal/model"
	"git.leaktechnologies.dev/stu/Goondex/internal/scraper/adultemp"
	"git.leaktechnologies.dev/stu/Goondex/internal/scraper/merger"
)

type Enricher struct {
	db      *db.DB
	adult   *adultemp.Scraper
	delay   time.Duration
	enabled bool
}

func NewEnricher(database *db.DB, delay time.Duration) (*Enricher, error) {
	adult, err := adultemp.NewScraper()
	if err != nil {
		return nil, err
	}
	return &Enricher{
		db:      database,
		adult:   adult,
		delay:   delay,
		enabled: true,
	}, nil
}

// EnrichPerformer tries to fill missing fields via Adult Empire by name search.
func (e *Enricher) EnrichPerformer(ctx context.Context, p *model.Performer) {
	if !e.enabled || p == nil {
		return
	}
	name := strings.TrimSpace(p.Name)
	if name == "" {
		return
	}

	results, err := e.adult.SearchPerformersByName(ctx, name)
	if err != nil || len(results) == 0 {
		return
	}
	data, err := e.adult.ScrapePerformerByURL(ctx, results[0].URL)
	if err != nil {
		return
	}

	// Only merge when names reasonably match
	if !merger.ShouldMerge(p.Name, data.Name) {
		return
	}

	merged := merger.MergePerformerData(p, data)

	// Preserve original source IDs (TPDB format)
	merged.Source = p.Source
	merged.SourceID = p.SourceID
	merged.SourceNumericID = p.SourceNumericID
	merged.ID = p.ID

	store := db.NewPerformerStore(e.db)
	if err := store.Create(merged); err != nil {
		log.Printf("enrich: failed to update performer %s: %v", name, err)
	}

	if e.delay > 0 {
		time.Sleep(e.delay)
	}
}
