"""
Core App Signals

Handles automatic setup tasks when tenants are created.
"""
import logging
from django.db import transaction
from django.db.models.signals import post_save
from django.dispatch import receiver

logger = logging.getLogger(__name__)


def _seed_plugins_for_tenant(tenant_schema_name):
    """
    Internal function to seed platform plugins for a tenant.
    Called after transaction commits to ensure schema tables exist.
    """
    from django_tenants.utils import schema_context
    from smoothschedule.scheduling.schedule.models import PluginTemplate
    from django.utils import timezone

    logger.info(f"Seeding platform plugins for new tenant: {tenant_schema_name}")

    try:
        with schema_context(tenant_schema_name):
            # Import the plugin definitions from the seed command
            from smoothschedule.scheduling.schedule.management.commands.seed_platform_plugins import get_platform_plugins

            plugins_data = get_platform_plugins()
            created_count = 0

            for plugin_data in plugins_data:
                # Check if plugin already exists by slug
                if PluginTemplate.objects.filter(slug=plugin_data['slug']).exists():
                    continue

                # Create the plugin
                PluginTemplate.objects.create(
                    name=plugin_data['name'],
                    slug=plugin_data['slug'],
                    category=plugin_data['category'],
                    short_description=plugin_data['short_description'],
                    description=plugin_data['description'],
                    plugin_code=plugin_data['plugin_code'],
                    logo_url=plugin_data.get('logo_url', ''),
                    visibility=PluginTemplate.Visibility.PLATFORM,
                    is_approved=True,
                    approved_at=timezone.now(),
                    author_name='Smooth Schedule',
                    license_type='PLATFORM',
                )
                created_count += 1

            logger.info(f"Created {created_count} platform plugins for tenant: {tenant_schema_name}")
    except Exception as e:
        logger.error(f"Failed to seed plugins for tenant {tenant_schema_name}: {e}")


@receiver(post_save, sender='core.Tenant')
def seed_platform_plugins_on_tenant_create(sender, instance, created, **kwargs):
    """
    Seed platform plugins when a new tenant is created.

    This ensures new tenants have access to all marketplace plugins immediately.
    Uses transaction.on_commit() to defer seeding until after the schema is
    fully created and migrations have run.
    """
    if not created:
        return

    # Skip public schema
    if instance.schema_name == 'public':
        return

    # Defer the seeding until after the transaction commits
    # This ensures the schema and all tables exist before we try to use them
    schema_name = instance.schema_name
    transaction.on_commit(lambda: _seed_plugins_for_tenant(schema_name))
