# Generated by Django 5.2.8 on 2025-12-01 16:54

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


class Migration(migrations.Migration):

    dependencies = [
        ('core', '0010_add_oauth_credential_model'),
        ('tickets', '0009_add_oauth_credential_to_email_settings'),
    ]

    operations = [
        migrations.CreateModel(
            name='TicketEmailAddress',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('display_name', models.CharField(help_text="Display name (e.g., 'Support', 'Billing', 'Sales')", max_length=100)),
                ('email_address', models.EmailField(help_text='Email address for sending/receiving tickets', max_length=254)),
                ('color', models.CharField(default='#3b82f6', help_text='Hex color code for visual identification (e.g., #3b82f6)', max_length=7)),
                ('imap_host', models.CharField(help_text='IMAP server hostname (e.g., imap.gmail.com)', max_length=255)),
                ('imap_port', models.IntegerField(default=993, help_text='IMAP port (993 for SSL, 143 for non-SSL)')),
                ('imap_use_ssl', models.BooleanField(default=True, help_text='Use SSL/TLS for IMAP')),
                ('imap_username', models.CharField(help_text='IMAP username (usually email address)', max_length=255)),
                ('imap_password', models.CharField(help_text='IMAP password or app-specific password', max_length=255)),
                ('imap_folder', models.CharField(default='INBOX', help_text='IMAP folder to monitor', max_length=100)),
                ('smtp_host', models.CharField(help_text='SMTP server hostname (e.g., smtp.gmail.com)', max_length=255)),
                ('smtp_port', models.IntegerField(default=587, help_text='SMTP port (587 for TLS, 465 for SSL)')),
                ('smtp_use_tls', models.BooleanField(default=True, help_text='Use STARTTLS for SMTP')),
                ('smtp_use_ssl', models.BooleanField(default=False, help_text='Use SSL/TLS for SMTP (port 465)')),
                ('smtp_username', models.CharField(help_text='SMTP username (usually email address)', max_length=255)),
                ('smtp_password', models.CharField(help_text='SMTP password or app-specific password', max_length=255)),
                ('is_active', models.BooleanField(default=True, help_text='Whether this email address is actively checked')),
                ('is_default', models.BooleanField(default=False, help_text='Default email for new tickets in this business')),
                ('last_check_at', models.DateTimeField(blank=True, help_text='When emails were last checked for this address', null=True)),
                ('last_error', models.TextField(blank=True, default='', help_text='Last error message if any')),
                ('emails_processed_count', models.IntegerField(default=0, help_text='Total number of emails processed for this address')),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('tenant', models.ForeignKey(help_text='Business this email address belongs to', on_delete=django.db.models.deletion.CASCADE, related_name='ticket_email_addresses', to='core.tenant')),
            ],
            options={
                'verbose_name': 'Ticket Email Address',
                'verbose_name_plural': 'Ticket Email Addresses',
                'ordering': ['-is_default', 'display_name'],
            },
        ),
        migrations.AddField(
            model_name='incomingticketemail',
            name='email_address',
            field=models.ForeignKey(blank=True, help_text='Email address configuration that received this email', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='incoming_emails', to='tickets.ticketemailaddress'),
        ),
        migrations.AddField(
            model_name='ticket',
            name='source_email_address',
            field=models.ForeignKey(blank=True, help_text='Email address this ticket was received from or sent to', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='tickets', to='tickets.ticketemailaddress'),
        ),
        migrations.AddIndex(
            model_name='ticketemailaddress',
            index=models.Index(fields=['tenant', 'is_active'], name='tickets_tic_tenant__b4c1e5_idx'),
        ),
        migrations.AddIndex(
            model_name='ticketemailaddress',
            index=models.Index(fields=['email_address'], name='tickets_tic_email_a_e2aa4a_idx'),
        ),
        migrations.AlterUniqueTogether(
            name='ticketemailaddress',
            unique_together={('tenant', 'email_address')},
        ),
    ]
