# Generated by Django 4.2.7 on 2023-11-14 22:33
from django.db import migrations
from edc_appointment.constants import MISSED_APPT, NEW_APPT, ONTIME_APPT, SKIPPED_APPT
from edc_constants.constants import CLINIC, COMMUNITY, HOSPITAL, NOT_APPLICABLE
from tqdm import tqdm

from intecomm_visit_schedule.constants import COMMUNITY_SCHEDULE, FACILITY_SCHEDULE


def update_appointment(apps, schema_editor):
    appointment_model_cls = apps.get_model("edc_appointment.appointment")
    appointment_type_model_cls = apps.get_model("edc_appointment.appointmenttype")

    # update_appt_type if null in community
    appt_type_obj = appointment_type_model_cls.objects.get(name=COMMUNITY)
    qs = appointment_model_cls.objects.filter(
        appt_type_id__isnull=True, schedule_name=COMMUNITY_SCHEDULE
    )
    total = qs.count()
    for obj in tqdm(qs, total=total):
        obj.appt_type_id = appt_type_obj.id
        obj.save_base(update_fields=["appt_type_id"])

    # update_appt_type if null in facility
    appt_type_obj = appointment_type_model_cls.objects.get(name=CLINIC)
    qs = appointment_model_cls.objects.filter(
        appt_type_id__isnull=True, schedule_name=FACILITY_SCHEDULE
    )
    total = qs.count()
    for obj in tqdm(qs, total=total):
        obj.appt_type_id = appt_type_obj.id
        obj.save_base(update_fields=["appt_type_id"])

    # update_appt_status from skipped to new in_community
    qs = appointment_model_cls.objects.filter(
        appt_status=SKIPPED_APPT, schedule_name=COMMUNITY_SCHEDULE
    )
    total = qs.count()
    for obj in tqdm(qs, total=total):
        obj.appt_status = NEW_APPT
        obj.appt_timing = ONTIME_APPT
        obj.save()

    # appt_type from N/A to commmunity in community_arm
    appt_type_obj = appointment_type_model_cls.objects.get(name=NOT_APPLICABLE)
    qs = appointment_model_cls.objects.filter(
        appt_type_id=appt_type_obj.id, appt_status=NEW_APPT, schedule_name=COMMUNITY_SCHEDULE
    )
    total = qs.count()
    appt_type_obj = appointment_type_model_cls.objects.get(name=COMMUNITY)
    for obj in tqdm(qs, total=total):
        obj.appt_type_id = appt_type_obj.id
        obj.save()

    # appt_type from N/A to clinic in facility_arm
    appt_type_obj = appointment_type_model_cls.objects.get(name=NOT_APPLICABLE)
    qs = appointment_model_cls.objects.filter(
        appt_type_id=appt_type_obj.id, appt_status=NEW_APPT, schedule_name=FACILITY_SCHEDULE
    )
    total = qs.count()
    appt_type_obj = appointment_type_model_cls.objects.get(name=CLINIC)
    for obj in tqdm(qs, total=total):
        obj.appt_type_id = appt_type_obj.id
        obj.save()

    # appt_timing to ontime if appt_status is NEW
    appointment_model_cls.objects.filter(appt_status=NEW_APPT).update(appt_timing=ONTIME_APPT)

    # appt_type to clinic if facility appt is missed
    appt_type_obj = appointment_type_model_cls.objects.get(name=CLINIC)
    appointment_model_cls.objects.filter(
        appt_timing=MISSED_APPT, schedule_name=FACILITY_SCHEDULE
    ).update(appt_type_id=appt_type_obj.id)

    # appt_type to community if community appt is missed
    appt_type_obj = appointment_type_model_cls.objects.get(name=COMMUNITY)
    appointment_model_cls.objects.filter(
        appt_timing=MISSED_APPT, schedule_name=COMMUNITY_SCHEDULE
    ).update(appt_type_id=appt_type_obj.id)

    # appt_type to clinic if hospital
    hospital = appointment_type_model_cls.objects.get(name=HOSPITAL)
    clinic = appointment_type_model_cls.objects.get(name=CLINIC)
    appointment_model_cls.objects.filter(appt_type_id=hospital.id).update(
        appt_type_id=clinic.id
    )


class Migration(migrations.Migration):
    dependencies = [
        ("intecomm_subject", "0123_alter_subjectvisit_managers"),
    ]

    operations = [migrations.RunPython(update_appointment)]
