# fortunaisk/migrations/0007_add_new_schema_squashed_0020_alter_ticketanomaly_detail.py
# Generated by Django 4.2.20 on 2025-05-30 13:34

# Standard Library
from decimal import Decimal

# Django
import django.db.migrations.operations.special
import django.db.models.deletion
import django.utils.timezone
from django.conf import settings
from django.db import migrations, models


def check_table_exists(cursor, table_name):
    """Vérifie si une table existe"""
    cursor.execute(f"SHOW TABLES LIKE '{table_name}'")
    return cursor.fetchone() is not None


def check_column_exists(cursor, table_name, column_name):
    """Vérifie si une colonne existe dans une table"""
    cursor.execute(f"SHOW COLUMNS FROM {table_name} LIKE '{column_name}'")
    return cursor.fetchone() is not None


def forwards_func(apps, schema_editor):
    """Migration des données de winners_distribution vers WinnerDistribution"""
    AutoLottery = apps.get_model("fortunaisk", "AutoLottery")
    Lottery = apps.get_model("fortunaisk", "Lottery")
    WinnerDistribution = apps.get_model("fortunaisk", "WinnerDistribution")

    # Migrer les distributions de Lottery.winners_distribution
    for lottery in Lottery.objects.all():
        # Si le champ JSON existe et n'est pas vide
        data = getattr(lottery, "winners_distribution", None)
        if not data:
            continue

        try:
            # On récupère la liste de pourcentages
            if isinstance(data, list):
                distributions = [str(d) for d in data]
            else:
                distributions = str(data).split(",")

            for idx, dist in enumerate(distributions, start=1):
                dist = dist.strip().rstrip("%")
                if not dist:
                    continue
                pct = Decimal(dist)
                WinnerDistribution.objects.create(
                    lottery_reference=lottery.lottery_reference,
                    winner_rank=idx,
                    winner_prize_distribution=pct,
                )
        except Exception as e:
            # Debug en cas de problème
            print(f"Erreur migration Lottery[{lottery.pk}]: {e}")

    # Migrer les distributions de AutoLottery.winners_distribution (passer en list si nécessaire)
    for auto in AutoLottery.objects.all():
        data = getattr(auto, "winners_distribution", None)
        if not data:
            continue

        try:
            if not isinstance(data, list):
                parts = str(data).split(",")
                auto.winners_distribution = [
                    float(p.strip().rstrip("%")) for p in parts if p.strip()
                ]
                auto.save(update_fields=["winners_distribution"])
        except Exception as e:
            print(f"Erreur migration AutoLottery[{auto.pk}]: {e}")


def fill_processedpayment_fields(apps, schema_editor):
    """Remplissage des champs user/character/amount/payed_at de ProcessedPayment"""
    ProcessedPayment = apps.get_model("fortunaisk", "ProcessedPayment")
    TicketPurchase = apps.get_model("fortunaisk", "TicketPurchase")

    for payment in ProcessedPayment.objects.all():
        tickets = TicketPurchase.objects.filter(payment_id=payment.payment_id)
        if not tickets.exists():
            continue
        ticket = tickets.first()
        payment.user = ticket.user
        payment.character = ticket.character
        payment.payed_at = ticket.purchase_date
        payment.amount = ticket.amount
        payment.save(update_fields=["user", "character", "payed_at", "amount"])


def convert_notification_config(apps, schema_editor):
    """Conversion du champ notification_config de dict → list + ping_roles"""
    WebhookConfiguration = apps.get_model("fortunaisk", "WebhookConfiguration")

    for webhook in WebhookConfiguration.objects.all():
        config = webhook.notification_config
        if isinstance(config, dict):
            events = config.get("events", [])
            ping_roles = config.get("ping_roles", [])
            webhook.notification_config = events
            webhook.ping_roles = ping_roles
            webhook.save(update_fields=["notification_config", "ping_roles"])


def set_default_webhook_name(apps, schema_editor):
    """Définir un nom par défaut pour les WebhookConfiguration vides"""
    WebhookConfiguration = apps.get_model("fortunaisk", "WebhookConfiguration")

    for idx, webhook in enumerate(
        WebhookConfiguration.objects.filter(name=""), start=1
    ):
        webhook.name = f"Webhook Configuration {idx}"
        webhook.save(update_fields=["name"])


def set_empty_detail(apps, schema_editor):
    """Remplacer les valeurs NULL par '' dans TicketAnomaly.detail"""
    TicketAnomaly = apps.get_model("fortunaisk", "TicketAnomaly")
    TicketAnomaly.objects.filter(detail__isnull=True).update(detail="")


