from django.core.management.base import BaseCommand
from artworks.models import Artwork, Artwork3DView
from PIL import Image
import io
from django.core.files.base import ContentFile


class Command(BaseCommand):
    help = 'Crée des vues 3D placeholder pour les œuvres qui n\'en ont pas'

    def handle(self, *args, **options):
        artworks_without_3d = Artwork.objects.filter(
            is_published=True
        ).exclude(
            view_3d__isnull=False
        )

        count = 0
        for artwork in artworks_without_3d:
            # Créer une image placeholder
            img = Image.new('RGB', (4096, 2048), color=(200, 200, 200))
            buffer = io.BytesIO()
            img.save(buffer, format='JPEG')

            # Créer la vue 3D
            view_3d = Artwork3DView.objects.create(
                artwork=artwork,
                initial_yaw=0,
                initial_pitch=0,
                initial_fov=75,
                allow_zoom=True,
                allow_rotation=True,
                show_controls=True,
                is_active=False  # Désactivé par défaut, à activer après avoir ajouté la vraie image
            )

            # Sauvegarder l'image placeholder
            view_3d.panorama_image.save(
                f'placeholder_{artwork.reference_code}.jpg',
                ContentFile(buffer.getvalue()),
                save=True
            )

            count += 1
            self.stdout.write(f'Vue 3D placeholder créée pour {artwork.reference_code}')

        self.stdout.write(
            self.style.SUCCESS(f'✓ {count} vues 3D placeholder créées')
        )