# Generated by Django 4.1.13 on 2026-03-23 07:00

import common.db.fields
from django.db import migrations
from django.db import migrations, models
from common.utils import text_hmac_sha256


user_email_mapper = {}


def get_user_email_mapper(apps, schema_editor):
    User = apps.get_model('users', 'User')
    for user in User.objects.all():
        user_email_mapper[str(user.id)] = user.email

    
def set_user_encrypted_email_and_email_lookup_field(apps, schema_editor):
    User = apps.get_model('users', 'User')
    for user in User.objects.all():
        if str(user.id) in user_email_mapper:
            email = user_email_mapper[str(user.id)]
            user.email = email
            user.email_lookup = text_hmac_sha256(email)
            user.save(update_fields=['email', 'email_lookup'])
        

class Migration(migrations.Migration):

    dependencies = [
        ('users', '0004_fix_user_wechat_phone'),
    ]

    operations = [
        migrations.RunPython(get_user_email_mapper),
        migrations.AlterField(
            model_name='user',
            name='email',
            field=common.db.fields.EncryptCharField(max_length=128, unique=True, verbose_name='Email'),
        ),
        migrations.AddField(
            model_name='user',
            name='email_lookup',
            field=models.CharField(blank=True, max_length=128, null=True, verbose_name='Email lookup'),
        ),
        migrations.RunPython(set_user_encrypted_email_and_email_lookup_field),
    ]
