from django.core.management.base import BaseCommand
from django.db import transaction

from servala.core.models.service import ControlPlane


class Command(BaseCommand):
    help = "Re-encrypts all encrypted fields. Use when rotating SECRET_KEY/SALT"

    def handle(self, *args, **options):
        self.stdout.write("Starting re-encryption of ControlPlane objects...")

        count = 0
        with transaction.atomic():
            for control_plane in ControlPlane.objects.all():
                control_plane.save()
                count += 1

        self.stdout.write(
            self.style.SUCCESS(f"Re-encrypted {count} ControlPlane objects")
        )
