# Generated by Django 5.2.9 on 2025-12-27 20:58

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 = [
        ("accounts", "0014_remove_toplist_user_remove_toplistitem_top_list_and_more"),
        ("pghistory", "0006_delete_aggregateevent"),
    ]

    operations = [
        migrations.CreateModel(
            name="LoginHistory",
            fields=[
                ("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
                (
                    "ip_address",
                    models.GenericIPAddressField(
                        blank=True, help_text="IP address from which the login occurred", null=True
                    ),
                ),
                (
                    "user_agent",
                    models.CharField(blank=True, help_text="Browser/client user agent string", max_length=500),
                ),
                (
                    "login_method",
                    models.CharField(
                        choices=[
                            ("PASSWORD", "Password"),
                            ("GOOGLE", "Google OAuth"),
                            ("DISCORD", "Discord OAuth"),
                            ("MAGIC_LINK", "Magic Link"),
                            ("SESSION", "Session Refresh"),
                        ],
                        default="PASSWORD",
                        help_text="Method used for authentication",
                        max_length=20,
                    ),
                ),
                (
                    "login_timestamp",
                    models.DateTimeField(auto_now_add=True, db_index=True, help_text="When the login occurred"),
                ),
                ("success", models.BooleanField(default=True, help_text="Whether the login was successful")),
                (
                    "country",
                    models.CharField(blank=True, help_text="Country derived from IP (optional)", max_length=100),
                ),
                ("city", models.CharField(blank=True, help_text="City derived from IP (optional)", max_length=100)),
                (
                    "user",
                    models.ForeignKey(
                        help_text="User who logged in",
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="login_history",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "verbose_name": "Login History",
                "verbose_name_plural": "Login History",
                "ordering": ["-login_timestamp"],
            },
        ),
        migrations.CreateModel(
            name="LoginHistoryEvent",
            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()),
                (
                    "ip_address",
                    models.GenericIPAddressField(
                        blank=True, help_text="IP address from which the login occurred", null=True
                    ),
                ),
                (
                    "user_agent",
                    models.CharField(blank=True, help_text="Browser/client user agent string", max_length=500),
                ),
                (
                    "login_method",
                    models.CharField(
                        choices=[
                            ("PASSWORD", "Password"),
                            ("GOOGLE", "Google OAuth"),
                            ("DISCORD", "Discord OAuth"),
                            ("MAGIC_LINK", "Magic Link"),
                            ("SESSION", "Session Refresh"),
                        ],
                        default="PASSWORD",
                        help_text="Method used for authentication",
                        max_length=20,
                    ),
                ),
                ("login_timestamp", models.DateTimeField(auto_now_add=True, help_text="When the login occurred")),
                ("success", models.BooleanField(default=True, help_text="Whether the login was successful")),
                (
                    "country",
                    models.CharField(blank=True, help_text="Country derived from IP (optional)", max_length=100),
                ),
                ("city", models.CharField(blank=True, help_text="City derived from IP (optional)", max_length=100)),
                (
                    "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="accounts.loginhistory",
                    ),
                ),
                (
                    "user",
                    models.ForeignKey(
                        db_constraint=False,
                        help_text="User who logged in",
                        on_delete=django.db.models.deletion.DO_NOTHING,
                        related_name="+",
                        related_query_name="+",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "abstract": False,
            },
        ),
        migrations.AddIndex(
            model_name="loginhistory",
            index=models.Index(fields=["user", "-login_timestamp"], name="accounts_lo_user_id_156da7_idx"),
        ),
        migrations.AddIndex(
            model_name="loginhistory",
            index=models.Index(fields=["ip_address"], name="accounts_lo_ip_addr_142937_idx"),
        ),
        pgtrigger.migrations.AddTrigger(
            model_name="loginhistory",
            trigger=pgtrigger.compiler.Trigger(
                name="insert_insert",
                sql=pgtrigger.compiler.UpsertTriggerSql(
                    func='INSERT INTO "accounts_loginhistoryevent" ("city", "country", "id", "ip_address", "login_method", "login_timestamp", "pgh_context_id", "pgh_created_at", "pgh_label", "pgh_obj_id", "success", "user_agent", "user_id") VALUES (NEW."city", NEW."country", NEW."id", NEW."ip_address", NEW."login_method", NEW."login_timestamp", _pgh_attach_context(), NOW(), \'insert\', NEW."id", NEW."success", NEW."user_agent", NEW."user_id"); RETURN NULL;',
                    hash="9ccc4d52099a09097d02128eb427d58ae955a377",
                    operation="INSERT",
                    pgid="pgtrigger_insert_insert_dc41d",
                    table="accounts_loginhistory",
                    when="AFTER",
                ),
            ),
        ),
        pgtrigger.migrations.AddTrigger(
            model_name="loginhistory",
            trigger=pgtrigger.compiler.Trigger(
                name="update_update",
                sql=pgtrigger.compiler.UpsertTriggerSql(
                    condition="WHEN (OLD.* IS DISTINCT FROM NEW.*)",
                    func='INSERT INTO "accounts_loginhistoryevent" ("city", "country", "id", "ip_address", "login_method", "login_timestamp", "pgh_context_id", "pgh_created_at", "pgh_label", "pgh_obj_id", "success", "user_agent", "user_id") VALUES (NEW."city", NEW."country", NEW."id", NEW."ip_address", NEW."login_method", NEW."login_timestamp", _pgh_attach_context(), NOW(), \'update\', NEW."id", NEW."success", NEW."user_agent", NEW."user_id"); RETURN NULL;',
                    hash="d5d998a5af1a55f181ebe8500a70022e8e4db724",
                    operation="UPDATE",
                    pgid="pgtrigger_update_update_110f5",
                    table="accounts_loginhistory",
                    when="AFTER",
                ),
            ),
        ),
    ]
