// codeberg.org/mapleopentech/monorepo/cloud/maplefile-backend/internal/usecase/user/anonymize_old_ips.go
package user

import (
	"context"
	"time"

	"go.uber.org/zap"

	"codeberg.org/mapleopentech/monorepo/cloud/maplefile-backend/config"
	dom_user "codeberg.org/mapleopentech/monorepo/cloud/maplefile-backend/internal/domain/user"
)

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

type anonymizeOldIPsUseCaseImpl struct {
	config *config.Configuration
	logger *zap.Logger
	repo   dom_user.Repository
}

func NewAnonymizeOldIPsUseCase(config *config.Configuration, logger *zap.Logger, repo dom_user.Repository) AnonymizeOldIPsUseCase {
	logger = logger.Named("UserAnonymizeOldIPsUseCase")
	return &anonymizeOldIPsUseCaseImpl{config, logger, repo}
}

func (uc *anonymizeOldIPsUseCaseImpl) Execute(ctx context.Context, cutoffDate time.Time) (int, error) {
	uc.logger.Debug("Anonymizing old IPs in user 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 user tables",
			zap.Error(err),
			zap.Time("cutoff_date", cutoffDate))
		return 0, err
	}

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

	return count, nil
}
