# Generated by Django 4.2.29 on 2026-03-26 15:13

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


class Migration(migrations.Migration):
    initial = True

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name="EDSCertificate",
            fields=[
                (
                    "id",
                    models.AutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "subject",
                    models.CharField(
                        help_text="Certificate subject DN", max_length=255
                    ),
                ),
                (
                    "issuer",
                    models.CharField(help_text="Certificate issuer DN", max_length=255),
                ),
                (
                    "serial_number",
                    models.CharField(
                        help_text="Certificate serial number",
                        max_length=255,
                        unique=True,
                    ),
                ),
                (
                    "valid_from",
                    models.DateTimeField(help_text="Certificate validity start"),
                ),
                (
                    "valid_to",
                    models.DateTimeField(help_text="Certificate validity end"),
                ),
                (
                    "public_key_pem",
                    models.TextField(
                        help_text="X.509 certificate in PEM format (public key only)"
                    ),
                ),
                (
                    "thumbprint",
                    models.CharField(
                        help_text="SHA256 thumbprint of certificate (hex-encoded)",
                        max_length=64,
                        unique=True,
                    ),
                ),
                (
                    "is_revoked",
                    models.BooleanField(
                        default=False, help_text="Certificate revocation status"
                    ),
                ),
                ("revoked_at", models.DateTimeField(blank=True, null=True)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                (
                    "user",
                    models.OneToOneField(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="eds_certificate",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "verbose_name": "EDS Certificate",
                "verbose_name_plural": "EDS Certificates",
            },
        ),
        migrations.CreateModel(
            name="FailedAuthenticationAttempt",
            fields=[
                (
                    "id",
                    models.AutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("username", models.CharField(db_index=True, max_length=150)),
                ("ip_address", models.GenericIPAddressField(db_index=True)),
                (
                    "attempt_type",
                    models.CharField(
                        choices=[
                            ("eds_signature", "EDS Signature Authentication"),
                            ("certificate_mismatch", "Certificate Mismatch"),
                            ("invalid_nonce", "Invalid Nonce"),
                            ("certificate_revoked", "Certificate Revoked"),
                            ("certificate_expired", "Certificate Expired"),
                            ("signature_invalid", "Signature Invalid"),
                        ],
                        max_length=50,
                    ),
                ),
                ("reason", models.TextField(blank=True)),
                (
                    "attempt_number",
                    models.IntegerField(
                        default=1, help_text="Which attempt in current cycle"
                    ),
                ),
                (
                    "is_locked",
                    models.BooleanField(
                        default=False, help_text="Was account locked when attempt made"
                    ),
                ),
                ("user_agent", models.TextField(blank=True)),
                ("certificate_serial", models.CharField(blank=True, max_length=255)),
                ("timestamp", models.DateTimeField(auto_now_add=True, db_index=True)),
            ],
            options={
                "verbose_name": "Failed Authentication Attempt",
                "verbose_name_plural": "Failed Authentication Attempts",
                "ordering": ["-timestamp"],
                "indexes": [
                    models.Index(
                        fields=["username", "-timestamp"],
                        name="django_eds__usernam_68e517_idx",
                    ),
                    models.Index(
                        fields=["ip_address", "-timestamp"],
                        name="django_eds__ip_addr_b747d0_idx",
                    ),
                    models.Index(
                        fields=["is_locked", "-timestamp"],
                        name="django_eds__is_lock_e2e7d0_idx",
                    ),
                ],
            },
        ),
        migrations.CreateModel(
            name="SignatureVerificationLog",
            fields=[
                (
                    "id",
                    models.AutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "status",
                    models.CharField(
                        choices=[
                            ("success", "Successful verification"),
                            ("failed_signature", "Signature verification failed"),
                            ("failed_nonce", "Nonce validation failed"),
                            ("failed_cert", "Certificate validation failed"),
                            ("failed_revoked", "Certificate revoked"),
                            ("failed_expired", "Certificate expired"),
                            ("failed_invalid_format", "Invalid signature format"),
                            ("error", "Verification error"),
                        ],
                        max_length=25,
                    ),
                ),
                (
                    "message",
                    models.TextField(
                        blank=True, help_text="Details about verification result"
                    ),
                ),
                ("ip_address", models.GenericIPAddressField(blank=True, null=True)),
                ("user_agent", models.TextField(blank=True)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                (
                    "certificate",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="verification_logs",
                        to="django_eds_auth.edscertificate",
                    ),
                ),
                (
                    "user",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="signature_logs",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "verbose_name": "Signature Verification Log",
                "verbose_name_plural": "Signature Verification Logs",
                "indexes": [
                    models.Index(
                        fields=["status", "-created_at"],
                        name="django_eds__status_8b75b1_idx",
                    ),
                    models.Index(
                        fields=["user", "-created_at"],
                        name="django_eds__user_id_450d95_idx",
                    ),
                ],
            },
        ),
        migrations.CreateModel(
            name="AuthenticationNonce",
            fields=[
                (
                    "id",
                    models.AutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("nonce", models.CharField(db_index=True, max_length=128, unique=True)),
                ("is_used", models.BooleanField(default=False)),
                ("expires_at", models.DateTimeField(db_index=True)),
                ("used_at", models.DateTimeField(blank=True, null=True)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                (
                    "user",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="auth_nonces",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "verbose_name": "Authentication Nonce",
                "verbose_name_plural": "Authentication Nonces",
                "indexes": [
                    models.Index(
                        fields=["nonce", "is_used"], name="django_eds__nonce_136a64_idx"
                    ),
                    models.Index(
                        fields=["expires_at"], name="django_eds__expires_ff4b13_idx"
                    ),
                ],
            },
        ),
    ]
