# Generated by Django 4.2.11 on 2024-06-17 12:01

from django.db import migrations
from django.db.migrations import RunPython
from edc_constants.constants import FEMALE, MALE
from edc_utils import get_utcnow


def update_gender(apps, schema_editor):
    """Update gender from 'F' to 'M' for participant added to patient
    log (and subsequently screened and consented) with the wrong gender.

    See also:
        Redmine ticket: #810
        Protocol incident: VWFD-NPT2
    """
    for model in ["intecomm_consent.subjectconsent", "edc_registration.registeredsubject"]:
        model_cls = apps.get_model(model)
        obj = model_cls.objects.get(
            subject_identifier="107-105-0080-1",
            screening_identifier="SYKEZZEV",
            site_id=105,
            gender=FEMALE,
        )
        print(
            f"\n  * Updating {model_cls.__name__} gender for {obj.subject_identifier=} "
            f"({obj.screening_identifier=}) from '{FEMALE}' to '{MALE}' ..."
        )

        obj.modified = get_utcnow()
        obj.user_modified = __name__ if len(__name__) <= 50 else f"{__name__[:46]} ..."
        obj.gender = MALE
        obj.save_base(
            update_fields=[
                "gender",
                "modified",
                "user_modified",
            ]
        )

        print("  * Updated.")
    print("Done.")


class Migration(migrations.Migration):

    dependencies = [
        ("intecomm_consent", "0025_alter_historicalsubjectconsent_guardian_name_and_more"),
        ("intecomm_screening", "0072_data_fix_810_gender_update"),
    ]

    operations = [RunPython(update_gender)]
