# Generated by Django 5.2.1 on 2025-06-20 19:40

import uuid

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

import django_attribution.models


class Migration(migrations.Migration):
    initial = True

    dependencies = [
        ("contenttypes", "0002_remove_content_type_name"),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name="Identity",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("uuid", models.UUIDField(default=uuid.uuid4, editable=False)),
                (
                    "created_at",
                    models.DateTimeField(
                        db_index=True, default=django.utils.timezone.now
                    ),
                ),
                ("updated_at", models.DateTimeField(auto_now=True, db_index=True)),
                ("is_active", models.BooleanField(db_index=True, default=True)),
                ("first_visit_user_agent", models.TextField(blank=True)),
                (
                    "linked_user",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="attribution_identities",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "merged_into",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="merged_identities",
                        to="django_attribution.identity",
                    ),
                ),
            ],
            options={
                "verbose_name_plural": "Identities",
            },
        ),
        migrations.CreateModel(
            name="Conversion",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("uuid", models.UUIDField(default=uuid.uuid4, editable=False)),
                (
                    "created_at",
                    models.DateTimeField(
                        db_index=True, default=django.utils.timezone.now
                    ),
                ),
                ("updated_at", models.DateTimeField(auto_now=True, db_index=True)),
                ("is_active", models.BooleanField(db_index=True, default=True)),
                ("event", models.CharField(db_index=True, max_length=255)),
                (
                    "conversion_value",
                    models.DecimalField(
                        blank=True, decimal_places=2, max_digits=12, null=True
                    ),
                ),
                (
                    "currency",
                    models.CharField(
                        blank=True,
                        default=django_attribution.models.get_default_currency,
                        max_length=3,
                    ),
                ),
                ("custom_data", models.JSONField(blank=True, default=dict)),
                (
                    "source_object_id",
                    models.PositiveIntegerField(blank=True, null=True),
                ),
                ("is_confirmed", models.BooleanField(default=True)),
                (
                    "source_content_type",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="conversions_as_source_object",
                        to="contenttypes.contenttype",
                    ),
                ),
                (
                    "identity",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="conversions",
                        to="django_attribution.identity",
                    ),
                ),
            ],
            options={
                "ordering": ["-created_at"],
            },
        ),
        migrations.CreateModel(
            name="Touchpoint",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("uuid", models.UUIDField(default=uuid.uuid4, editable=False)),
                (
                    "created_at",
                    models.DateTimeField(
                        db_index=True, default=django.utils.timezone.now
                    ),
                ),
                ("updated_at", models.DateTimeField(auto_now=True, db_index=True)),
                ("is_active", models.BooleanField(db_index=True, default=True)),
                ("url", models.URLField(max_length=2048)),
                ("referrer", models.URLField(blank=True, max_length=2048)),
                (
                    "utm_source",
                    models.CharField(blank=True, db_index=True, max_length=255),
                ),
                (
                    "utm_medium",
                    models.CharField(blank=True, db_index=True, max_length=255),
                ),
                (
                    "utm_campaign",
                    models.CharField(blank=True, db_index=True, max_length=255),
                ),
                ("utm_term", models.CharField(blank=True, max_length=255)),
                ("utm_content", models.CharField(blank=True, max_length=255)),
                ("fbclid", models.CharField(blank=True, max_length=255)),
                ("gclid", models.CharField(blank=True, max_length=255)),
                ("msclkid", models.CharField(blank=True, max_length=255)),
                ("ttclid", models.CharField(blank=True, max_length=255)),
                ("li_fat_id", models.CharField(blank=True, max_length=255)),
                ("twclid", models.CharField(blank=True, max_length=255)),
                ("igshid", models.CharField(blank=True, max_length=255)),
                (
                    "identity",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="touchpoints",
                        to="django_attribution.identity",
                    ),
                ),
            ],
            options={
                "ordering": ["-created_at"],
            },
        ),
        migrations.AddIndex(
            model_name="identity",
            index=models.Index(
                fields=["created_at"], name="django_attr_created_440cba_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="identity",
            index=models.Index(
                fields=["linked_user", "merged_into", "created_at"],
                name="django_attr_linked__56cc41_idx",
            ),
        ),
        migrations.AddConstraint(
            model_name="identity",
            constraint=models.UniqueConstraint(
                condition=models.Q(("merged_into__isnull", True)),
                fields=("linked_user",),
                name="unique_canonical_identity_per_user",
            ),
        ),
        migrations.AddConstraint(
            model_name="identity",
            constraint=models.CheckConstraint(
                condition=models.Q(("merged_into", models.F("id")), _negated=True),
                name="prevent_self_merge",
            ),
        ),
        migrations.AddIndex(
            model_name="conversion",
            index=models.Index(
                fields=["identity", "created_at"], name="django_attr_identit_f10e06_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="conversion",
            index=models.Index(
                fields=["event", "created_at"], name="django_attr_event_e2d18c_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="conversion",
            index=models.Index(
                fields=["source_content_type", "source_object_id"],
                name="django_attr_source__9fdd48_idx",
            ),
        ),
        migrations.AddIndex(
            model_name="touchpoint",
            index=models.Index(
                fields=["identity", "created_at"], name="django_attr_identit_102964_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="touchpoint",
            index=models.Index(
                fields=["utm_source", "utm_medium"],
                name="django_attr_utm_sou_f9e901_idx",
            ),
        ),
        migrations.AddIndex(
            model_name="touchpoint",
            index=models.Index(
                fields=["utm_campaign", "utm_source", "created_at"],
                name="django_attr_utm_cam_b95d45_idx",
            ),
        ),
    ]
