package worker

import (
	"context"
	"log/slog"

	"github.com/TopherMayor/unified-media-manager/internal/config"
	"github.com/TopherMayor/unified-media-manager/internal/service"
)

type MetadataRefreshWorker struct {
	metadataSvc *service.MetadataService
	cfg         *config.Config
}

func NewMetadataRefreshWorker(metadataSvc *service.MetadataService, cfg *config.Config) *MetadataRefreshWorker {
	return &MetadataRefreshWorker{
		metadataSvc: metadataSvc,
		cfg:         cfg,
	}
}

func (w *MetadataRefreshWorker) Name() string {
	return "metadata_refresh"
}

func (w *MetadataRefreshWorker) CronExpr() string {
	return w.cfg.WorkerMetadataInterval
}

func (w *MetadataRefreshWorker) Run(ctx context.Context) error {
	if err := w.metadataSvc.RefreshAllMetadata(ctx); err != nil {
		slog.Error("metadata refresh failed", "error", err)
		return err
	}

	slog.Info("metadata refresh completed")
	return nil
}
