# Generated by Django 4.2.20 on 2025-10-31 08:21

from django.conf import settings
import django.core.validators
from django.db import migrations, models
import django.db.models.deletion
import re
import unicrm.models


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ('unicom', '0017_add_chat_metadata'),
    ]

    operations = [
        migrations.CreateModel(
            name='Company',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('created_at', models.DateTimeField(auto_now_add=True, verbose_name='Created at')),
                ('updated_at', models.DateTimeField(auto_now=True, verbose_name='Updated at')),
                ('name', models.CharField(max_length=255, verbose_name='Name')),
                ('domain', models.CharField(blank=True, help_text='Primary email domain for this company (optional)', max_length=255, verbose_name='Email domain')),
                ('website', models.URLField(blank=True, verbose_name='Website')),
                ('notes', models.TextField(blank=True, verbose_name='Internal notes')),
            ],
            options={
                'verbose_name_plural': 'Companies',
                'ordering': ('name',),
            },
        ),
        migrations.CreateModel(
            name='Contact',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('created_at', models.DateTimeField(auto_now_add=True, verbose_name='Created at')),
                ('updated_at', models.DateTimeField(auto_now=True, verbose_name='Updated at')),
                ('first_name', models.CharField(blank=True, max_length=150, verbose_name='First name')),
                ('last_name', models.CharField(blank=True, max_length=150, verbose_name='Last name')),
                ('email', models.EmailField(max_length=254, unique=True, verbose_name='Email')),
                ('phone_number', models.CharField(blank=True, max_length=50, verbose_name='Phone number')),
                ('job_title', models.CharField(blank=True, max_length=150, verbose_name='Job title')),
                ('attributes', models.JSONField(blank=True, default=dict, help_text='Arbitrary data map available to template variables.', verbose_name='Custom attributes')),
                ('company', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='contacts', to='unicrm.company')),
                ('owner', models.ForeignKey(blank=True, help_text='Optional staff owner responsible for this contact.', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='owned_contacts', to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'ordering': ('last_name', 'first_name'),
            },
        ),
        migrations.CreateModel(
            name='MailingList',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('created_at', models.DateTimeField(auto_now_add=True, verbose_name='Created at')),
                ('updated_at', models.DateTimeField(auto_now=True, verbose_name='Updated at')),
                ('name', models.CharField(max_length=255, unique=True, verbose_name='Name')),
                ('slug', models.SlugField(help_text='URL-friendly identifier for public subscription management.', max_length=255, unique=True, validators=[django.core.validators.RegexValidator(re.compile('^[-a-zA-Z0-9_]+\\Z'), 'Enter a valid “slug” consisting of letters, numbers, underscores or hyphens.', 'invalid')], verbose_name='Slug')),
                ('description', models.TextField(blank=True, verbose_name='Description')),
            ],
            options={
                'ordering': ('name',),
            },
        ),
        migrations.CreateModel(
            name='Segment',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('created_at', models.DateTimeField(auto_now_add=True, verbose_name='Created at')),
                ('updated_at', models.DateTimeField(auto_now=True, verbose_name='Updated at')),
                ('name', models.CharField(max_length=255, unique=True, verbose_name='Name')),
                ('description', models.TextField(blank=True, verbose_name='Description')),
                ('code', models.TextField(default=unicrm.models._default_segment_code, help_text='Python snippet with a function `apply(qs)` returning a filtered QuerySet. The `qs` argument contains `unicrm.Contact` records.', verbose_name='Filter code')),
            ],
            options={
                'ordering': ('name',),
            },
        ),
        migrations.CreateModel(
            name='TemplateVariable',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('created_at', models.DateTimeField(auto_now_add=True, verbose_name='Created at')),
                ('updated_at', models.DateTimeField(auto_now=True, verbose_name='Updated at')),
                ('key', models.CharField(help_text='Slug-like identifier used in templates, e.g. "contact_first_name".', max_length=100, unique=True, verbose_name='Key')),
                ('label', models.CharField(help_text='Human-readable label for editors.', max_length=255, verbose_name='Label')),
                ('description', models.TextField(help_text='Explain exactly what data this variable returns.', verbose_name='Description')),
                ('code', models.TextField(default=unicrm.models._default_template_variable_code, help_text='Python snippet defining `compute(contact)` which returns a string value.', verbose_name='Code')),
                ('is_active', models.BooleanField(default=True, verbose_name='Is active')),
            ],
            options={
                'ordering': ('key',),
            },
        ),
        migrations.CreateModel(
            name='Communication',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('created_at', models.DateTimeField(auto_now_add=True, verbose_name='Created at')),
                ('updated_at', models.DateTimeField(auto_now=True, verbose_name='Updated at')),
                ('scheduled_for', models.DateTimeField(blank=True, null=True, verbose_name='Scheduled for')),
                ('status', models.CharField(choices=[('draft', 'Draft'), ('scheduled', 'Scheduled'), ('sending', 'Sending'), ('completed', 'Completed'), ('cancelled', 'Cancelled')], default='draft', max_length=20, verbose_name='Status')),
                ('status_summary', models.JSONField(blank=True, default=dict, help_text='Aggregated counters for linked messages (e.g. sent, delivered, failed).', verbose_name='Status summary')),
                ('initiated_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='initiated_communications', to=settings.AUTH_USER_MODEL)),
                ('segment', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='communications', to='unicrm.segment')),
                ('template', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='unicrm_communications', to='unicom.messagetemplate')),
            ],
            options={
                'ordering': ('-created_at',),
            },
        ),
        migrations.CreateModel(
            name='Subscription',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('created_at', models.DateTimeField(auto_now_add=True, verbose_name='Created at')),
                ('updated_at', models.DateTimeField(auto_now=True, verbose_name='Updated at')),
                ('subscribed_at', models.DateTimeField(auto_now_add=True, verbose_name='Subscribed at')),
                ('unsubscribed_at', models.DateTimeField(blank=True, null=True, verbose_name='Unsubscribed at')),
                ('unsubscribe_feedback', models.TextField(blank=True, verbose_name='Unsubscribe feedback')),
                ('contact', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='subscriptions', to='unicrm.contact')),
                ('mailing_list', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='subscriptions', to='unicrm.mailinglist')),
            ],
            options={
                'ordering': ('-subscribed_at',),
                'unique_together': {('contact', 'mailing_list')},
            },
        ),
        migrations.CreateModel(
            name='CommunicationMessage',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('created_at', models.DateTimeField(auto_now_add=True, verbose_name='Created at')),
                ('updated_at', models.DateTimeField(auto_now=True, verbose_name='Updated at')),
                ('metadata', models.JSONField(blank=True, default=dict, verbose_name='Metadata')),
                ('communication', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='messages', to='unicrm.communication')),
                ('contact', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='communications', to='unicrm.contact')),
                ('message', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='unicrm_links', to='unicom.message')),
            ],
            options={
                'ordering': ('-created_at',),
                'unique_together': {('communication', 'contact', 'message')},
            },
        ),
    ]
