# Generated by Django 4.2.23 on 2026-10-12 17:35

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


class Migration(migrations.Migration):

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ('cms', '1008_delete_multimediaasset_alter_service_service_type'),
    ]

    operations = [
        migrations.CreateModel(
            name='BlockedIP',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('ip_address', models.CharField(help_text='IP address, CIDR range or prefix to block', max_length=64, unique=True)),
                ('block_type', models.CharField(choices=[('single', 'Single IP address'), ('cidr', 'CIDR range (e.g. 203.0.113.0/24)'), ('prefix', 'IP prefix (e.g. 203.0.113.)')], default='single', max_length=20)),
                ('reason', models.CharField(blank=True, help_text='Short reason shown in listings', max_length=255)),
                ('source', models.CharField(choices=[('manual', 'Manual'), ('auto', 'Automatic'), ('contact_form', 'Contact form'), ('rate_limit', 'Rate limit')], default='manual', max_length=20)),
                ('notes', models.TextField(blank=True, help_text='Internal notes about this block')),
                ('is_active', models.BooleanField(default=True, help_text='Disable to temporarily allow this address')),
                ('hit_count', models.PositiveIntegerField(default=0, help_text='Number of requests blocked')),
                ('last_hit', models.DateTimeField(blank=True, null=True)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('expires_at', models.DateTimeField(blank=True, help_text='Optional expiry. Leave blank for a permanent block.', null=True)),
            ],
            options={
                'verbose_name': 'Blocked IP',
                'verbose_name_plural': 'Blocked IPs',
                'ordering': ['-created_at'],
            },
        ),
        migrations.CreateModel(
            name='VisitorPageView',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('ip_address', models.GenericIPAddressField(blank=True, null=True)),
                ('path', models.CharField(max_length=500)),
                ('method', models.CharField(default='GET', max_length=8)),
                ('status_code', models.PositiveSmallIntegerField(blank=True, null=True)),
                ('referer', models.CharField(blank=True, max_length=500)),
                ('user_agent', models.TextField(blank=True)),
                ('viewed_at', models.DateTimeField(auto_now_add=True)),
            ],
            options={
                'verbose_name': 'Visitor Page View',
                'verbose_name_plural': 'Visitor Page Views',
                'ordering': ['-viewed_at'],
            },
        ),
        migrations.CreateModel(
            name='VisitorSession',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('session_key', models.CharField(db_index=True, max_length=64, unique=True)),
                ('ip_address', models.GenericIPAddressField(blank=True, null=True)),
                ('ip_version', models.PositiveSmallIntegerField(default=0)),
                ('is_bot', models.BooleanField(default=False)),
                ('is_blocked', models.BooleanField(default=False)),
                ('country_code', models.CharField(blank=True, max_length=2)),
                ('country_name', models.CharField(blank=True, max_length=100)),
                ('region', models.CharField(blank=True, max_length=100)),
                ('city', models.CharField(blank=True, max_length=100)),
                ('postal_code', models.CharField(blank=True, max_length=20)),
                ('latitude', models.FloatField(blank=True, null=True)),
                ('longitude', models.FloatField(blank=True, null=True)),
                ('timezone_name', models.CharField(blank=True, max_length=64)),
                ('isp', models.CharField(blank=True, help_text='Internet service provider', max_length=200)),
                ('org', models.CharField(blank=True, help_text='Organisation / ASN owner', max_length=200)),
                ('asn', models.CharField(blank=True, max_length=50)),
                ('is_proxy', models.BooleanField(default=False)),
                ('is_hosting', models.BooleanField(default=False)),
                ('user_agent', models.TextField(blank=True)),
                ('device_type', models.CharField(blank=True, help_text='desktop, mobile, tablet, bot', max_length=20)),
                ('browser', models.CharField(blank=True, max_length=50)),
                ('os', models.CharField(blank=True, max_length=50)),
                ('referer', models.CharField(blank=True, max_length=500)),
                ('landing_path', models.CharField(blank=True, max_length=500)),
                ('last_path', models.CharField(blank=True, max_length=500)),
                ('region_code', models.CharField(blank=True, max_length=10)),
                ('page_views', models.PositiveIntegerField(default=0)),
                ('first_seen', models.DateTimeField(auto_now_add=True)),
                ('last_seen', models.DateTimeField(default=django.utils.timezone.now)),
            ],
            options={
                'verbose_name': 'Visitor Session',
                'verbose_name_plural': 'Visitor Sessions',
                'ordering': ['-last_seen'],
            },
        ),
        migrations.AddField(
            model_name='contact',
            name='city',
            field=models.CharField(blank=True, max_length=100),
        ),
        migrations.AddField(
            model_name='contact',
            name='country_code',
            field=models.CharField(blank=True, help_text='ISO country code from geolocation', max_length=2),
        ),
        migrations.AddField(
            model_name='contact',
            name='country_name',
            field=models.CharField(blank=True, max_length=100),
        ),
        migrations.AddField(
            model_name='contact',
            name='ip_address',
            field=models.GenericIPAddressField(blank=True, help_text='Submitter IP address', null=True),
        ),
        migrations.AddField(
            model_name='contact',
            name='is_spam',
            field=models.BooleanField(default=False, help_text='Flagged as spam/fake by automatic checks'),
        ),
        migrations.AddField(
            model_name='contact',
            name='spam_reason',
            field=models.CharField(blank=True, help_text='Why the submission was flagged', max_length=255),
        ),
        migrations.AddField(
            model_name='contact',
            name='user_agent',
            field=models.TextField(blank=True, help_text='Raw user agent string'),
        ),
        migrations.AddIndex(
            model_name='contact',
            index=models.Index(fields=['ip_address'], name='cms_contact_ip_addr_1fd7cd_idx'),
        ),
        migrations.AddIndex(
            model_name='contact',
            index=models.Index(fields=['is_spam'], name='cms_contact_is_spam_49ecfe_idx'),
        ),
        migrations.AddIndex(
            model_name='visitorsession',
            index=models.Index(fields=['last_seen'], name='cms_visitor_last_se_6a60d9_idx'),
        ),
        migrations.AddIndex(
            model_name='visitorsession',
            index=models.Index(fields=['ip_address'], name='cms_visitor_ip_addr_8180d4_idx'),
        ),
        migrations.AddIndex(
            model_name='visitorsession',
            index=models.Index(fields=['country_code'], name='cms_visitor_country_3de60b_idx'),
        ),
        migrations.AddIndex(
            model_name='visitorsession',
            index=models.Index(fields=['is_bot'], name='cms_visitor_is_bot_f3daf7_idx'),
        ),
        migrations.AddField(
            model_name='visitorpageview',
            name='session',
            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='pageviews', to='cms.visitorsession'),
        ),
        migrations.AddField(
            model_name='blockedip',
            name='created_by',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='blocked_ips', to=settings.AUTH_USER_MODEL),
        ),
        migrations.AddIndex(
            model_name='visitorpageview',
            index=models.Index(fields=['viewed_at'], name='cms_visitor_viewed__cb5240_idx'),
        ),
        migrations.AddIndex(
            model_name='visitorpageview',
            index=models.Index(fields=['path'], name='cms_visitor_path_cf2c58_idx'),
        ),
        migrations.AddIndex(
            model_name='blockedip',
            index=models.Index(fields=['is_active'], name='cms_blocked_is_acti_70701a_idx'),
        ),
        migrations.AddIndex(
            model_name='blockedip',
            index=models.Index(fields=['ip_address'], name='cms_blocked_ip_addr_94594b_idx'),
        ),
        migrations.AddIndex(
            model_name='blockedip',
            index=models.Index(fields=['expires_at'], name='cms_blocked_expires_a4349f_idx'),
        ),
    ]
