# Generated by Django 5.2.8 on 2025-12-01 20:13

from django.db import migrations


def migrate_email_settings_to_addresses(apps, schema_editor):
    """
    Migrate data from TicketEmailSettings (singleton) to TicketEmailAddress (multi-email system).
    Creates a platform-wide email address from the existing settings.
    """
    TicketEmailSettings = apps.get_model('tickets', 'TicketEmailSettings')
    TicketEmailAddress = apps.get_model('tickets', 'TicketEmailAddress')

    # Get the existing settings
    settings = TicketEmailSettings.objects.first()

    if settings and settings.imap_host and settings.smtp_host:
        # Check if we already have a platform email address with this email
        existing = TicketEmailAddress.objects.filter(
            tenant__isnull=True,
            email_address=settings.support_email_address
        ).first()

        if not existing:
            # Create new TicketEmailAddress from settings
            TicketEmailAddress.objects.create(
                tenant=None,  # Platform-wide
                display_name='Platform Support',
                email_address=settings.support_email_address or settings.smtp_from_email or settings.imap_username,
                color='#3b82f6',  # Default blue
                # IMAP settings
                imap_host=settings.imap_host,
                imap_port=settings.imap_port,
                imap_use_ssl=settings.imap_use_ssl,
                imap_username=settings.imap_username,
                imap_password=settings.imap_password,
                imap_folder=settings.imap_folder,
                # SMTP settings
                smtp_host=settings.smtp_host,
                smtp_port=settings.smtp_port,
                smtp_use_tls=settings.smtp_use_tls,
                smtp_use_ssl=settings.smtp_use_ssl,
                smtp_username=settings.smtp_username,
                smtp_password=settings.smtp_password,
                # Status
                is_active=settings.is_enabled,
                is_default=True,
            )
            print(f"✓ Migrated email settings to new TicketEmailAddress: {settings.support_email_address}")
        else:
            print(f"✓ Platform email address already exists: {settings.support_email_address}")
    else:
        print("✓ No email settings to migrate")


def reverse_migration(apps, schema_editor):
    """
    Reverse migration - delete the migrated platform email address.
    """
    TicketEmailAddress = apps.get_model('tickets', 'TicketEmailAddress')

    # Delete platform-wide email addresses
    deleted_count = TicketEmailAddress.objects.filter(
        tenant__isnull=True,
        display_name='Platform Support'
    ).delete()[0]

    print(f"✓ Deleted {deleted_count} platform email address(es)")


class Migration(migrations.Migration):

    dependencies = [
        ('tickets', '0011_alter_ticketemailaddress_tenant'),
    ]

    operations = [
        migrations.RunPython(migrate_email_settings_to_addresses, reverse_migration),
    ]