def fix_ping_roles(apps, schema_editor):
    """Nettoyage du format ping_roles pour WebhookConfiguration"""
    WebhookConfiguration = apps.get_model("fortunaisk", "WebhookConfiguration")

    for webhook in WebhookConfiguration.objects.all():
        roles = webhook.ping_roles
        if isinstance(roles, str):
            try:
                webhook.ping_roles = [
                    role.strip() for role in roles.split(",") if role.strip()
                ]
                webhook.save(update_fields=["ping_roles"])
            except Exception:
                webhook.ping_roles = []
                webhook.save(update_fields=["ping_roles"])


class Migration(migrations.Migration):

    replaces = [
        ("fortunaisk", "0007_add_new_schema"),
        ("fortunaisk", "0008_migrate_data"),
        ("fortunaisk", "0009_fill_processedpayment_fields"),
        ("fortunaisk", "0010_autolottery_tax_lottery_tax"),
        ("fortunaisk", "0011_autolottery_tax_amount_lottery_tax_amount"),
        ("fortunaisk", "0012_alter_autolottery_winners_distribution_and_more"),
        ("fortunaisk", "0013_remove_lottery_winners_distribution"),
        ("fortunaisk", "0014_alter_webhookconfiguration_options_and_more"),
        ("fortunaisk", "0015_alter_webhookconfiguration_options_and_more"),
        ("fortunaisk", "0016_convert_notification_config"),
        ("fortunaisk", "0017_fill_webhook_names"),
        ("fortunaisk", "0018_fill_detail_empty"),
        ("fortunaisk", "0019_fix_ping_roles"),
        ("fortunaisk", "0020_alter_ticketanomaly_detail"),
    ]

    dependencies = [
        ("eveonline", "0017_alliance_and_corp_names_are_not_unique"),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ("fortunaisk", "0006_lowercase_interval_period"),
    ]

    operations = [
        # 1) Create new WinnerDistribution table
        migrations.CreateModel(
            name="WinnerDistribution",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "lottery_reference",
                    models.CharField(
                        max_length=20,
                        db_index=True,
                        verbose_name="Lottery Reference",
                    ),
                ),
                (
                    "winner_rank",
                    models.PositiveIntegerField(verbose_name="Winner Rank"),
                ),
                (
                    "winner_prize_distribution",
                    models.DecimalField(
                        max_digits=5,
                        decimal_places=2,
                        verbose_name="Prize Distribution (%)",
                    ),
                ),
                (
                    "created_at",
                    models.DateTimeField(auto_now_add=True, verbose_name="Created At"),
                ),
                (
                    "updated_at",
                    models.DateTimeField(auto_now=True, verbose_name="Updated At"),
                ),
            ],
            options={
                "db_table": "fortunaisk_winnerdistribution",
                "ordering": ["lottery_reference", "winner_rank"],
                "default_permissions": (),
            },
        ),
        # 2) Add missing fields to ProcessedPayment
        migrations.AddField(
            model_name="processedpayment",
            name="character",
            field=models.ForeignKey(
                blank=True,
                null=True,
                on_delete=django.db.models.deletion.SET_NULL,
                to="eveonline.evecharacter",
                verbose_name="Eve Character",
            ),
        ),
        migrations.AddField(
            model_name="processedpayment",
            name="user",
            field=models.ForeignKey(
                blank=True,
                null=True,
                on_delete=django.db.models.deletion.SET_NULL,
                to=settings.AUTH_USER_MODEL,
                verbose_name="Django User",
            ),
        ),
        migrations.AddField(
            model_name="processedpayment",
            name="amount",
            field=models.DecimalField(
                max_digits=25,
                decimal_places=2,
                default=Decimal("0.00"),
                verbose_name="Total Paid Amount",
                help_text="Total amount paid in ISK.",
            ),
        ),
        migrations.AddField(
            model_name="processedpayment",
            name="payed_at",
            field=models.DateTimeField(
                blank=True,
                null=True,
                verbose_name="Paid At",
                help_text="Payment date & time.",
            ),
        ),
        # 3) Add missing fields to TicketAnomaly
        migrations.AddField(
            model_name="ticketanomaly",
            name="solved",
            field=models.BooleanField(default=False, verbose_name="Solved"),
        ),
        migrations.AddField(
            model_name="ticketanomaly",
            name="solved_at",
            field=models.DateTimeField(blank=True, null=True, verbose_name="Solved At"),
        ),
        migrations.AddField(
            model_name="ticketanomaly",
            name="solved_by",
            field=models.ForeignKey(
                blank=True,
                null=True,
                on_delete=django.db.models.deletion.SET_NULL,
                related_name="resolved_anomalies",
                to=settings.AUTH_USER_MODEL,
                verbose_name="Solved By",
            ),
        ),
        migrations.AddField(
            model_name="ticketanomaly",
            name="detail",
            field=models.TextField(
                blank=True,
                default="",
                verbose_name="Detail",
                help_text="Resolution details.",
            ),
        ),
        # 4) Champs ajoutés pour Winner
        migrations.AddField(
            model_name="winner",
            name="distributed_at",
            field=models.DateTimeField(
                blank=True, null=True, verbose_name="Distributed At"
            ),
        ),
        migrations.AddField(
            model_name="winner",
            name="distributed_by",
            field=models.ForeignKey(
                blank=True,
                null=True,
                on_delete=django.db.models.deletion.SET_NULL,
                related_name="distributed_winnings",
                to=settings.AUTH_USER_MODEL,
                verbose_name="Distributed By",
            ),
        ),
        # 5) Champs ajoutés pour WebhookConfiguration
        migrations.AddField(
            model_name="webhookconfiguration",
            name="notification_config",
            field=models.JSONField(
                blank=True,
                default=list,
                verbose_name="Events to Notify",
                help_text="Which events should be sent to this webhook.",
            ),
        ),
        migrations.AddField(
            model_name="webhookconfiguration",
            name="created_at",
            field=models.DateTimeField(
                auto_now_add=True,
                verbose_name="Created At",
            ),
        ),
        migrations.AddField(
            model_name="webhookconfiguration",
            name="created_by",
            field=models.ForeignKey(
                blank=True,
                null=True,
                on_delete=django.db.models.deletion.SET_NULL,
                to=settings.AUTH_USER_MODEL,
                verbose_name="Created By",
            ),
        ),
        migrations.AddField(
            model_name="webhookconfiguration",
            name="name",
            field=models.CharField(
                max_length=100,
                default="",
                blank=True,
                verbose_name="Configuration Name",
            ),
        ),
        migrations.AddField(
            model_name="webhookconfiguration",
            name="ping_roles",
            field=models.JSONField(
                blank=True,
                default=list,
                verbose_name="Ping Roles",
                help_text="List of role IDs to mention.",
            ),
        ),
        # 6) Champs tax pour AutoLottery et Lottery
        migrations.AddField(
            model_name="autolottery",
            name="tax",
            field=models.DecimalField(
                max_digits=5,
                decimal_places=2,
                default=Decimal("0.00"),
                verbose_name="Tax (%)",
            ),
        ),
        migrations.AddField(
            model_name="autolottery",
            name="tax_amount",
            field=models.DecimalField(
                max_digits=25,
                decimal_places=2,
                default=Decimal("0.00"),
                verbose_name="Tax Amount (ISK)",
            ),
        ),
        migrations.AddField(
            model_name="lottery",
            name="tax",
            field=models.DecimalField(
                max_digits=5,
                decimal_places=2,
                default=Decimal("0.00"),
                verbose_name="Tax (%)",
            ),
        ),
        migrations.AddField(
            model_name="lottery",
            name="tax_amount",
            field=models.DecimalField(
                max_digits=25,
                decimal_places=2,
                default=Decimal("0.00"),
                verbose_name="Tax Amount (ISK)",
            ),
        ),
        # 7) Data migration: winners_distribution → WinnerDistribution
        migrations.RunPython(
            code=forwards_func,
            reverse_code=django.db.migrations.operations.special.RunPython.noop,
        ),
        # 8) Remove obsolete JSONField from Lottery
        migrations.RemoveField(
            model_name="lottery",
            name="winners_distribution",
        ),
        # 9) Other data migrations and cleanup
        migrations.RunPython(
            code=fill_processedpayment_fields,
            reverse_code=django.db.migrations.operations.special.RunPython.noop,
        ),
        migrations.RunPython(
            code=convert_notification_config,
            reverse_code=django.db.migrations.operations.special.RunPython.noop,
        ),
        migrations.RunPython(
            code=set_default_webhook_name,
            reverse_code=django.db.migrations.operations.special.RunPython.noop,
        ),
        migrations.RunPython(
            code=set_empty_detail,
            reverse_code=django.db.migrations.operations.special.RunPython.noop,
        ),
        migrations.RunPython(
            code=fix_ping_roles,
            reverse_code=django.db.migrations.operations.special.RunPython.noop,
        ),
        # 10) Make TicketAnomaly.detail non-nullable
        migrations.AlterField(
            model_name="ticketanomaly",
            name="detail",
            field=models.TextField(default="", verbose_name="Detail"),
        ),
    ]
