# Generated by Django 5.1.6 on 2025-12-26 15:57

import django.core.validators
import django.db.models.deletion
import pgtrigger.compiler
import pgtrigger.migrations
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ("pghistory", "0006_delete_aggregateevent"),
        ("rides", "0027_alter_company_options_alter_rankingsnapshot_options_and_more"),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name="RideCredit",
            fields=[
                ("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                ("count", models.PositiveIntegerField(default=1, help_text="Number of times ridden")),
                (
                    "rating",
                    models.IntegerField(
                        blank=True,
                        help_text="Personal rating (1-5)",
                        null=True,
                        validators=[
                            django.core.validators.MinValueValidator(1),
                            django.core.validators.MaxValueValidator(5),
                        ],
                    ),
                ),
                ("first_ridden_at", models.DateField(blank=True, help_text="Date of first ride", null=True)),
                ("last_ridden_at", models.DateField(blank=True, help_text="Date of most recent ride", null=True)),
                ("notes", models.TextField(blank=True, help_text="Personal notes about the experience")),
                (
                    "ride",
                    models.ForeignKey(
                        help_text="The ride that was ridden",
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="credits",
                        to="rides.ride",
                    ),
                ),
                (
                    "user",
                    models.ForeignKey(
                        help_text="User who rode the ride",
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="ride_credits",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "verbose_name": "Ride Credit",
                "verbose_name_plural": "Ride Credits",
                "ordering": ["-last_ridden_at", "-first_ridden_at", "-created_at"],
                "abstract": False,
                "unique_together": {("user", "ride")},
            },
        ),
        migrations.CreateModel(
            name="RideCreditEvent",
            fields=[
                ("pgh_id", models.AutoField(primary_key=True, serialize=False)),
                ("pgh_created_at", models.DateTimeField(auto_now_add=True)),
                ("pgh_label", models.TextField(help_text="The event label.")),
                ("id", models.BigIntegerField()),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                ("count", models.PositiveIntegerField(default=1, help_text="Number of times ridden")),
                (
                    "rating",
                    models.IntegerField(
                        blank=True,
                        help_text="Personal rating (1-5)",
                        null=True,
                        validators=[
                            django.core.validators.MinValueValidator(1),
                            django.core.validators.MaxValueValidator(5),
                        ],
                    ),
                ),
                ("first_ridden_at", models.DateField(blank=True, help_text="Date of first ride", null=True)),
                ("last_ridden_at", models.DateField(blank=True, help_text="Date of most recent ride", null=True)),
                ("notes", models.TextField(blank=True, help_text="Personal notes about the experience")),
                (
                    "pgh_context",
                    models.ForeignKey(
                        db_constraint=False,
                        null=True,
                        on_delete=django.db.models.deletion.DO_NOTHING,
                        related_name="+",
                        to="pghistory.context",
                    ),
                ),
                (
                    "pgh_obj",
                    models.ForeignKey(
                        db_constraint=False,
                        on_delete=django.db.models.deletion.DO_NOTHING,
                        related_name="events",
                        to="rides.ridecredit",
                    ),
                ),
                (
                    "ride",
                    models.ForeignKey(
                        db_constraint=False,
                        help_text="The ride that was ridden",
                        on_delete=django.db.models.deletion.DO_NOTHING,
                        related_name="+",
                        related_query_name="+",
                        to="rides.ride",
                    ),
                ),
                (
                    "user",
                    models.ForeignKey(
                        db_constraint=False,
                        help_text="User who rode the ride",
                        on_delete=django.db.models.deletion.DO_NOTHING,
                        related_name="+",
                        related_query_name="+",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "abstract": False,
            },
        ),
        pgtrigger.migrations.AddTrigger(
            model_name="ridecredit",
            trigger=pgtrigger.compiler.Trigger(
                name="insert_insert",
                sql=pgtrigger.compiler.UpsertTriggerSql(
                    func='INSERT INTO "rides_ridecreditevent" ("count", "created_at", "first_ridden_at", "id", "last_ridden_at", "notes", "pgh_context_id", "pgh_created_at", "pgh_label", "pgh_obj_id", "rating", "ride_id", "updated_at", "user_id") VALUES (NEW."count", NEW."created_at", NEW."first_ridden_at", NEW."id", NEW."last_ridden_at", NEW."notes", _pgh_attach_context(), NOW(), \'insert\', NEW."id", NEW."rating", NEW."ride_id", NEW."updated_at", NEW."user_id"); RETURN NULL;',
                    hash="99d9f7e7134fbcb6f84a1966fe9539c8ccc22eee",
                    operation="INSERT",
                    pgid="pgtrigger_insert_insert_00439",
                    table="rides_ridecredit",
                    when="AFTER",
                ),
            ),
        ),
        pgtrigger.migrations.AddTrigger(
            model_name="ridecredit",
            trigger=pgtrigger.compiler.Trigger(
                name="update_update",
                sql=pgtrigger.compiler.UpsertTriggerSql(
                    condition="WHEN (OLD.* IS DISTINCT FROM NEW.*)",
                    func='INSERT INTO "rides_ridecreditevent" ("count", "created_at", "first_ridden_at", "id", "last_ridden_at", "notes", "pgh_context_id", "pgh_created_at", "pgh_label", "pgh_obj_id", "rating", "ride_id", "updated_at", "user_id") VALUES (NEW."count", NEW."created_at", NEW."first_ridden_at", NEW."id", NEW."last_ridden_at", NEW."notes", _pgh_attach_context(), NOW(), \'update\', NEW."id", NEW."rating", NEW."ride_id", NEW."updated_at", NEW."user_id"); RETURN NULL;',
                    hash="1795a528b3b188da59c8cf60053df8eddb80904c",
                    operation="UPDATE",
                    pgid="pgtrigger_update_update_32a65",
                    table="rides_ridecredit",
                    when="AFTER",
                ),
            ),
        ),
    ]
