"""Initial migration for advertisers app.

This migration creates the initial database tables for:
- BrandCategory: Categories for organizing brands
- Agency: Advertising agencies
- Brand: Brand entities managed by agencies
- UserAdvertiser: User access relationships to brands

Generated by Django 4.2.7 on 2024-01-15 10:00:00
"""

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


class Migration(migrations.Migration):
    """Initial migration for advertisers app."""

    initial = True

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        # Create BrandCategory table
        migrations.CreateModel(
            name='BrandCategory',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('created_at', models.DateTimeField(auto_now_add=True, help_text='Timestamp when the record was created')),
                ('updated_at', models.DateTimeField(auto_now=True, help_text='Timestamp when the record was last updated')),
                ('created_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='%(class)s_created', to=settings.AUTH_USER_MODEL)),
                ('updated_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='%(class)s_updated', to=settings.AUTH_USER_MODEL)),
                ('status', models.CharField(choices=[('active', 'Active'), ('inactive', 'Inactive'), ('archived', 'Archived')], default='active', max_length=20)),
                ('name', models.CharField(help_text='Category name', max_length=100, unique=True)),
                ('description', models.TextField(blank=True, help_text='Category description')),
                ('parent', models.ForeignKey(blank=True, help_text='Parent category for hierarchical structure', null=True, on_delete=django.db.models.deletion.CASCADE, related_name='subcategories', to='advertisers.brandcategory')),
                ('sort_order', models.PositiveIntegerField(default=0, help_text='Sort order for display')),
            ],
            options={
                'verbose_name': 'Brand Category',
                'verbose_name_plural': 'Brand Categories',
                'db_table': 'advertisers_brand_category',
                'ordering': ['sort_order', 'name'],
            },
        ),
        
        # Create Agency table
        migrations.CreateModel(
            name='Agency',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('created_at', models.DateTimeField(auto_now_add=True, help_text='Timestamp when the record was created')),
                ('updated_at', models.DateTimeField(auto_now=True, help_text='Timestamp when the record was last updated')),
                ('created_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='%(class)s_created', to=settings.AUTH_USER_MODEL)),
                ('updated_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='%(class)s_updated', to=settings.AUTH_USER_MODEL)),
                ('status', models.CharField(choices=[('active', 'Active'), ('inactive', 'Inactive'), ('archived', 'Archived')], default='active', max_length=20)),
                ('name', models.CharField(help_text='Agency name', max_length=200)),
                ('description', models.TextField(blank=True, help_text='Agency description')),
                ('email', models.EmailField(help_text='Primary contact email', max_length=254, unique=True)),
                ('phone', models.CharField(blank=True, help_text='Contact phone number', max_length=20)),
                ('website', models.URLField(blank=True, help_text='Agency website URL')),
                ('address', models.TextField(blank=True, help_text='Physical address')),
                ('city', models.CharField(blank=True, help_text='City', max_length=100)),
                ('country', models.CharField(blank=True, help_text='Country', max_length=100)),
                ('contact_person', models.CharField(blank=True, help_text='Primary contact person name', max_length=200)),
                ('logo', models.ImageField(blank=True, help_text='Agency logo', null=True, upload_to='agencies/logos/')),
                ('owner', models.ForeignKey(help_text='Agency owner/manager', on_delete=django.db.models.deletion.CASCADE, related_name='owned_agencies', to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'verbose_name': 'Agency',
                'verbose_name_plural': 'Agencies',
                'db_table': 'advertisers_agency',
                'ordering': ['name'],
            },
        ),
        
        # Create Brand table
        migrations.CreateModel(
            name='Brand',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('created_at', models.DateTimeField(auto_now_add=True, help_text='Timestamp when the record was created')),
                ('updated_at', models.DateTimeField(auto_now=True, help_text='Timestamp when the record was last updated')),
                ('created_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='%(class)s_created', to=settings.AUTH_USER_MODEL)),
                ('updated_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='%(class)s_updated', to=settings.AUTH_USER_MODEL)),
                ('status', models.CharField(choices=[('active', 'Active'), ('inactive', 'Inactive'), ('archived', 'Archived')], default='active', max_length=20)),
                ('name', models.CharField(help_text='Brand name', max_length=200)),
                ('description', models.TextField(blank=True, help_text='Brand description')),
                ('industry', models.CharField(blank=True, help_text='Industry sector', max_length=100)),
                ('target_audience', models.TextField(blank=True, help_text='Target audience description')),
                ('annual_budget', models.DecimalField(blank=True, decimal_places=2, help_text='Annual advertising budget', max_digits=15, null=True)),
                ('contact_email', models.EmailField(blank=True, help_text='Brand contact email', max_length=254)),
                ('website', models.URLField(blank=True, help_text='Brand website URL')),
                ('logo', models.ImageField(blank=True, help_text='Brand logo', null=True, upload_to='brands/logos/')),
                ('agency', models.ForeignKey(help_text='Agency managing this brand', on_delete=django.db.models.deletion.CASCADE, related_name='brands', to='advertisers.agency')),
                ('category', models.ForeignKey(blank=True, help_text='Brand category', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='brands', to='advertisers.brandcategory')),
            ],
            options={
                'verbose_name': 'Brand',
                'verbose_name_plural': 'Brands',
                'db_table': 'advertisers_brand',
                'ordering': ['name'],
            },
        ),
        
        # Create UserAdvertiser table
        migrations.CreateModel(
            name='UserAdvertiser',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('created_at', models.DateTimeField(auto_now_add=True, help_text='Timestamp when the record was created')),
                ('updated_at', models.DateTimeField(auto_now=True, help_text='Timestamp when the record was last updated')),
                ('created_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='%(class)s_created', to=settings.AUTH_USER_MODEL)),
                ('updated_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='%(class)s_updated', to=settings.AUTH_USER_MODEL)),
                ('role', models.CharField(choices=[('admin', 'Administrator'), ('editor', 'Editor'), ('viewer', 'Viewer')], default='viewer', help_text='User role for this brand', max_length=20)),
                ('permissions', models.JSONField(blank=True, default=dict, help_text='Custom permissions for this user-brand relationship')),
                ('is_active', models.BooleanField(default=True, help_text='Whether this relationship is active')),
                ('granted_at', models.DateTimeField(auto_now_add=True, help_text='When access was granted')),
                ('expires_at', models.DateTimeField(blank=True, help_text='When access expires (optional)', null=True)),
                ('brand', models.ForeignKey(help_text='Brand the user has access to', on_delete=django.db.models.deletion.CASCADE, related_name='user_relationships', to='advertisers.brand')),
                ('user', models.ForeignKey(help_text='User with access to the brand', on_delete=django.db.models.deletion.CASCADE, related_name='advertiser_relationships', to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'verbose_name': 'User-Advertiser Relationship',
                'verbose_name_plural': 'User-Advertiser Relationships',
                'db_table': 'advertisers_user_advertiser',
                'ordering': ['-granted_at'],
            },
        ),
        
        # Add unique constraints
        migrations.AddConstraint(
            model_name='brand',
            constraint=models.UniqueConstraint(fields=['name', 'agency'], name='unique_brand_per_agency'),
        ),
        migrations.AddConstraint(
            model_name='useradvertiser',
            constraint=models.UniqueConstraint(fields=['user', 'brand'], name='unique_user_brand_relationship'),
        ),
        
        # Add indexes for performance
        migrations.AddIndex(
            model_name='agency',
            index=models.Index(fields=['status', 'created_at'], name='agency_status_created_idx'),
        ),
        migrations.AddIndex(
            model_name='brand',
            index=models.Index(fields=['agency', 'status'], name='brand_agency_status_idx'),
        ),
        migrations.AddIndex(
            model_name='brand',
            index=models.Index(fields=['category', 'status'], name='brand_category_status_idx'),
        ),
        migrations.AddIndex(
            model_name='useradvertiser',
            index=models.Index(fields=['user', 'is_active'], name='user_advertiser_user_active_idx'),
        ),
        migrations.AddIndex(
            model_name='useradvertiser',
            index=models.Index(fields=['brand', 'is_active'], name='user_advertiser_brand_active_idx'),
        ),
    ]