# Generated by Django 5.2.5 on 2025-08-30 20:57

from django.db import migrations, models


def migrate_avatar_data(apps, schema_editor):
    """
    Migrate avatar data from old CloudflareImageField to new ForeignKey structure.
    Since we're transitioning to a new system, we'll just drop the old avatar column
    and add the new avatar_id column for ForeignKey relationships.
    """
    # This is a data migration - we'll handle the schema changes in the operations
    pass


def reverse_migrate_avatar_data(apps, schema_editor):
    """
    Reverse migration - not implemented as this is a one-way migration
    """
    pass


def safe_add_avatar_field(apps, schema_editor):
    """
    Safely add avatar field, checking if it already exists.
    """
    # Check if the column already exists
    with schema_editor.connection.cursor() as cursor:
        cursor.execute(
            """
            SELECT column_name
            FROM information_schema.columns
            WHERE table_name='accounts_userprofile'
            AND column_name='avatar_id'
        """
        )

        column_exists = cursor.fetchone() is not None

        if not column_exists:
            # Column doesn't exist, add it
            UserProfile = apps.get_model("accounts", "UserProfile")
            field = models.ForeignKey(
                "django_cloudflareimages_toolkit.CloudflareImage", on_delete=models.SET_NULL, null=True, blank=True
            )
            field.set_attributes_from_name("avatar")
            schema_editor.add_field(UserProfile, field)


def reverse_safe_add_avatar_field(apps, schema_editor):
    """
    Reverse the safe avatar field addition.
    """
    # Check if the column exists and remove it
    with schema_editor.connection.cursor() as cursor:
        cursor.execute(
            """
            SELECT column_name
            FROM information_schema.columns
            WHERE table_name='accounts_userprofile'
            AND column_name='avatar_id'
        """
        )

        column_exists = cursor.fetchone() is not None

        if column_exists:
            UserProfile = apps.get_model("accounts", "UserProfile")
            field = models.ForeignKey(
                "django_cloudflareimages_toolkit.CloudflareImage", on_delete=models.SET_NULL, null=True, blank=True
            )
            field.set_attributes_from_name("avatar")
            schema_editor.remove_field(UserProfile, field)


class Migration(migrations.Migration):

    dependencies = [
        (
            "accounts",
            "0009_notificationpreference_notificationpreferenceevent_and_more",
        ),
        ("django_cloudflareimages_toolkit", "0001_initial"),
    ]

    operations = [
        # First, remove the old avatar column (CloudflareImageField)
        migrations.RunSQL(
            "ALTER TABLE accounts_userprofile DROP COLUMN IF EXISTS avatar;",
            reverse_sql="-- Cannot reverse this operation",
        ),
        # Safely add the new avatar_id column for ForeignKey
        migrations.RunPython(
            safe_add_avatar_field,
            reverse_safe_add_avatar_field,
        ),
        # Run the data migration
        migrations.RunPython(
            migrate_avatar_data,
            reverse_migrate_avatar_data,
        ),
    ]
