# Generated by Django 5.1.11 on 2025-09-16 00:10

import django.db.models.deletion
import uuid
from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('core', '0008_channel_message_channel_channel_channel_has_target'),
    ]

    operations = [
        migrations.CreateModel(
            name='MessageTemplate',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, help_text='primary key for the record as UUID', primary_key=True, serialize=False, verbose_name='id')),
                ('created_at', models.DateTimeField(auto_now_add=True, help_text='date and time at which a record was created', verbose_name='created on')),
                ('updated_at', models.DateTimeField(auto_now=True, help_text='date and time at which a record was last updated', verbose_name='updated on')),
                ('name', models.CharField(help_text="Name of the template (e.g., 'Standard Reply', 'Out of Office', 'Work Signature')", max_length=255, verbose_name='name')),
                ('type', models.SmallIntegerField(choices=[(1, 'reply'), (2, 'new_message'), (3, 'signature')], default=1, help_text='Type of template (reply, new_message, signature)', verbose_name='type')),
                ('is_active', models.BooleanField(default=True, help_text='Whether this template is available for use', verbose_name='is active')),
                ('is_forced', models.BooleanField(default=False, help_text='Whether this template is forced; no other template of the same type can be used in the same scope', verbose_name='is forced')),
            ],
            options={
                'verbose_name': 'message template',
                'verbose_name_plural': 'message templates',
                'db_table': 'messages_messagetemplate',
                'ordering': ['-created_at'],
            },
        ),
        migrations.AddField(
            model_name='blob',
            name='maildomain',
            field=models.ForeignKey(blank=True, help_text='Mail domain that owns this blob', null=True, on_delete=django.db.models.deletion.CASCADE, related_name='blobs', to='core.maildomain'),
        ),
        migrations.AlterField(
            model_name='blob',
            name='mailbox',
            field=models.ForeignKey(blank=True, help_text='Mailbox that owns this blob', null=True, on_delete=django.db.models.deletion.CASCADE, related_name='blobs', to='core.mailbox'),
        ),
        migrations.AlterField(
            model_name='message',
            name='draft_blob',
            field=models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='draft', to='core.blob'),
        ),
        migrations.AddConstraint(
            model_name='blob',
            constraint=models.CheckConstraint(condition=models.Q(('mailbox__isnull', False), ('maildomain__isnull', False), _connector='OR'), name='blob_has_owner'),
        ),
        migrations.AddField(
            model_name='messagetemplate',
            name='blob',
            field=models.ForeignKey(blank=True, help_text='Reference to the blob containing template content as JSON: {html: str, text: str, raw: any}', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='message_templates', to='core.blob'),
        ),
        migrations.AddField(
            model_name='messagetemplate',
            name='mailbox',
            field=models.ForeignKey(blank=True, help_text='Mailbox that can use this template', null=True, on_delete=django.db.models.deletion.CASCADE, related_name='message_templates', to='core.mailbox'),
        ),
        migrations.AddField(
            model_name='messagetemplate',
            name='maildomain',
            field=models.ForeignKey(blank=True, help_text='Mail domain that can use this template', null=True, on_delete=django.db.models.deletion.CASCADE, related_name='message_templates', to='core.maildomain'),
        ),
        migrations.AddField(
            model_name='message',
            name='signature',
            field=models.ForeignKey(blank=True, help_text='Signature template for the message', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='messages', to='core.messagetemplate'),
        ),
        migrations.AddIndex(
            model_name='messagetemplate',
            index=models.Index(fields=['mailbox', 'type', 'is_active'], name='messages_me_mailbox_00a4d4_idx'),
        ),
        migrations.AddIndex(
            model_name='messagetemplate',
            index=models.Index(fields=['maildomain', 'type', 'is_active'], name='messages_me_maildom_4a0e75_idx'),
        ),
        migrations.AddConstraint(
            model_name='messagetemplate',
            constraint=models.CheckConstraint(condition=models.Q(('mailbox__isnull', False), ('maildomain__isnull', False), _connector='OR'), name='messagetemplate_has_owner'),
        ),
        migrations.AddConstraint(
            model_name='messagetemplate',
            constraint=models.UniqueConstraint(condition=models.Q(('is_forced', True)), fields=('mailbox', 'type'), name='uniq_forced_template_mailbox_type'),
        ),
        migrations.AddConstraint(
            model_name='messagetemplate',
            constraint=models.UniqueConstraint(condition=models.Q(('is_forced', True)), fields=('maildomain', 'type'), name='uniq_forced_template_maildomain_type'),
        ),
    ]
