# Generated by Django 5.2.8 on 2025-11-28 18:54

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


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        ('core', '0007_add_tenant_permissions'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name='APIToken',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('name', models.CharField(help_text='Human-readable name for identifying this token', max_length=100)),
                ('key_hash', models.CharField(db_index=True, help_text='SHA-256 hash of the token key', max_length=64, unique=True)),
                ('key_prefix', models.CharField(help_text='Prefix of the key for identification (e.g., ss_live_a1b2)', max_length=16)),
                ('scopes', models.JSONField(default=list, help_text='List of permission scopes granted to this token')),
                ('is_active', models.BooleanField(default=True, help_text='Whether this token is currently active')),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('last_used_at', models.DateTimeField(blank=True, help_text='When the token was last used for authentication', null=True)),
                ('expires_at', models.DateTimeField(blank=True, help_text='Optional expiration date for the token', null=True)),
                ('rate_limit_override', models.PositiveIntegerField(blank=True, help_text='Custom rate limit (requests/hour) if different from default', null=True)),
                ('created_by', models.ForeignKey(blank=True, help_text='User who created this token', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='created_api_tokens', to=settings.AUTH_USER_MODEL)),
                ('tenant', models.ForeignKey(help_text='The business this token belongs to', on_delete=django.db.models.deletion.CASCADE, related_name='api_tokens', to='core.tenant')),
            ],
            options={
                'verbose_name': 'API Token',
                'verbose_name_plural': 'API Tokens',
                'ordering': ['-created_at'],
            },
        ),
        migrations.CreateModel(
            name='WebhookSubscription',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('url', models.URLField(help_text='The HTTPS URL to send webhook payloads to', max_length=2048)),
                ('secret', models.CharField(help_text='Secret key for HMAC-SHA256 signature verification', max_length=64)),
                ('events', models.JSONField(default=list, help_text='List of event types to subscribe to')),
                ('is_active', models.BooleanField(default=True, help_text='Whether this subscription is currently active')),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('failure_count', models.PositiveIntegerField(default=0, help_text='Number of consecutive delivery failures')),
                ('last_triggered_at', models.DateTimeField(blank=True, help_text='When a webhook was last sent', null=True)),
                ('last_success_at', models.DateTimeField(blank=True, help_text='When a webhook was last successfully delivered', null=True)),
                ('last_failure_at', models.DateTimeField(blank=True, help_text='When a webhook last failed to deliver', null=True)),
                ('description', models.TextField(blank=True, help_text='Optional description of what this webhook is for')),
                ('api_token', models.ForeignKey(help_text='The API token that owns this subscription', on_delete=django.db.models.deletion.CASCADE, related_name='webhook_subscriptions', to='public_api.apitoken')),
                ('tenant', models.ForeignKey(help_text='The business this webhook belongs to', on_delete=django.db.models.deletion.CASCADE, related_name='webhook_subscriptions', to='core.tenant')),
            ],
            options={
                'verbose_name': 'Webhook Subscription',
                'verbose_name_plural': 'Webhook Subscriptions',
                'ordering': ['-created_at'],
            },
        ),
        migrations.CreateModel(
            name='WebhookDelivery',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('event_type', models.CharField(help_text='The type of event', max_length=50)),
                ('event_id', models.CharField(help_text='Unique identifier for this event instance', max_length=64)),
                ('payload', models.JSONField(help_text='The JSON payload sent to the webhook URL')),
                ('response_status', models.PositiveIntegerField(blank=True, help_text='HTTP status code received', null=True)),
                ('response_body', models.TextField(blank=True, help_text='Response body (truncated to 10KB)')),
                ('delivered_at', models.DateTimeField(blank=True, help_text='When the webhook was successfully delivered', null=True)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('success', models.BooleanField(default=False, help_text='Whether the delivery was successful')),
                ('retry_count', models.PositiveIntegerField(default=0, help_text='Number of retry attempts made')),
                ('next_retry_at', models.DateTimeField(blank=True, help_text='When the next retry will be attempted', null=True)),
                ('error_message', models.TextField(blank=True, help_text='Error message if delivery failed')),
                ('subscription', models.ForeignKey(help_text='The subscription this delivery is for', on_delete=django.db.models.deletion.CASCADE, related_name='deliveries', to='public_api.webhooksubscription')),
            ],
            options={
                'verbose_name': 'Webhook Delivery',
                'verbose_name_plural': 'Webhook Deliveries',
                'ordering': ['-created_at'],
                'indexes': [models.Index(fields=['subscription', '-created_at'], name='public_api__subscri_6964d3_idx'), models.Index(fields=['event_type', '-created_at'], name='public_api__event_t_bb35c8_idx'), models.Index(fields=['success', 'next_retry_at'], name='public_api__success_06dadf_idx')],
            },
        ),
    ]
