# Generated by Django 5.2.8 on 2025-12-02 02:56

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


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        ('core', '0011_tenant_twilio_phone_number_and_more'),
    ]

    operations = [
        migrations.CreateModel(
            name='CommunicationCredits',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('balance_cents', models.IntegerField(default=0, help_text='Current credit balance in cents')),
                ('auto_reload_enabled', models.BooleanField(default=False, help_text='Automatically reload credits when balance falls below threshold')),
                ('auto_reload_threshold_cents', models.IntegerField(default=1000, help_text='Reload when balance falls below this amount (cents)')),
                ('auto_reload_amount_cents', models.IntegerField(default=2500, help_text='Amount to reload (cents)')),
                ('low_balance_warning_cents', models.IntegerField(default=500, help_text='Send warning email when balance falls below this amount (cents)')),
                ('low_balance_warning_sent', models.BooleanField(default=False, help_text='Whether low balance warning has been sent (reset on reload)')),
                ('low_balance_warning_sent_at', models.DateTimeField(blank=True, help_text='When the last low balance warning was sent', null=True)),
                ('stripe_payment_method_id', models.CharField(blank=True, default='', help_text='Stripe Payment Method ID for auto-reload', max_length=255)),
                ('last_twilio_sync_at', models.DateTimeField(blank=True, help_text='When usage was last synced from Twilio', null=True)),
                ('twilio_sync_period_start', models.DateField(blank=True, help_text='Start of current Twilio billing period', null=True)),
                ('twilio_raw_usage_cents', models.IntegerField(default=0, help_text='Raw Twilio cost for current period (cents)')),
                ('billed_usage_cents', models.IntegerField(default=0, help_text='Amount already deducted from credits for current period (cents)')),
                ('total_loaded_cents', models.IntegerField(default=0, help_text='Total credits ever loaded (cents)')),
                ('total_spent_cents', models.IntegerField(default=0, help_text='Total credits ever spent (cents)')),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('tenant', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='communication_credits', to='core.tenant')),
            ],
            options={
                'verbose_name': 'Communication Credits',
                'verbose_name_plural': 'Communication Credits',
            },
        ),
        migrations.CreateModel(
            name='ProxyPhoneNumber',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('phone_number', models.CharField(help_text='E.164 format phone number', max_length=20, unique=True)),
                ('twilio_sid', models.CharField(help_text='Twilio Phone Number SID', max_length=50)),
                ('status', models.CharField(choices=[('available', 'Available'), ('assigned', 'Assigned to Tenant'), ('reserved', 'Reserved for Session'), ('inactive', 'Inactive')], default='available', max_length=20)),
                ('assigned_at', models.DateTimeField(blank=True, null=True)),
                ('monthly_fee_cents', models.IntegerField(default=200, help_text='Monthly fee charged to tenant (cents)')),
                ('last_billed_at', models.DateTimeField(blank=True, help_text='When this number was last billed', null=True)),
                ('friendly_name', models.CharField(blank=True, default='', help_text='Friendly name for the number', max_length=100)),
                ('capabilities', models.JSONField(blank=True, default=dict, help_text='Twilio capabilities (voice, sms, mms)')),
                ('is_active', models.BooleanField(default=True)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('assigned_tenant', models.ForeignKey(blank=True, help_text='Tenant this number is assigned to', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='proxy_phone_numbers', to='core.tenant')),
            ],
            options={
                'verbose_name': 'Proxy Phone Number',
                'verbose_name_plural': 'Proxy Phone Numbers',
                'ordering': ['phone_number'],
            },
        ),
        migrations.CreateModel(
            name='CreditTransaction',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('amount_cents', models.IntegerField(help_text='Amount in cents (positive=credit, negative=debit)')),
                ('balance_after_cents', models.IntegerField(help_text='Balance after this transaction (cents)')),
                ('transaction_type', models.CharField(choices=[('manual', 'Manual Top-up'), ('auto_reload', 'Auto Reload'), ('usage', 'Usage'), ('refund', 'Refund'), ('adjustment', 'Adjustment'), ('promo', 'Promotional Credit')], default='usage', max_length=20)),
                ('description', models.CharField(blank=True, default='', max_length=255)),
                ('reference_type', models.CharField(blank=True, default='', help_text='Type: sms, voice, proxy_number, etc.', max_length=50)),
                ('reference_id', models.CharField(blank=True, default='', help_text='External reference (Twilio SID, etc.)', max_length=100)),
                ('stripe_charge_id', models.CharField(blank=True, default='', help_text='Stripe Charge/PaymentIntent ID', max_length=255)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('credits', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='transactions', to='comms_credits.communicationcredits')),
            ],
            options={
                'ordering': ['-created_at'],
                'indexes': [models.Index(fields=['credits', '-created_at'], name='comms_credi_credits_ae1f83_idx'), models.Index(fields=['transaction_type', '-created_at'], name='comms_credi_transac_c0fc69_idx')],
            },
        ),
        migrations.CreateModel(
            name='MaskedSession',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('event_id', models.IntegerField(blank=True, help_text='ID of the Event this session is for (in tenant schema)', null=True)),
                ('customer_phone', models.CharField(help_text="Customer's real phone number", max_length=20)),
                ('staff_phone', models.CharField(help_text="Staff member's real phone number", max_length=20)),
                ('status', models.CharField(choices=[('active', 'Active'), ('closed', 'Closed'), ('expired', 'Expired')], default='active', max_length=20)),
                ('expires_at', models.DateTimeField(help_text='When this session automatically expires')),
                ('closed_at', models.DateTimeField(blank=True, null=True)),
                ('sms_count', models.IntegerField(default=0, help_text='Number of SMS messages sent through this session')),
                ('voice_seconds', models.IntegerField(default=0, help_text='Total voice call duration in seconds')),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('tenant', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='masked_sessions', to='core.tenant')),
                ('proxy_number', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='sessions', to='comms_credits.proxyphonenumber')),
            ],
            options={
                'ordering': ['-created_at'],
                'indexes': [models.Index(fields=['tenant', 'status'], name='comms_credi_tenant__9ddf1c_idx'), models.Index(fields=['proxy_number', 'status'], name='comms_credi_proxy_n_e0fe91_idx'), models.Index(fields=['expires_at'], name='comms_credi_expires_6dd7c7_idx')],
            },
        ),
    ]
