# Generated by Django 4.2.23 on 2026-01-22

# Django
from django.db import migrations, models


def _add_field_if_missing(
    apps,
    schema_editor,
    model_name: str,
    field_name: str,
    field: models.Field,
) -> None:
    model = apps.get_model("indy_hub", model_name)
    table_name = model._meta.db_table
    with schema_editor.connection.cursor() as cursor:
        existing_columns = {
            col.name
            for col in schema_editor.connection.introspection.get_table_description(
                cursor, table_name
            )
        }
    if field_name in existing_columns:
        return
    field.set_attributes_from_name(field_name)
    db_type = field.db_type(connection=schema_editor.connection)
    if db_type is None:
        return
    default = field.default
    default_sql = ""
    if default is not models.NOT_PROVIDED:
        default_sql = f" DEFAULT {default}"
    null_sql = "" if field.null else " NOT NULL"
    schema_editor.execute(
        "ALTER TABLE {table} ADD COLUMN {column} {definition}".format(
            table=schema_editor.quote_name(table_name),
            column=schema_editor.quote_name(field.column),
            definition=f"{db_type}{null_sql}{default_sql}",
        )
    )


def add_missing_columns(apps, schema_editor):
    _add_field_if_missing(
        apps,
        schema_editor,
        "charactersettings",
        "jobs_notify_custom_hours",
        models.PositiveSmallIntegerField(default=6),
    )
    _add_field_if_missing(
        apps,
        schema_editor,
        "charactersettings",
        "corp_jobs_notify_custom_hours",
        models.PositiveSmallIntegerField(default=6),
    )
    _add_field_if_missing(
        apps,
        schema_editor,
        "corporationsharingsetting",
        "corp_jobs_notify_custom_hours",
        models.PositiveSmallIntegerField(default=6),
    )


class Migration(migrations.Migration):

    replaces = [
        ("indy_hub", "0071_add_job_notification_custom_hours"),
        (
            "indy_hub",
            "0072_alter_charactersettings_corp_jobs_notify_frequency_and_more",
        ),
        ("indy_hub", "0073_ensure_job_notification_custom_hours_columns"),
    ]

    dependencies = [
        ("indy_hub", "0070_rename_cached_assets_tables"),
    ]

    operations = [
        migrations.SeparateDatabaseAndState(
            state_operations=[
                migrations.AddField(
                    model_name="charactersettings",
                    name="jobs_notify_custom_hours",
                    field=models.PositiveSmallIntegerField(default=6),
                ),
                migrations.AddField(
                    model_name="charactersettings",
                    name="corp_jobs_notify_custom_hours",
                    field=models.PositiveSmallIntegerField(default=6),
                ),
                migrations.AddField(
                    model_name="corporationsharingsetting",
                    name="corp_jobs_notify_custom_hours",
                    field=models.PositiveSmallIntegerField(default=6),
                ),
                migrations.AlterField(
                    model_name="charactersettings",
                    name="corp_jobs_notify_frequency",
                    field=models.CharField(
                        choices=[
                            ("disabled", "Disabled"),
                            ("immediate", "Immediate"),
                            ("daily", "Daily digest"),
                            ("weekly", "Weekly digest"),
                            ("monthly", "Monthly digest"),
                            ("custom", "Custom cadence"),
                            ("custom_hours", "Hourly digest"),
                        ],
                        default="disabled",
                        max_length=20,
                    ),
                ),
                migrations.AlterField(
                    model_name="charactersettings",
                    name="jobs_notify_frequency",
                    field=models.CharField(
                        choices=[
                            ("disabled", "Disabled"),
                            ("immediate", "Immediate"),
                            ("daily", "Daily digest"),
                            ("weekly", "Weekly digest"),
                            ("monthly", "Monthly digest"),
                            ("custom", "Custom cadence"),
                            ("custom_hours", "Hourly digest"),
                        ],
                        default="disabled",
                        max_length=20,
                    ),
                ),
                migrations.AlterField(
                    model_name="corporationsharingsetting",
                    name="corp_jobs_notify_frequency",
                    field=models.CharField(
                        choices=[
                            ("disabled", "Disabled"),
                            ("immediate", "Immediate"),
                            ("daily", "Daily digest"),
                            ("weekly", "Weekly digest"),
                            ("monthly", "Monthly digest"),
                            ("custom", "Custom cadence"),
                            ("custom_hours", "Hourly digest"),
                        ],
                        default="disabled",
                        max_length=20,
                    ),
                ),
            ],
            database_operations=[
                migrations.RunPython(add_missing_columns, migrations.RunPython.noop),
                migrations.AlterField(
                    model_name="charactersettings",
                    name="corp_jobs_notify_frequency",
                    field=models.CharField(
                        choices=[
                            ("disabled", "Disabled"),
                            ("immediate", "Immediate"),
                            ("daily", "Daily digest"),
                            ("weekly", "Weekly digest"),
                            ("monthly", "Monthly digest"),
                            ("custom", "Custom cadence"),
                            ("custom_hours", "Hourly digest"),
                        ],
                        default="disabled",
                        max_length=20,
                    ),
                ),
                migrations.AlterField(
                    model_name="charactersettings",
                    name="jobs_notify_frequency",
                    field=models.CharField(
                        choices=[
                            ("disabled", "Disabled"),
                            ("immediate", "Immediate"),
                            ("daily", "Daily digest"),
                            ("weekly", "Weekly digest"),
                            ("monthly", "Monthly digest"),
                            ("custom", "Custom cadence"),
                            ("custom_hours", "Hourly digest"),
                        ],
                        default="disabled",
                        max_length=20,
                    ),
                ),
                migrations.AlterField(
                    model_name="corporationsharingsetting",
                    name="corp_jobs_notify_frequency",
                    field=models.CharField(
                        choices=[
                            ("disabled", "Disabled"),
                            ("immediate", "Immediate"),
                            ("daily", "Daily digest"),
                            ("weekly", "Weekly digest"),
                            ("monthly", "Monthly digest"),
                            ("custom", "Custom cadence"),
                            ("custom_hours", "Hourly digest"),
                        ],
                        default="disabled",
                        max_length=20,
                    ),
                ),
            ],
        )
    ]
