"""
Management command to update existing Hero components with CTA button props.
"""
from django.core.management.base import BaseCommand
from smoothschedule.platform.tenant_sites.models import Page


class Command(BaseCommand):
    help = 'Update existing Hero components with CTA button props'

    def handle(self, *args, **options):
        pages = Page.objects.filter(is_home=True)
        updated_count = 0
        skipped_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', {})

                    # Add CTA button props if they don't exist
                    if 'ctaText' not in props:
                        props['ctaText'] = 'Book Now'
                        props['ctaLink'] = '/book'
                        props['buttonColor'] = '#4f46e5'
                        updated = True
                        self.stdout.write(
                            self.style.SUCCESS(
                                f'Updated Hero component in page: {page.title} ({page.site.tenant.name})'
                            )
                        )

            if updated:
                page.save()
                updated_count += 1
            else:
                skipped_count += 1

        self.stdout.write(
            self.style.SUCCESS(
                f'\nSummary: Updated {updated_count} pages, skipped {skipped_count} pages'
            )
        )
