from django.core.management.base import BaseCommand
import subprocess

class Command(BaseCommand):
    help = 'Tworzy CDN dla kontenera OVH (OpenStack Swift)'

    def handle(self, *args, **kwargs):
        container_name = 'hously'
        region = 'waw'

        try:
            self.stdout.write(f'✨ Tworzenie CDN dla kontenera: {container_name}')
            result = subprocess.run(
                [
                    'openstack', 'cdn', 'create',
                    '--region', region,
                    '--name', container_name,
                    container_name
                ],
                capture_output=True,
                text=True,
                check=True
            )

            self.stdout.write(self.style.SUCCESS(f'✅ CDN utworzony pomyślnie:'))
            self.stdout.write(result.stdout)

        except subprocess.CalledProcessError as e:
            self.stderr.write(self.style.ERROR('❌ Błąd podczas tworzenia CDN:'))
            self.stderr.write(self.style.ERROR(e.stderr))
        except FileNotFoundError:
            self.stderr.write(self.style.ERROR(
                '❌ Nie znaleziono polecenia openstack. Upewnij się, że masz zainstalowany i skonfigurowany OpenStack CLI.'))
