# Generated by Django 5.0.14 on 2025-10-24 19:37

import uuid

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


class Migration(migrations.Migration):

    initial = True

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

    operations = [
        migrations.CreateModel(
            name="ReferralAction",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                (
                    "action_type",
                    models.CharField(
                        choices=[
                            ("click", "CLICK"),
                            ("signup", "SIGNUP"),
                            ("purchase", "PURCHASE"),
                            ("other", "OTHER"),
                        ],
                        max_length=20,
                    ),
                ),
                ("ip_address", models.GenericIPAddressField(blank=True, null=True)),
                ("user_agent", models.TextField(blank=True)),
                ("referring_url", models.URLField(blank=True)),
                ("timestamp", models.DateTimeField(auto_now_add=True)),
                ("metadata", models.JSONField(default=dict)),
                ("converted_at", models.DateTimeField(blank=True, null=True)),
                (
                    "conversion_value",
                    models.DecimalField(blank=True, decimal_places=2, max_digits=10, null=True),
                ),
                ("is_converted", models.BooleanField(default=False)),
                ("session_id", models.CharField(blank=True, max_length=100)),
            ],
        ),
        migrations.CreateModel(
            name="Affiliate",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                ("code", models.CharField(max_length=50, unique=True)),
                ("is_active", models.BooleanField(default=True)),
                ("joined_at", models.DateTimeField(auto_now_add=True)),
                ("balance", models.DecimalField(decimal_places=2, default=0.0, max_digits=10)),
                (
                    "payout_threshold",
                    models.DecimalField(decimal_places=2, default=50.0, max_digits=10),
                ),
                ("payout_method", models.CharField(blank=True, max_length=50)),
                ("payout_details", models.JSONField(default=dict)),
                (
                    "user",
                    models.OneToOneField(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="affiliate",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
        ),
        migrations.CreateModel(
            name="Commission",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                ("amount", models.DecimalField(decimal_places=2, max_digits=10)),
                ("rate", models.DecimalField(decimal_places=2, max_digits=5)),
                ("calculated_at", models.DateTimeField(auto_now_add=True)),
                (
                    "status",
                    models.CharField(
                        choices=[
                            ("pending", "Pending"),
                            ("approved", "Approved"),
                            ("rejected", "Rejected"),
                            ("paid", "Paid"),
                        ],
                        default="pending",
                        max_length=20,
                    ),
                ),
                (
                    "affiliate",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="commissions",
                        to="django_affiliate_system.affiliate",
                    ),
                ),
                (
                    "referral_action",
                    models.OneToOneField(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="commission",
                        to="django_affiliate_system.referralaction",
                    ),
                ),
            ],
        ),
        migrations.CreateModel(
            name="ReferralLink",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                ("slug", models.SlugField(unique=True)),
                ("destination_url", models.URLField()),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("is_active", models.BooleanField(default=True)),
                (
                    "affiliate",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="referral_links",
                        to="django_affiliate_system.affiliate",
                    ),
                ),
            ],
        ),
        migrations.AddField(
            model_name="referralaction",
            name="referral_link",
            field=models.ForeignKey(
                on_delete=django.db.models.deletion.CASCADE,
                related_name="actions",
                to="django_affiliate_system.referrallink",
            ),
        ),
        migrations.CreateModel(
            name="ReferralSession",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                ("session_id", models.CharField(max_length=100, unique=True)),
                ("first_touch", models.DateTimeField(auto_now_add=True)),
                ("last_touch", models.DateTimeField(auto_now=True)),
                ("is_converted", models.BooleanField(default=False)),
                (
                    "conversion_value",
                    models.DecimalField(decimal_places=2, default=0, max_digits=10),
                ),
                ("ip_address", models.GenericIPAddressField(blank=True, null=True)),
                ("user_agent", models.TextField(blank=True)),
                (
                    "affiliate",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to="django_affiliate_system.affiliate",
                    ),
                ),
                (
                    "first_referral_link",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="first_sessions",
                        to="django_affiliate_system.referrallink",
                    ),
                ),
                (
                    "last_referral_link",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="last_sessions",
                        to="django_affiliate_system.referrallink",
                    ),
                ),
            ],
            options={
                "db_table": "affiliates_referralsession",
            },
        ),
        migrations.CreateModel(
            name="Tenant",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                ("name", models.CharField(max_length=255)),
                ("slug", models.SlugField(unique=True)),
                ("destination_url", models.URLField()),
                ("subdomain", models.URLField(blank=True)),
                ("api_key", models.UUIDField(default=uuid.uuid4, unique=True)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("is_active", models.BooleanField(default=True)),
                (
                    "default_commission_rate",
                    models.DecimalField(decimal_places=2, default=10.0, max_digits=5),
                ),
                ("cookie_duration_days", models.PositiveIntegerField(default=30)),
                (
                    "owner",
                    models.OneToOneField(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.PROTECT,
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
        ),
        migrations.AddField(
            model_name="referralaction",
            name="tenant",
            field=models.ForeignKey(
                on_delete=django.db.models.deletion.CASCADE, to="django_affiliate_system.tenant"
            ),
        ),
        migrations.CreateModel(
            name="Payout",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                ("amount", models.DecimalField(decimal_places=2, max_digits=10)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("processed_at", models.DateTimeField(blank=True, null=True)),
                (
                    "status",
                    models.CharField(
                        choices=[
                            ("pending", "Pending"),
                            ("processing", "Processing"),
                            ("paid", "Paid"),
                            ("failed", "Failed"),
                        ],
                        default="pending",
                        max_length=20,
                    ),
                ),
                ("method", models.CharField(max_length=50)),
                ("reference", models.CharField(blank=True, max_length=255)),
                ("metadata", models.JSONField(default=dict)),
                (
                    "affiliate",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="payouts",
                        to="django_affiliate_system.affiliate",
                    ),
                ),
                (
                    "tenant",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="payouts",
                        to="django_affiliate_system.tenant",
                    ),
                ),
            ],
            options={
                "ordering": ["-created_at"],
            },
        ),
        migrations.CreateModel(
            name="CommissionRule",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                ("name", models.CharField(max_length=255)),
                (
                    "action_type",
                    models.CharField(
                        choices=[
                            ("click", "CLICK"),
                            ("signup", "SIGNUP"),
                            ("purchase", "PURCHASE"),
                            ("other", "OTHER"),
                        ],
                        max_length=20,
                    ),
                ),
                ("is_percentage", models.BooleanField(default=True)),
                ("value", models.DecimalField(decimal_places=2, max_digits=10)),
                (
                    "min_value",
                    models.DecimalField(blank=True, decimal_places=2, max_digits=10, null=True),
                ),
                (
                    "max_value",
                    models.DecimalField(blank=True, decimal_places=2, max_digits=10, null=True),
                ),
                ("is_active", models.BooleanField(default=True)),
                (
                    "tenant",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="commission_rules",
                        to="django_affiliate_system.tenant",
                    ),
                ),
            ],
        ),
        migrations.AddField(
            model_name="affiliate",
            name="tenant",
            field=models.ForeignKey(
                on_delete=django.db.models.deletion.CASCADE,
                related_name="affiliates",
                to="django_affiliate_system.tenant",
            ),
        ),
        migrations.AddIndex(
            model_name="referralaction",
            index=models.Index(
                fields=["referral_link", "action_type"], name="django_affi_referra_0889de_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="referralaction",
            index=models.Index(
                fields=["tenant", "is_converted"], name="django_affi_tenant__81903a_idx"
            ),
        ),
        migrations.AlterUniqueTogether(
            name="affiliate",
            unique_together={("tenant", "user")},
        ),
    ]
