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

import (
	"context"
	"time"

	"go.uber.org/zap"

	"codeberg.org/mapleopentech/monorepo/cloud/maplefile-backend/config"
	dom_file "codeberg.org/mapleopentech/monorepo/cloud/maplefile-backend/internal/domain/file"
)

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

type anonymizeOldIPsUseCaseImpl struct {
	config *config.Configuration
	logger *zap.Logger
	repo   dom_file.FileMetadataRepository
}

func NewAnonymizeOldIPsUseCase(
	config *config.Configuration,
	logger *zap.Logger,
	repo dom_file.FileMetadataRepository,
) AnonymizeOldIPsUseCase {
	logger = logger.Named("FileMetadataAnonymizeOldIPsUseCase")
	return &anonymizeOldIPsUseCaseImpl{config, logger, repo}
}

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

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

	return count, nil
}
