# Generated by Django 4.2.24 on 2025-10-30 18:07

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import opencontractserver.shared.defaults
import opencontractserver.shared.fields


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        ("corpuses", "0022_add_agent_instructions"),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name="Badge",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("backend_lock", models.BooleanField(default=False)),
                ("is_public", models.BooleanField(default=False)),
                ("created", models.DateTimeField(auto_now_add=True)),
                ("modified", models.DateTimeField(auto_now=True)),
                (
                    "name",
                    models.CharField(
                        help_text="Unique name for the badge",
                        max_length=100,
                        unique=True,
                    ),
                ),
                (
                    "description",
                    models.TextField(
                        help_text="Description of what this badge represents or how to earn it"
                    ),
                ),
                (
                    "icon",
                    models.CharField(
                        help_text="Icon identifier from lucide-react (e.g., 'Trophy', 'Star', 'Award')",
                        max_length=100,
                    ),
                ),
                (
                    "badge_type",
                    models.CharField(
                        choices=[("GLOBAL", "Global"), ("CORPUS", "Corpus-Specific")],
                        default="GLOBAL",
                        help_text="Whether this badge is global or corpus-specific",
                        max_length=10,
                    ),
                ),
                (
                    "color",
                    models.CharField(
                        default="#05313d",
                        help_text="Hex color code for badge display",
                        max_length=7,
                    ),
                ),
                (
                    "is_auto_awarded",
                    models.BooleanField(
                        default=False,
                        help_text="Whether this badge is automatically awarded based on criteria",
                    ),
                ),
                (
                    "criteria_config",
                    opencontractserver.shared.fields.NullableJSONField(
                        blank=True,
                        default=opencontractserver.shared.defaults.jsonfield_default_value,
                        help_text="JSON configuration for auto-award criteria. Example: {'type': 'reputation_threshold', 'value': 100, 'scope': 'global'}",
                        null=True,
                    ),
                ),
                (
                    "corpus",
                    models.ForeignKey(
                        blank=True,
                        help_text="If badge_type is CORPUS, the corpus this badge belongs to",
                        null=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="badges",
                        to="corpuses.corpus",
                    ),
                ),
                (
                    "creator",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "user_lock",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="locked_%(class)s_objects",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "ordering": ["-created"],
            },
        ),
        migrations.CreateModel(
            name="UserBadge",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "awarded_at",
                    models.DateTimeField(
                        auto_now_add=True, help_text="When the badge was awarded"
                    ),
                ),
                (
                    "awarded_by",
                    models.ForeignKey(
                        blank=True,
                        help_text="User who awarded the badge (null for auto-awards)",
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="badges_awarded",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "badge",
                    models.ForeignKey(
                        help_text="Badge that was awarded",
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="awards",
                        to="badges.badge",
                    ),
                ),
                (
                    "corpus",
                    models.ForeignKey(
                        blank=True,
                        help_text="For corpus-specific badges, the context in which it was awarded",
                        null=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="user_badges",
                        to="corpuses.corpus",
                    ),
                ),
                (
                    "user",
                    models.ForeignKey(
                        help_text="User who received the badge",
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="badges",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "ordering": ["-awarded_at"],
                "indexes": [
                    models.Index(
                        fields=["user", "awarded_at"],
                        name="badges_user_user_id_60359e_idx",
                    ),
                    models.Index(
                        fields=["badge", "corpus"],
                        name="badges_user_badge_i_d1444e_idx",
                    ),
                ],
            },
        ),
        migrations.AddConstraint(
            model_name="userbadge",
            constraint=models.UniqueConstraint(
                fields=("user", "badge", "corpus"), name="unique_user_badge_corpus"
            ),
        ),
        migrations.AddIndex(
            model_name="badge",
            index=models.Index(
                fields=["badge_type", "corpus"], name="badges_badg_badge_t_1c0369_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="badge",
            index=models.Index(
                fields=["is_auto_awarded"], name="badges_badg_is_auto_a74c0c_idx"
            ),
        ),
    ]
