#!/usr/bin/env python
"""Script to inject WordPress structure data into the backend"""

from igny8_core.business.integration.models import SiteIntegration
from igny8_core.auth.models import Site
from django.utils import timezone

# Get site 5
try:
    site = Site.objects.get(id=5)
    print(f"✓ Found site: {site.name}")
except Site.DoesNotExist:
    print("✗ Site with ID 5 not found!")
    exit(1)

# Get or create WordPress integration for this site
integration, created = SiteIntegration.objects.get_or_create(
    site=site,
    platform='wordpress',
    defaults={
        'is_active': True,
        'sync_enabled': True,
        'config_json': {}
    }
)

print(f"✓ Integration ID: {integration.id} (newly created: {created})")

# Add structure data
integration.config_json = {
    'content_types': {
        'post_types': {
            'post': {
                'label': 'Posts',
                'count': 150,
                'enabled': True,
                'fetch_limit': 100,
                'synced_count': 0
            },
            'page': {
                'label': 'Pages',
                'count': 25,
                'enabled': True,
                'fetch_limit': 100,
                'synced_count': 0
            },
            'product': {
                'label': 'Products',
                'count': 89,
                'enabled': True,
                'fetch_limit': 100,
                'synced_count': 0
            }
        },
        'taxonomies': {
            'category': {
                'label': 'Categories',
                'count': 15,
                'enabled': True,
                'fetch_limit': 100,
                'synced_count': 0
            },
            'post_tag': {
                'label': 'Tags',
                'count': 234,
                'enabled': True,
                'fetch_limit': 100,
                'synced_count': 0
            },
            'product_cat': {
                'label': 'Product Categories',
                'count': 12,
                'enabled': True,
                'fetch_limit': 100,
                'synced_count': 0
            }
        },
        'last_structure_fetch': timezone.now().isoformat()
    },
    'plugin_connection_enabled': True,
    'two_way_sync_enabled': True
}

integration.save()
print("✓ Structure data saved!")
print(f"✓ Post Types: {len(integration.config_json['content_types']['post_types'])}")
print(f"✓ Taxonomies: {len(integration.config_json['content_types']['taxonomies'])}")
print(f"✓ Last fetch: {integration.config_json['content_types']['last_structure_fetch']}")
print("\n🎉 SUCCESS! Now refresh: https://app.igny8.com/sites/5/settings?tab=content-types")

