from django.core.management.base import BaseCommand

from api.models import TtStaff, TtLocation, TtStudentSet, TtActivityTemplate, TtActivity, TtPathway
from api.utils import convert_to_redis_week, bulk_sync_to_redis
from backend.redis_client import redis_client

"""
JUST A BACKUP TASK FILE, WONT RUN, INCASE IN FUTURE STILL NEED ADD PATHWAY_ID TO STUDENT_SET
"""
class Command(BaseCommand):
    def handle(self, *args, **options):
        to_update = []
        # check student set module and pos_id same with pathway_string_2 and pos_id, assign the pathway_id to student_set
        student_sets = TtStudentSet.objects.all().prefetch_related("module")
        pathways = TtPathway.objects.all().values("id","pathway_string_2","pos_id")
        pathway_lookup = {
            (p["pos_id"], p["pathway_string_2"]): p["id"]
            for p in pathways
        }
        for student_set in student_sets:
            sorted_ids = sorted(val.id for val in student_set.module.all())
            student_set_module_string = ";".join(map(str, sorted_ids)) if sorted_ids else None

            match_key = (student_set.pos_id, student_set_module_string)
            pathway_id = pathway_lookup.get(match_key)
            if pathway_id:
                student_set.pathway_id = pathway_id
                to_update.append(student_set)
        if to_update:
            TtStudentSet.objects.bulk_update(to_update, fields=["pathway_id"])





