# Generated by Django 5.2.11 on 2026-02-24 19:47

from django.db import migrations

from edc_offstudy.constants import COMPLETED_FOLLOWUP
from meta_prn.models import OffStudyMedication


def update_reasons(apps, schema_editor):
    other_reasons = [
        "Patient has reached month 48",
        "end of study",
        "Participant has reached month48 follow up",
        "The patient has completed three years and does not wish to reconsent",
        "Participant has reached month 48-month", "completed 36 months follow up",
        "completed 36 months of follow up",
        "Patient has reached month 36 and has declined to re-consent",
        "Reached 36 months refused to re consent",
        "reached month 48",
        (
            "Patient reached month 36 asked time to think before she reconsent "
            "but eventually decided not to continue with the trial"
        ),
        "Completed 36 months of treatment",
        "Reached month 36 does not wish to continue with the trial",
        "reached month 36 and declined reconsenting",
        "Patient. has reach month 48",
    ]
    for other_reason in other_reasons:
        for obj in OffStudyMedication.objects.filter(reason_other=other_reason):
            obj.comment = (
                f"{obj.comment} - {obj.reason_other}" if obj.comment else obj.reason_other
            )
            obj.reason = COMPLETED_FOLLOWUP
            obj.save_base(update_fields=["comment", "reason"])


class Migration(migrations.Migration):

    dependencies = [
        (
            "meta_prn",
            "0069_alter_historicaloffstudymedication_singleton_field_and_more",
        ),
    ]

    operations = [migrations.RunPython(update_reasons)]
