// monorepo/cloud/backend/internal/usecase/collection/anonymize_old_ips.go
package collection

import (
	"context"
	"time"

	"go.uber.org/zap"

	"codeberg.org/mapleopentech/monorepo/cloud/maplefile-backend/config"
	dom_collection "codeberg.org/mapleopentech/monorepo/cloud/maplefile-backend/internal/domain/collection"
)

type AnonymizeOldIPsUseCase interface {
	Execute(ctx context.Context, cutoffDate time.Time) (int, error)
}

type anonymizeOldIPsUseCaseImpl struct {
	config *config.Configuration
	logger *zap.Logger
	repo   dom_collection.CollectionRepository
}

func NewAnonymizeOldIPsUseCase(
	config *config.Configuration,
	logger *zap.Logger,
	repo dom_collection.CollectionRepository,
) AnonymizeOldIPsUseCase {
	logger = logger.Named("CollectionAnonymizeOldIPsUseCase")
	return &anonymizeOldIPsUseCaseImpl{config, logger, repo}
}

func (uc *anonymizeOldIPsUseCaseImpl) Execute(ctx context.Context, cutoffDate time.Time) (int, error) {
	uc.logger.Debug("Anonymizing old IPs in collection tables",
		zap.Time("cutoff_date", cutoffDate))

	count, err := uc.repo.AnonymizeOldIPs(ctx, cutoffDate)
	if err != nil {
		uc.logger.Error("Failed to anonymize old IPs in collection tables",
			zap.Error(err),
			zap.Time("cutoff_date", cutoffDate))
		return 0, err
	}

	uc.logger.Info("Successfully anonymized old IPs in collection tables",
		zap.Int("count", count),
		zap.Time("cutoff_date", cutoffDate))

	return count, nil
}
