from django.db import migrations, models


def deprecate_nhif_deductions(apps, schema_editor):
    Deduction = apps.get_model("human_resource", "Deduction")
    StaffDeductionScheme = apps.get_model("human_resource", "StaffDeductionScheme")

    nhif_deductions = Deduction.objects.filter(deduction_title="NHIF")
    for deduction in nhif_deductions:
        StaffDeductionScheme.objects.filter(deduction_id=deduction.id).delete()
    nhif_deductions.update(recycle_bin=True)

    Deduction.objects.filter(
        deduction_title="SHIF",
        recycle_bin=False,
    ).update(
        deduction_title="SHA",
        deduction_description="Social Health Authority contribution",
    )


class Migration(migrations.Migration):

    dependencies = [
        ("human_resource", "0008_hr_payroll_settings"),
    ]

    operations = [
        migrations.RemoveField(
            model_name="hrpayrollsettings",
            name="skip_legacy_nhif_scheme",
        ),
        migrations.RunPython(deprecate_nhif_deductions, migrations.RunPython.noop),
    ]
