# Generated by Django 5.2.9 on 2026-01-11 03:20

from django.db import migrations, models


def drop_nodeservice_table(apps, schema_editor):
    table_name = "nodes_nodeservice"
    connection = schema_editor.connection
    if table_name not in connection.introspection.table_names():
        return
    quoted_name = schema_editor.quote_name(table_name)
    with connection.cursor() as cursor:
        cursor.execute(f"SELECT 1 FROM {quoted_name} LIMIT 1")
        if cursor.fetchone():
            return
    schema_editor.execute(f"DROP TABLE IF EXISTS {quoted_name}")


class Migration(migrations.Migration):

    replaces = [
        ("nodes", "0012_move_sshaccount_to_credentials"),
        ("nodes", "0012_delete_nodemanager"),
        ("nodes", "0013_merge_20260104_1818"),
        ("nodes", "0014_netmessage_lcd_channel_num_and_more"),
        ("nodes", "0015_remove_nodeservice"),
        ("nodes", "0016_alter_node_last_seen"),
        ("nodes", "0017_rename_last_seen_node_last_updated"),
    ]

    dependencies = [
        ("dns", "0003_remove_godaddydnsrecord_node_manager_and_more"),
        ("nodes", "0002_squashed_0011_netmessage_expires_at"),
    ]

    operations = [
        migrations.SeparateDatabaseAndState(
            state_operations=[
                migrations.DeleteModel(
                    name="SSHAccount",
                ),
            ],
        ),
        migrations.DeleteModel(
            name="NodeManager",
        ),
        migrations.AddField(
            model_name="netmessage",
            name="lcd_channel_num",
            field=models.PositiveSmallIntegerField(
                default=0,
                help_text="LCD channel number to target when displaying locally.",
            ),
        ),
        migrations.AddField(
            model_name="netmessage",
            name="lcd_channel_type",
            field=models.CharField(
                blank=True,
                default="low",
                help_text="LCD channel type for local display (for example low or high).",
                max_length=20,
            ),
        ),
        migrations.SeparateDatabaseAndState(
            database_operations=[
                migrations.RunPython(
                    code=drop_nodeservice_table,
                    reverse_code=migrations.RunPython.noop,
                ),
            ],
            state_operations=[
                migrations.DeleteModel(
                    name="NodeService",
                ),
            ],
        ),
        migrations.RenameField(
            model_name="node",
            old_name="last_seen",
            new_name="last_updated",
        ),
        migrations.AlterField(
            model_name="node",
            name="last_updated",
            field=models.DateTimeField(auto_now=True, verbose_name="Last updated"),
        ),
    ]
