from django.core.management.base import BaseCommand

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

class Command(BaseCommand):
    def handle(self, *args, **options):
        redis_data={
            "update":{},
        }
        redis_table = "activity"

        models = (
            TtActivity.objects
            .prefetch_related("location_suitability")
        )
        total = 0
        for val in models:
            # only location requirement = 0 and got suitability need update this column to 1
            location_suitability = [ls.id for ls in val.location_suitability.all()]
            if location_suitability:
                total += 1
                redis_data["update"].setdefault(redis_table, []).append({
                    "id": val.id,
                    "location_requirement_type": TtActivity.LOCATION_REQUIREMENT_TYPE['wildcard'],
                })

        bulk_sync_to_redis(redis_client, redis_data)
        print("activity count:"+str(total))

