# Generated by Django 5.2.8 on 2025-11-29 21:30

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


class Migration(migrations.Migration):

    dependencies = [
        ('tickets', '0003_ticket_is_sandbox'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name='TicketEmailSettings',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('imap_host', models.CharField(blank=True, default='', help_text='IMAP server hostname (e.g., imap.gmail.com)', max_length=255)),
                ('imap_port', models.IntegerField(default=993, help_text='IMAP server port (993 for SSL, 143 for non-SSL)')),
                ('imap_use_ssl', models.BooleanField(default=True, help_text='Use SSL/TLS connection')),
                ('imap_username', models.CharField(blank=True, default='', help_text='IMAP login username (usually email address)', max_length=255)),
                ('imap_password', models.CharField(blank=True, default='', help_text='IMAP login password or app-specific password', max_length=255)),
                ('imap_folder', models.CharField(default='INBOX', help_text='IMAP folder to monitor for incoming emails', max_length=100)),
                ('support_email_address', models.EmailField(blank=True, default='', help_text='Support email address (e.g., support@yourdomain.com)', max_length=254)),
                ('support_email_domain', models.CharField(blank=True, default='', help_text='Domain for ticket reply addresses (e.g., mail.talova.net)', max_length=255)),
                ('is_enabled', models.BooleanField(default=False, help_text='Enable inbound email processing')),
                ('check_interval_seconds', models.IntegerField(default=60, help_text='How often to check for new emails (in seconds)')),
                ('max_attachment_size_mb', models.IntegerField(default=10, help_text='Maximum attachment size in MB')),
                ('allowed_attachment_types', models.JSONField(blank=True, default=list, help_text='List of allowed attachment MIME types (empty = all allowed)')),
                ('last_check_at', models.DateTimeField(blank=True, help_text='When emails were last checked', 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')),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
            ],
            options={
                'verbose_name': 'Ticket Email Settings',
                'verbose_name_plural': 'Ticket Email Settings',
            },
        ),
        migrations.AddField(
            model_name='ticketcomment',
            name='source',
            field=models.CharField(choices=[('WEB', 'Web Interface'), ('EMAIL', 'Email Reply'), ('API', 'API')], default='WEB', help_text='How this comment was created (web, email, API).', max_length=10),
        ),
        migrations.CreateModel(
            name='IncomingTicketEmail',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('message_id', models.CharField(help_text='Email Message-ID header for deduplication', max_length=255, unique=True)),
                ('from_address', models.EmailField(help_text='Sender email address', max_length=254)),
                ('from_name', models.CharField(blank=True, default='', help_text='Sender display name', max_length=255)),
                ('to_address', models.EmailField(help_text='Recipient email address (our support address)', max_length=254)),
                ('subject', models.CharField(help_text='Email subject line', max_length=500)),
                ('body_text', models.TextField(blank=True, default='', help_text='Plain text email body')),
                ('body_html', models.TextField(blank=True, default='', help_text='HTML email body')),
                ('extracted_reply', models.TextField(blank=True, default='', help_text='Extracted reply text (without quoted content)')),
                ('raw_headers', models.JSONField(blank=True, default=dict, help_text='Raw email headers for debugging')),
                ('ticket_id_from_email', models.CharField(blank=True, default='', help_text='Ticket ID extracted from email address or subject', max_length=50)),
                ('processing_status', models.CharField(choices=[('PENDING', 'Pending'), ('PROCESSED', 'Processed'), ('FAILED', 'Failed'), ('SPAM', 'Marked as Spam'), ('NO_MATCH', 'No Matching Ticket'), ('DUPLICATE', 'Duplicate')], default='PENDING', help_text='Current processing status', max_length=20)),
                ('error_message', models.TextField(blank=True, default='', help_text='Error details if processing failed')),
                ('email_date', models.DateTimeField(help_text='Date from email headers')),
                ('received_at', models.DateTimeField(auto_now_add=True, help_text='When we received/processed this email')),
                ('processed_at', models.DateTimeField(blank=True, help_text='When processing completed', null=True)),
                ('matched_user', models.ForeignKey(blank=True, help_text='User matched by email address', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='incoming_ticket_emails', to=settings.AUTH_USER_MODEL)),
                ('ticket', models.ForeignKey(blank=True, help_text='Matched ticket (if found)', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='incoming_emails', to='tickets.ticket')),
            ],
            options={
                'ordering': ['-received_at'],
            },
        ),
        migrations.AddField(
            model_name='ticketcomment',
            name='incoming_email',
            field=models.ForeignKey(blank=True, help_text='The incoming email that created this comment.', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='created_comments', to='tickets.incomingticketemail'),
        ),
        migrations.AddIndex(
            model_name='incomingticketemail',
            index=models.Index(fields=['message_id'], name='tickets_inc_message_b2c788_idx'),
        ),
        migrations.AddIndex(
            model_name='incomingticketemail',
            index=models.Index(fields=['from_address'], name='tickets_inc_from_ad_989278_idx'),
        ),
        migrations.AddIndex(
            model_name='incomingticketemail',
            index=models.Index(fields=['processing_status'], name='tickets_inc_process_489f0d_idx'),
        ),
        migrations.AddIndex(
            model_name='incomingticketemail',
            index=models.Index(fields=['ticket'], name='tickets_inc_ticket__a2c3b3_idx'),
        ),
    ]
