# Generated by Django 5.2.10 on 2026-01-11 18:06

import apps.core.choices.fields
import django.db.models.deletion
import uuid
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ("core", "0011_alter_applicationerror_severity_and_more"),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name="AlertCorrelationRule",
            fields=[
                ("id", models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                (
                    "rule_name",
                    models.CharField(
                        db_index=True, help_text="Unique name for this correlation rule", max_length=255, unique=True
                    ),
                ),
                (
                    "rule_description",
                    models.TextField(blank=True, help_text="Description of what this rule correlates"),
                ),
                (
                    "min_alerts_required",
                    models.PositiveIntegerField(
                        default=3, help_text="Minimum number of alerts needed to trigger correlation"
                    ),
                ),
                (
                    "time_window_minutes",
                    models.PositiveIntegerField(default=30, help_text="Time window in minutes for alert correlation"),
                ),
                (
                    "incident_severity",
                    apps.core.choices.fields.RichChoiceField(
                        allow_deprecated=False,
                        choice_group="severity_levels",
                        choices=[("critical", "Critical"), ("high", "High"), ("medium", "Medium"), ("low", "Low")],
                        default="medium",
                        domain="core",
                        help_text="Severity to assign to correlated incidents",
                        max_length=20,
                    ),
                ),
                (
                    "incident_title_template",
                    models.CharField(
                        help_text="Template for incident title (supports {count}, {rule_name})", max_length=255
                    ),
                ),
                (
                    "is_active",
                    models.BooleanField(db_index=True, default=True, help_text="Whether this rule is currently active"),
                ),
                ("created_at", models.DateTimeField(auto_now_add=True, help_text="When this rule was created")),
                ("updated_at", models.DateTimeField(auto_now=True, help_text="When this rule was last updated")),
            ],
            options={
                "verbose_name": "Alert Correlation Rule",
                "verbose_name_plural": "Alert Correlation Rules",
                "ordering": ["rule_name"],
            },
        ),
        migrations.CreateModel(
            name="CleanupJobLog",
            fields=[
                ("id", models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ("job_name", models.CharField(db_index=True, help_text="Name of the cleanup job", max_length=255)),
                (
                    "status",
                    apps.core.choices.fields.RichChoiceField(
                        allow_deprecated=False,
                        choice_group="cleanup_job_statuses",
                        choices=[
                            ("success", "Success"),
                            ("failed", "Failed"),
                            ("partial", "Partial"),
                            ("skipped", "Skipped"),
                        ],
                        db_index=True,
                        default="success",
                        domain="core",
                        help_text="Execution status",
                        max_length=20,
                    ),
                ),
                ("records_processed", models.PositiveIntegerField(default=0, help_text="Number of records processed")),
                ("records_deleted", models.PositiveIntegerField(default=0, help_text="Number of records deleted")),
                ("error_message", models.TextField(blank=True, help_text="Error message if job failed", null=True)),
                (
                    "duration_ms",
                    models.PositiveIntegerField(blank=True, help_text="Execution duration in milliseconds", null=True),
                ),
                (
                    "executed_at",
                    models.DateTimeField(auto_now_add=True, db_index=True, help_text="When this job was executed"),
                ),
            ],
            options={
                "verbose_name": "Cleanup Job Log",
                "verbose_name_plural": "Cleanup Job Logs",
                "ordering": ["-executed_at"],
                "indexes": [
                    models.Index(fields=["job_name", "executed_at"], name="core_cleanu_job_nam_4530fd_idx"),
                    models.Index(fields=["status", "executed_at"], name="core_cleanu_status_fa6360_idx"),
                ],
            },
        ),
        migrations.CreateModel(
            name="Anomaly",
            fields=[
                ("id", models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                (
                    "metric_name",
                    models.CharField(
                        db_index=True, help_text="Name of the metric that exhibited anomalous behavior", max_length=255
                    ),
                ),
                (
                    "metric_category",
                    models.CharField(
                        db_index=True,
                        help_text="Category of the metric (e.g., 'performance', 'error_rate', 'traffic')",
                        max_length=100,
                    ),
                ),
                (
                    "anomaly_type",
                    apps.core.choices.fields.RichChoiceField(
                        allow_deprecated=False,
                        choice_group="anomaly_types",
                        choices=[
                            ("spike", "Spike"),
                            ("drop", "Drop"),
                            ("trend_change", "Trend Change"),
                            ("outlier", "Outlier"),
                            ("threshold_breach", "Threshold Breach"),
                        ],
                        db_index=True,
                        domain="core",
                        help_text="Type of anomaly detected",
                        max_length=30,
                    ),
                ),
                (
                    "severity",
                    apps.core.choices.fields.RichChoiceField(
                        allow_deprecated=False,
                        choice_group="severity_levels",
                        choices=[("critical", "Critical"), ("high", "High"), ("medium", "Medium"), ("low", "Low")],
                        db_index=True,
                        domain="core",
                        help_text="Severity of the anomaly",
                        max_length=20,
                    ),
                ),
                (
                    "anomaly_value",
                    models.DecimalField(decimal_places=6, help_text="The anomalous value detected", max_digits=20),
                ),
                (
                    "baseline_value",
                    models.DecimalField(decimal_places=6, help_text="The expected baseline value", max_digits=20),
                ),
                (
                    "deviation_score",
                    models.DecimalField(decimal_places=4, help_text="Standard deviations from normal", max_digits=10),
                ),
                (
                    "confidence_score",
                    models.DecimalField(
                        decimal_places=4, help_text="Confidence score of the detection (0-1)", max_digits=5
                    ),
                ),
                ("detection_algorithm", models.CharField(help_text="Algorithm used for detection", max_length=100)),
                ("time_window_start", models.DateTimeField(help_text="Start of the detection time window")),
                ("time_window_end", models.DateTimeField(help_text="End of the detection time window")),
                (
                    "alert_created",
                    models.BooleanField(
                        db_index=True, default=False, help_text="Whether an alert was created for this anomaly"
                    ),
                ),
                (
                    "detected_at",
                    models.DateTimeField(auto_now_add=True, db_index=True, help_text="When this anomaly was detected"),
                ),
                (
                    "alert",
                    models.ForeignKey(
                        blank=True,
                        help_text="Linked system alert if created",
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="anomalies",
                        to="core.systemalert",
                    ),
                ),
            ],
            options={
                "verbose_name": "Anomaly",
                "verbose_name_plural": "Anomalies",
                "ordering": ["-detected_at"],
                "indexes": [
                    models.Index(fields=["metric_name", "detected_at"], name="core_anomal_metric__06c3c9_idx"),
                    models.Index(fields=["severity", "detected_at"], name="core_anomal_severit_ea7a17_idx"),
                    models.Index(fields=["anomaly_type", "detected_at"], name="core_anomal_anomaly_eb45f7_idx"),
                    models.Index(fields=["alert_created", "detected_at"], name="core_anomal_alert_c_5a0c1a_idx"),
                ],
            },
        ),
        migrations.CreateModel(
            name="PipelineError",
            fields=[
                ("id", models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                (
                    "function_name",
                    models.CharField(
                        db_index=True, help_text="Name of the function/pipeline that failed", max_length=255
                    ),
                ),
                ("error_message", models.TextField(help_text="Error message describing the failure")),
                (
                    "error_code",
                    models.CharField(
                        blank=True, db_index=True, help_text="Error code for categorization", max_length=100, null=True
                    ),
                ),
                ("error_context", models.JSONField(blank=True, help_text="Additional context data as JSON", null=True)),
                ("stack_trace", models.TextField(blank=True, help_text="Full stack trace for debugging", null=True)),
                (
                    "severity",
                    apps.core.choices.fields.RichChoiceField(
                        allow_deprecated=False,
                        choice_group="pipeline_error_severities",
                        choices=[
                            ("critical", "Critical"),
                            ("error", "Error"),
                            ("warning", "Warning"),
                            ("info", "Info"),
                        ],
                        db_index=True,
                        default="error",
                        domain="core",
                        help_text="Severity level of the error",
                        max_length=20,
                    ),
                ),
                (
                    "submission_id",
                    models.UUIDField(
                        blank=True, db_index=True, help_text="ID of related content submission if applicable", null=True
                    ),
                ),
                (
                    "item_id",
                    models.CharField(
                        blank=True,
                        db_index=True,
                        help_text="Generic reference to related item",
                        max_length=255,
                        null=True,
                    ),
                ),
                (
                    "request_id",
                    models.UUIDField(blank=True, db_index=True, help_text="Request ID for correlation", null=True),
                ),
                ("trace_id", models.UUIDField(blank=True, db_index=True, help_text="Distributed trace ID", null=True)),
                (
                    "resolved",
                    models.BooleanField(db_index=True, default=False, help_text="Whether this error has been resolved"),
                ),
                (
                    "resolved_at",
                    models.DateTimeField(
                        blank=True, db_index=True, help_text="When this error was resolved", null=True
                    ),
                ),
                (
                    "resolution_notes",
                    models.TextField(blank=True, help_text="Notes about how the error was resolved", null=True),
                ),
                (
                    "occurred_at",
                    models.DateTimeField(auto_now_add=True, db_index=True, help_text="When this error occurred"),
                ),
                (
                    "resolved_by",
                    models.ForeignKey(
                        blank=True,
                        help_text="User who resolved this error",
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="resolved_pipeline_errors",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "verbose_name": "Pipeline Error",
                "verbose_name_plural": "Pipeline Errors",
                "ordering": ["-occurred_at"],
                "indexes": [
                    models.Index(fields=["severity", "occurred_at"], name="core_pipeli_severit_9c8037_idx"),
                    models.Index(fields=["function_name", "occurred_at"], name="core_pipeli_functio_efb015_idx"),
                    models.Index(fields=["resolved", "occurred_at"], name="core_pipeli_resolve_cd60c5_idx"),
                ],
            },
        ),
    ]
