from django.db import migrations


# Restore only recognizable, fully question-marked copies of the existing
# poultry letterhead. Leave custom, blank, and readable branch text untouched.
TEXT = {
    "local_name": "جاوید تسل تجارتی شرکت",
    "invoice_tagline": "د نړۍ د معتبرو کمپنیو څخه حیواني درمل، واکسین او زراعتي درمل، سامان آلات په لوړه معیار او مناسب قیمت تر لاسه کولای شئ.",
    "contact_person": "جاوید (تسل) / ډاکټر هدایت الله",
    "facebook": "جاوید تسل تجارتی شرکت",
    "address": "جلال آباد ښار، د درمسال چوک، نور عمر پلازه زیر زمین 5-6-7 نمبر دفتر",
}


def repair_poultry_text(apps, schema_editor):
    model = apps.get_model("shop_management", "CompanySetting")
    for profile in model.objects.using(schema_editor.connection.alias).filter(invoice_template="jawid_poultry"):
        updates = {}
        for field, original in TEXT.items():
            masked = "".join("?" if ord(char) > 127 else char for char in original)
            if getattr(profile, field) == masked:
                updates[field] = original
        if updates:
            model.objects.using(schema_editor.connection.alias).filter(pk=profile.pk).update(**updates)


class Migration(migrations.Migration):
    dependencies = [("shop_management", "0118_sale_replaces_sale")]
    operations = [migrations.RunPython(repair_poultry_text, migrations.RunPython.noop)]
