"""
Management command to update existing pages to use Lumina design style.
"""
from django.core.management.base import BaseCommand
from smoothschedule.platform.tenant_sites.models import Page


class Command(BaseCommand):
    help = 'Update existing pages to use Lumina design style'

    def handle(self, *args, **options):
        pages = Page.objects.all()
        updated_count = 0

        for page in pages:
            content = page.puck_data.get('content', [])
            updated = False

            for component in content:
                if component.get('type') == 'Hero':
                    props = component.get('props', {})

                    # Remove old color props
                    if 'backgroundColor' in props:
                        del props['backgroundColor']
                        updated = True
                    if 'textColor' in props:
                        del props['textColor']
                        updated = True
                    if 'buttonColor' in props:
                        del props['buttonColor']
                        updated = True

                    # Ensure CTA props exist
                    if 'ctaText' not in props:
                        props['ctaText'] = 'Book Now'
                        props['ctaLink'] = '/book'
                        updated = True

                elif component.get('type') == 'Booking':
                    props = component.get('props', {})

                    # Remove old color props
                    if 'accentColor' in props:
                        del props['accentColor']
                        updated = True
                    if 'buttonLabel' in props:
                        del props['buttonLabel']
                        updated = True

                elif component.get('type') == 'TextSection':
                    props = component.get('props', {})

                    # Remove old color props
                    if 'backgroundColor' in props:
                        del props['backgroundColor']
                        updated = True

            if updated:
                page.save()
                updated_count += 1
                self.stdout.write(
                    self.style.SUCCESS(
                        f'Updated page: {page.title} ({page.site.tenant.name})'
                    )
                )

        self.stdout.write(
            self.style.SUCCESS(
                f'\nSummary: Updated {updated_count} pages to Lumina style'
            )
        )
