# Generated by ChatGPT move ssh account
from django.db import migrations, models
import django.db.models.deletion

import apps.credentials.models
import apps.sigils.fields


def migrate_content_type(apps, schema_editor):
    ContentType = apps.get_model("contenttypes", "ContentType")
    ContentType.objects.filter(app_label="nodes", model="sshaccount").update(
        app_label="credentials"
    )


def reverse_content_type(apps, schema_editor):
    ContentType = apps.get_model("contenttypes", "ContentType")
    ContentType.objects.filter(
        app_label="credentials", model="sshaccount"
    ).update(app_label="nodes")


class Migration(migrations.Migration):

    dependencies = [
        ("nodes", "0012_move_sshaccount_to_credentials"),
    ]

    operations = [
        migrations.SeparateDatabaseAndState(
            database_operations=[],
            state_operations=[
                migrations.CreateModel(
                    name="SSHAccount",
                    fields=[
                        (
                            "id",
                            models.BigAutoField(
                                auto_created=True,
                                primary_key=True,
                                serialize=False,
                                verbose_name="ID",
                            ),
                        ),
                        ("is_seed_data", models.BooleanField(default=False, editable=False)),
                        ("is_user_data", models.BooleanField(default=False, editable=False)),
                        ("is_deleted", models.BooleanField(default=False, editable=False)),
                        ("username", models.CharField(max_length=150)),
                        (
                            "password",
                            apps.sigils.fields.SigilShortAutoField(
                                blank=True,
                                help_text="Password for password-based authentication.",
                                max_length=255,
                            ),
                        ),
                        (
                            "private_key",
                            models.FileField(
                                blank=True,
                                help_text="Optional private key for key-based authentication.",
                                upload_to=apps.credentials.models.ssh_key_upload_path,
                            ),
                        ),
                        (
                            "public_key",
                            models.FileField(
                                blank=True,
                                help_text="Optional public key for key-based authentication.",
                                upload_to=apps.credentials.models.ssh_key_upload_path,
                            ),
                        ),
                        ("created_at", models.DateTimeField(auto_now_add=True)),
                        ("updated_at", models.DateTimeField(auto_now=True)),
                        (
                            "node",
                            models.ForeignKey(
                                on_delete=django.db.models.deletion.CASCADE,
                                related_name="ssh_accounts",
                                to="nodes.node",
                            ),
                        ),
                    ],
                    options={
                        "verbose_name": "SSH Account",
                        "verbose_name_plural": "SSH Accounts",
                        "ordering": ("username", "pk"),
                        "unique_together": {("node", "username")},
                        "db_table": "nodes_sshaccount",
                    },
                )
            ],
        ),
        migrations.RunPython(migrate_content_type, reverse_content_type),
    ]
