# Generated by Django 5.2.9 on 2026-01-02 16:18

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


class Migration(migrations.Migration):

    dependencies = [
        ("core", "0004_alter_slughistory_options_and_more"),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name="ApplicationError",
            fields=[
                ("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
                (
                    "error_id",
                    models.UUIDField(
                        db_index=True,
                        default=uuid.uuid4,
                        help_text="Unique identifier for this error instance",
                        unique=True,
                    ),
                ),
                (
                    "request_id",
                    models.CharField(
                        blank=True, db_index=True, help_text="Request correlation ID if available", max_length=255
                    ),
                ),
                (
                    "error_type",
                    models.CharField(
                        db_index=True,
                        help_text="Type/class of the error (e.g., 'ValidationError', 'TypeError')",
                        max_length=100,
                    ),
                ),
                ("error_message", models.TextField(help_text="Human-readable error message")),
                ("error_stack", models.TextField(blank=True, help_text="Stack trace if available")),
                (
                    "error_code",
                    models.CharField(
                        blank=True, db_index=True, help_text="Application-specific error code", max_length=50
                    ),
                ),
                (
                    "severity",
                    models.CharField(
                        choices=[("critical", "Critical"), ("high", "High"), ("medium", "Medium"), ("low", "Low")],
                        db_index=True,
                        default="medium",
                        help_text="Error severity level",
                        max_length=20,
                    ),
                ),
                (
                    "source",
                    models.CharField(
                        choices=[("frontend", "Frontend"), ("backend", "Backend"), ("api", "API")],
                        db_index=True,
                        help_text="Where the error originated",
                        max_length=20,
                    ),
                ),
                (
                    "endpoint",
                    models.CharField(blank=True, help_text="URL/endpoint where the error occurred", max_length=500),
                ),
                ("http_method", models.CharField(blank=True, help_text="HTTP method of the request", max_length=10)),
                (
                    "http_status",
                    models.PositiveIntegerField(blank=True, help_text="HTTP status code returned", null=True),
                ),
                ("user_agent", models.TextField(blank=True, help_text="User agent string from the client")),
                (
                    "ip_address_hash",
                    models.CharField(
                        blank=True,
                        db_index=True,
                        help_text="Hashed IP address for rate limiting (privacy-preserving)",
                        max_length=64,
                    ),
                ),
                (
                    "metadata",
                    models.JSONField(
                        blank=True, default=dict, help_text="Additional context (action, entity info, etc.)"
                    ),
                ),
                (
                    "environment",
                    models.JSONField(
                        blank=True, default=dict, help_text="Client environment info (viewport, browser, etc.)"
                    ),
                ),
                (
                    "created_at",
                    models.DateTimeField(auto_now_add=True, db_index=True, help_text="When the error was recorded"),
                ),
                (
                    "resolved",
                    models.BooleanField(
                        db_index=True, default=False, help_text="Whether this error has been addressed"
                    ),
                ),
                (
                    "resolved_at",
                    models.DateTimeField(blank=True, help_text="When the error was marked resolved", null=True),
                ),
                ("resolution_notes", models.TextField(blank=True, help_text="Notes about how the error was resolved")),
                (
                    "resolved_by",
                    models.ForeignKey(
                        blank=True,
                        help_text="Admin who resolved this error",
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="resolved_errors",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "user",
                    models.ForeignKey(
                        blank=True,
                        help_text="User who encountered the error",
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="application_errors",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "verbose_name": "Application Error",
                "verbose_name_plural": "Application Errors",
                "ordering": ["-created_at"],
                "indexes": [
                    models.Index(fields=["severity", "created_at"], name="core_applic_severit_6eeb93_idx"),
                    models.Index(fields=["source", "created_at"], name="core_applic_source_31a37f_idx"),
                    models.Index(fields=["error_type", "created_at"], name="core_applic_error_t_e787f5_idx"),
                    models.Index(fields=["resolved", "created_at"], name="core_applic_resolve_2b0297_idx"),
                ],
            },
        ),
    ]
