package db_updater

import (
	"github.com/rs/zerolog/log"
	"github.com/safing/mmdbmeld"
	"sync"
	"thuanle.me/ip-info/configs"
)

func mergeMmdb() error {
	c, err := mmdbmeld.LoadConfig(configs.MmdbmeldConfig)
	if err != nil {
		log.Err(err).Msg("Failed to load mmdbmeld config")
		return err
	}

	for _, db := range c.Databases {
		log.Info().Str("name", db.Name).Msg("Building mmdb file")

		// Apply defaults.
		dbP := &db //nolint:gosec,scopelint // Only used within loop.
		c.Defaults.ApplyTo(dbP)

		// Load sources for database.
		sources, err := mmdbmeld.LoadSources(db)
		if err != nil {
			log.Err(err).Msg("Failed to load sources")
			return err
		}

		// Create wait group.
		var wg sync.WaitGroup
		wg.Add(1)

		// Start log writer.
		updates := make(chan string, 100)
		go func() {
			defer wg.Done()
			for msg := range updates {
				log.Debug().Str("msg", msg).Msg("MMDB update")
			}
		}()

		// Read all sources and write to mmdb.
		err = mmdbmeld.WriteMMDB(db, sources, updates)
		if err != nil {
			log.Err(err).Msg("Failed to write mmdb")
			return err
		}
		wg.Wait()
		log.Info().Str("file", db.Output).Msg("MMDB created")
	}
	return nil
}
