# Generated by Django 4.2.24 on 2025-10-27 02:03

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


class Migration(migrations.Migration):

    dependencies = [
        ("auth", "0012_alter_user_first_name_max_length"),
        ("corpuses", "0022_add_agent_instructions"),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        (
            "conversations",
            "0005_chatmessage_agent_type_chatmessage_deleted_at_and_more",
        ),
    ]

    operations = [
        migrations.CreateModel(
            name="MessageVote",
            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)),
                (
                    "vote_type",
                    models.CharField(
                        choices=[("upvote", "Upvote"), ("downvote", "Downvote")],
                        help_text="Type of vote (upvote or downvote)",
                        max_length=16,
                    ),
                ),
                (
                    "created_at",
                    models.DateTimeField(
                        auto_now_add=True, help_text="Timestamp when the vote was cast"
                    ),
                ),
                (
                    "updated_at",
                    models.DateTimeField(
                        auto_now=True,
                        help_text="Timestamp when the vote was last changed",
                    ),
                ),
                (
                    "creator",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "permissions": (
                    ("permission_messagevote", "permission messagevote"),
                    ("create_messagevote", "create messagevote"),
                    ("read_messagevote", "read messagevote"),
                    ("update_messagevote", "update messagevote"),
                    ("remove_messagevote", "delete messagevote"),
                ),
            },
        ),
        migrations.CreateModel(
            name="UserReputation",
            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)),
                (
                    "reputation_score",
                    models.IntegerField(
                        default=0,
                        help_text="Current reputation score (upvotes - downvotes)",
                    ),
                ),
                (
                    "total_upvotes_received",
                    models.IntegerField(
                        default=0,
                        help_text="Total upvotes received across all messages",
                    ),
                ),
                (
                    "total_downvotes_received",
                    models.IntegerField(
                        default=0,
                        help_text="Total downvotes received across all messages",
                    ),
                ),
                (
                    "last_calculated_at",
                    models.DateTimeField(
                        auto_now=True,
                        help_text="Timestamp when reputation was last calculated",
                    ),
                ),
                (
                    "corpus",
                    models.ForeignKey(
                        blank=True,
                        help_text="The corpus for which reputation is tracked (null = global)",
                        null=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="user_reputations",
                        to="corpuses.corpus",
                    ),
                ),
                (
                    "creator",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "user",
                    models.ForeignKey(
                        help_text="The user whose reputation is being tracked",
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="reputation_scores",
                        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={
                "permissions": (
                    ("permission_userreputation", "permission userreputation"),
                    ("create_userreputation", "create userreputation"),
                    ("read_userreputation", "read userreputation"),
                    ("update_userreputation", "update userreputation"),
                    ("remove_userreputation", "delete userreputation"),
                ),
            },
        ),
        migrations.AddField(
            model_name="chatmessage",
            name="downvote_count",
            field=models.IntegerField(
                default=0, help_text="Cached count of downvotes for this message"
            ),
        ),
        migrations.AddField(
            model_name="chatmessage",
            name="upvote_count",
            field=models.IntegerField(
                default=0, help_text="Cached count of upvotes for this message"
            ),
        ),
        migrations.CreateModel(
            name="MessageVoteUserObjectPermission",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "content_object",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to="conversations.messagevote",
                    ),
                ),
                (
                    "permission",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to="auth.permission",
                    ),
                ),
                (
                    "user",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "abstract": False,
            },
        ),
        migrations.CreateModel(
            name="MessageVoteGroupObjectPermission",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "content_object",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to="conversations.messagevote",
                    ),
                ),
                (
                    "group",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE, to="auth.group"
                    ),
                ),
                (
                    "permission",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to="auth.permission",
                    ),
                ),
            ],
            options={
                "abstract": False,
            },
        ),
        migrations.AddField(
            model_name="messagevote",
            name="message",
            field=models.ForeignKey(
                help_text="The message being voted on",
                on_delete=django.db.models.deletion.CASCADE,
                related_name="votes",
                to="conversations.chatmessage",
            ),
        ),
        migrations.AddField(
            model_name="messagevote",
            name="user_lock",
            field=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,
            ),
        ),
        migrations.CreateModel(
            name="UserReputationUserObjectPermission",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "content_object",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to="conversations.userreputation",
                    ),
                ),
                (
                    "permission",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to="auth.permission",
                    ),
                ),
                (
                    "user",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "abstract": False,
                "unique_together": {("user", "permission", "content_object")},
            },
        ),
        migrations.CreateModel(
            name="UserReputationGroupObjectPermission",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "content_object",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to="conversations.userreputation",
                    ),
                ),
                (
                    "group",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE, to="auth.group"
                    ),
                ),
                (
                    "permission",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to="auth.permission",
                    ),
                ),
            ],
            options={
                "abstract": False,
                "unique_together": {("group", "permission", "content_object")},
            },
        ),
        migrations.AddIndex(
            model_name="userreputation",
            index=models.Index(
                fields=["user", "corpus"], name="conversatio_user_id_72cc6b_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="userreputation",
            index=models.Index(
                fields=["reputation_score"], name="conversatio_reputat_8dbfc4_idx"
            ),
        ),
        migrations.AddConstraint(
            model_name="userreputation",
            constraint=models.UniqueConstraint(
                fields=("user", "corpus"), name="one_reputation_per_user_per_corpus"
            ),
        ),
        migrations.AlterUniqueTogether(
            name="messagevoteuserobjectpermission",
            unique_together={("user", "permission", "content_object")},
        ),
        migrations.AlterUniqueTogether(
            name="messagevotegroupobjectpermission",
            unique_together={("group", "permission", "content_object")},
        ),
        migrations.AddIndex(
            model_name="messagevote",
            index=models.Index(
                fields=["message", "vote_type"], name="conversatio_message_167185_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="messagevote",
            index=models.Index(
                fields=["creator"], name="conversatio_creator_c9df95_idx"
            ),
        ),
        migrations.AddConstraint(
            model_name="messagevote",
            constraint=models.UniqueConstraint(
                fields=("message", "creator"), name="one_vote_per_user_per_message"
            ),
        ),
    ]
