# Generated by Django 5.2.2 on 2025-07-29 13:15

from django.db import migrations
from integrations.imports import helpers, simkl
import json

def migrate_simkl_tasks(apps, schema_editor):
    PeriodicTask = apps.get_model('django_celery_beat', 'PeriodicTask')
    tasks = PeriodicTask.objects.filter(name__startswith='Import from SIMKL')
    for task in tasks:
        # Update the task name to match the new import function
        kwargs = json.loads(task.kwargs)
        if hasattr(kwargs, "token"):
            continue
        kwargs["token"]= kwargs["username"]
        kwargs["username"]=simkl.get_username(helpers.decrypt(kwargs["token"]))
        task.name = task.name.replace(kwargs["token"], kwargs["username"])
        task.kwargs = json.dumps(kwargs)
        task.save()

class Migration(migrations.Migration):

    dependencies = [
        ('app', '0050_fix_null_history_users'),
    ]

    operations = [
        migrations.RunPython(migrate_simkl_tasks, reverse_code=migrations.RunPython.noop),
    ]