# Generated by Django 5.1.5 on 2025-02-07 16:13

import django.db.models.functions.text
from django.db import migrations, models


def fill_dn_email(apps, schema_editor):
    """Fill the dn_email field with the email field."""
    Mailbox = apps.get_model("mailbox_manager", "Mailbox")

    for mailbox in Mailbox.objects.select_related("domain").filter(
        models.Q(dn_email="") | models.Q(dn_email__isnull=True)
    ):
        # quite naive but we don't have many data
        mailbox.dn_email = f"{mailbox.local_part}@{mailbox.domain.name}"
        mailbox.save()


class Migration(migrations.Migration):

    dependencies = [
        ('mailbox_manager', '0022_maildomain_organization'),
    ]

    operations = [
        migrations.AddField(
            model_name='mailbox',
            name='last_login',
            field=models.DateTimeField(blank=True, null=True, verbose_name='last login'),
        ),
        migrations.AddField(
            model_name='mailbox',
            name='password',
            field=models.CharField(default='', max_length=128, verbose_name='password'),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name='mailbox',
            name='dn_email',
            field=models.EmailField(blank=True, editable=False, max_length=254, null=True, unique=True,
                                    verbose_name='email'),
        ),
        migrations.RunPython(
            code=fill_dn_email,
            reverse_code=migrations.RunPython.noop,
        ),
        migrations.AlterField(
            model_name='mailbox',
            name='dn_email',
            field=models.EmailField(blank=True, editable=False, max_length=254, unique=True, verbose_name='email'),
        ),
    ]
