// monorepo/cloud/maplefile-backend/internal/repo/filemetadata/anonymize_file_ips.go
package filemetadata

import (
	"context"
	"time"

	"github.com/gocql/gocql"
	"go.uber.org/zap"
)

// AnonymizeFileIPsByOwner immediately anonymizes all IP addresses for files owned by a specific user
// Used for GDPR right-to-be-forgotten implementation
func (impl *fileMetadataRepositoryImpl) AnonymizeFileIPsByOwner(ctx context.Context, ownerID gocql.UUID) (int, error) {
	impl.Logger.Info("Anonymizing IPs for files owned by user (GDPR mode)",
		zap.String("owner_id", ownerID.String()))

	count := 0

	// Query all files owned by this user
	query := `SELECT id FROM maplefile.files_by_id WHERE owner_id = ? ALLOW FILTERING`
	iter := impl.Session.Query(query, ownerID).WithContext(ctx).Iter()

	var fileID gocql.UUID
	var fileIDs []gocql.UUID

	// Collect all file IDs first
	for iter.Scan(&fileID) {
		fileIDs = append(fileIDs, fileID)
	}

	if err := iter.Close(); err != nil {
		impl.Logger.Error("Error querying files by owner", zap.Error(err))
		return count, err
	}

	// Anonymize IPs for each file
	for _, fID := range fileIDs {
		updateQuery := `
			UPDATE maplefile.files_by_id
			SET created_from_ip_address = '0.0.0.0',
			    modified_from_ip_address = '0.0.0.0',
			    ip_anonymized_at = ?
			WHERE id = ?
		`

		if err := impl.Session.Query(updateQuery, time.Now(), fID).WithContext(ctx).Exec(); err != nil {
			impl.Logger.Error("Failed to anonymize file IPs",
				zap.String("file_id", fID.String()),
				zap.Error(err))
			continue // Best-effort: continue with next file
		}
		count++
	}

	impl.Logger.Info("✅ Successfully anonymized file IPs",
		zap.String("owner_id", ownerID.String()),
		zap.Int("files_anonymized", count))

	return count, nil
}
