from collections import defaultdict

from django.db.models import Prefetch
from rest_framework import serializers, status

from api.models import TtLocationSuitability, TtSetting, TtModule, TtWeekPattern, TtAvailability, TtStartPreference, \
    TtUsagePreference, TtWeek, TtAcademicTerm
from api.models.audit_trail import AuditTrail
from api.models.audit_trail_details import AuditTrailDetails
from api.views.admin.base import AdminApiBase
from api.validator import BaseValidator
from api.utils import decrypt_aes_128_cbc, encrypt_aes_128_cbc, generate_kafka_data_update, log_critical_error, get_exception_detail, \
    get_add_new_data, get_ip, get_old_new_data, bulk_sync_to_redis, get_multiple_old_new_data, search_old_new_data, \
    convert_to_redis_week
from rest_framework.exceptions import ValidationError
from api.translation import __
from backend.kafka import send_request
from backend.redis_client import redis_client


class ModuleUpdate(AdminApiBase):
    def validate_request(self,request,settings):
        rules = {
            "id": "required|array|exists:api.TtModule,id",
        }
        # if need rename field, can put here
        attribute = {
            # "email": __("attr.email"),
        }
        # validate id first, cause need get module by id, if wrong id direct return error
        validator = BaseValidator(request.data,rules,attribute)
        error = validator.validate()
        if error:
            raise ValidationError(error)

        edit_module = (
            TtModule.objects
            .select_related("department","academic_term","week_pattern","availability","start_preference","usage_preference")
            .prefetch_related(
                "week",#1
                "tag__tag" # tags means the relation add in models, __tag means the relation table tag_id get the tag object #2
            )
            .filter(id__in=request.data.get("id"))
        )

        slot_per_week = settings["slot_per_week"]

        rules = {
            "name": "nullable",
            "desc": "nullable",
            "department_id": "nullable|exists:api.TtDepartment,id",
            # "academic_term_id": "nullable|exists:api.TtAcademicTerm,id",
            "planned_size": "nullable|integer",
            # "real_size": "nullable|integer",
            "credit_provided": "nullable|integer",
            "week_pattern_id": "nullable|exists:api.TtWeekPattern,id",
            "week_pattern": "nullable|array|exists:api.TtWeek,id",
            "availability_id": "nullable|exists:api.TtAvailability,id",
            "availability_pattern": "nullable|char_count:" + slot_per_week + "|regex:[01]+",
            "start_preference_id": "nullable|exists:api.TtStartPreference,id",
            "start_preference_pattern": "nullable|char_count:" + slot_per_week + "|regex:[1-9]+",
            "usage_preference_id": "nullable|exists:api.TtUsagePreference,id",
            "usage_preference_pattern": "nullable|char_count:" + slot_per_week + "|regex:[1-9]+",
            "tag": "nullable|array|exists:api.TtTag,id",
        }

        if request.data.get("code") and edit_module.count() == 1:
            module = edit_module.first()
            if request.data.get("code") != module.code:
                rules['code'] = "nullable|not_exists:api.TtModule,code"

        # if need rename field, can put here
        attribute = {
            # "email": __("attr.email"),
        }

        validator = BaseValidator(request.data,rules,attribute)
        error = validator.validate()

        # custom error after basic validation

        if error:
            raise ValidationError(error)

        return edit_module


    def post(self, request):
        # Validate input
        try:
            setting_params = {
                "minute_per_slot",
                "slot_per_week"
            }
            settings = TtSetting.get_multiple_setting(setting_params)
            edit_module = self.validate_request(request,settings)
            minute_per_slot = int(settings["minute_per_slot"])
            first_activity = edit_module.first()

            academic_term_id = first_activity.academic_term_id
            academic_term = TtAcademicTerm.objects.filter(id=academic_term_id).prefetch_related(Prefetch("week", queryset=TtWeek.objects.order_by("week"))).first()

            default_availability_pattern = academic_term.default_availability
            default_start_preference_pattern = academic_term.default_start
            default_usage_preference_pattern = academic_term.default_usage

            default_week_pattern = [w.id for w in academic_term.week.all()]
            week_pattern_for_redis = convert_to_redis_week(list([w.week for w in academic_term.week.all()]))

            action="module_update" # this is for audit log only

            # this 3 pattern if got give id, force pattern to null
            availability_id = request.data.get("availability_id")
            availability_pattern = request.data.get("availability_pattern")
            if availability_id:
                availability_pattern = None

            start_preference_id = request.data.get("start_preference_id")
            start_preference_pattern = request.data.get("start_preference_pattern")
            if start_preference_id:
                start_preference_pattern = None

            usage_preference_id = request.data.get("usage_preference_id")
            usage_preference_pattern = request.data.get("usage_preference_pattern")
            if usage_preference_id:
                usage_preference_pattern = None

            updated = False
            update_fields = {}
            # not allow update academic_term, so even FE got give also ignore
            ignore_fields = ['id','academic_term_id','tag','timestamp','signature']
            # if more than 1 module edit, ignore code
            if edit_module.count() > 1 or not request.data.get("code"):
                ignore_fields.append('code')
            valid_fields = {f.column for f in TtModule._meta.fields}
            for key, val in request.data.items():
                if key not in ignore_fields and key in valid_fields:
                    updated = True
                    match key:
                        case "availability_pattern" | "start_preference_pattern" | "usage_preference_pattern":
                            # got extra handle the value for these key, so the param same name as the key, so can using this code to handle
                            update_fields[key] = locals()[key]
                        case _:
                            # default the key will same as db column name
                            update_fields[key] = val

            # if FE got pass in either 1 param, will start do checking based on pass in data need update to default pattern or not
            # if both is None/0/"", means will update to default pattern
            if "availability_id" in request.data or "availability_pattern" in request.data:
                if not availability_id and not availability_pattern:
                    availability_pattern = default_availability_pattern
                    update_fields['availability_pattern'] = availability_pattern
            if "start_preference_id" in request.data or "start_preference_pattern" in request.data:
                if not start_preference_id and not start_preference_pattern:
                    start_preference_pattern = default_start_preference_pattern
                    update_fields['start_preference_pattern'] = start_preference_pattern
            if "usage_preference_id" in request.data or "usage_preference_pattern" in request.data:
                if not usage_preference_id and not usage_preference_pattern:
                    usage_preference_pattern = default_usage_preference_pattern
                    update_fields['usage_preference_pattern'] = usage_preference_pattern

            # be4 save need get the old new data already, because bulk update is no matter got change or no changes also will update
            old_new_data = get_multiple_old_new_data(edit_module,request.data.items(),ignore_fields,valid_fields)
            if update_fields: # 2026-04-27, ignore updated param, it is useless, this code is replace by "replace in file", so didnt remove param updated
                update_fields['updated_by'] = request.user.id
                edit_module.update(**update_fields)

            # custom week pattern store in relation, so need handle like this
            # if both not passing in, will not touch this, if got pass in [] or "", will handle also
            week_pattern_id = request.data.get("week_pattern_id")
            week_pattern = request.data.get("week_pattern")
            if "week_pattern" in request.data or "week_pattern_id" in request.data:
                # if is None, put in the default week_pattern first, so if got id, will reset to null, if no id, will update to default week_pattern
                if not week_pattern:
                    week_pattern = default_week_pattern
                else:
                    # if not empty of week_pattern, will get this data for update redis
                    # week store in redis need - 1, cause schedule there week is start from 0
                    week_pattern_for_redis = convert_to_redis_week(list(TtWeek.objects.filter(id__in=week_pattern).values_list("week",flat=True)))

                if week_pattern_id:
                    week_pattern = None
                    week_pattern_for_redis = []
                old_new_data = self.update_m2m_field_bulk(edit_module,'week',week_pattern,old_new_data)

            if "tag" in request.data:
                new_ids = request.data.get("tag", [])
                old_new_data = self.update_m2m_field_bulk(edit_module,'tag',new_ids,old_new_data,False,True)

            redis_data = {
                "update": {}
            }

            redis_module_table = "module"
            redis_module_data = {}

            redis_module_column = [
                "name","department_id","academic_term_id","planned_size","real_size","credit_provided",
                "availability_id","availability_pattern","start_preference_id","week_pattern_id",
                "start_preference_pattern","usage_preference_id","usage_preference_pattern"
            ]

            # if only 1 module, can edit code
            if edit_module.count() == 1:
                redis_module_column.append('code')
            # got new data only means got update, then will for loop request.data, cause the old_new_data['new_data'] for foreign key name, like department_id, will return in department, so cant use it
            if old_new_data['new_data']:
                for edit_id in request.data.get("id"):
                    # every new module reset the redis_module_data and append to redis_data['update']
                    redis_module_data = {"id": edit_id}
                    for key, val in request.data.items():
                        if key in redis_module_column:
                            if key in ["usage_preference_id", "availability_id", "start_preference_id","week_pattern_id"]:
                                # jose request if no id, dun store null, want to store "0"
                                redis_module_data[key] = 0 if val in (None, "") else val
                            else:
                                redis_module_data[key] = val
                    # this must custom the handle, for pattern, if not in some scenario will store null in redis when no id
                    if "availability_id" in request.data or "availability_pattern" in request.data:
                        redis_module_data["availability_pattern"] = availability_pattern
                    if "start_preference_id" in request.data or "start_preference_pattern" in request.data:
                        redis_module_data["start_preference_pattern"] = start_preference_pattern
                    if "usage_preference_id" in request.data or "usage_preference_pattern" in request.data:
                        redis_module_data["usage_preference_pattern"] = usage_preference_pattern
                    if "week_pattern_id" in request.data or "week_pattern" in request.data:
                        redis_module_data["week_pattern"] = week_pattern_for_redis or []
                    if len(redis_module_data)>1:# > 1 means not only id
                        redis_data["update"].setdefault(redis_module_table, []).append(redis_module_data)

            bulk_sync_to_redis(redis_client,redis_data)

            if old_new_data['old_data'] or old_new_data['new_data']:
                #kafka update/ split by id
                kafka_module = []
                method = "module_update"
                kafka_topic = self.kafka_config['MICROSERVICES_TT_TOPIC']
                if kafka_topic:
                    new_data = old_new_data.get("new_data")
                    relation_fields = [
                        "week",
                        "tag"
                    ]
                    relation_data = generate_kafka_data_update(new_data, relation_fields)
                    update_fields.pop("updated_by", None)
                    for module in edit_module:
                        data = {"id": module.id, **update_fields, **relation_data}
                        kafka_module.append(data)
                    kafka_request_data = {
                        "session_id": request.user.name,
                        "module": kafka_module,
                    }
                    send_request(kafka_topic, kafka_request_data, None, method)

                # insert audit trail and details
                # can insert the parent first, then only insert details
                audit_trail = AuditTrail.objects.create(
                    user_id=request.user.id,
                    type=self.audit_type,
                    ip_address=get_ip(request)
                )

                name = ",".join(edit_module.values_list("name", flat=True))
                remark_param = {
                    "name":name
                }

                # call to a function for insert details, so in future if want change to used worker, can modify in function only
                AuditTrailDetails.custom_insert(
                    audit_trail=audit_trail,
                    action=action,
                    remark_param=remark_param,
                    new_data=old_new_data['new_data'],
                    old_data=old_new_data['old_data'],
                )

            response = {

            }

            return self.api_response(data=response)
        except ValidationError as e:
            first_message = e.detail['error']
            errors = e.detail['errors']
            return self.api_response(error=first_message,errors=errors,code=status.HTTP_400_BAD_REQUEST)
        except Exception as e:
            e_details = get_exception_detail(e)
            log_critical_error(user_id=None,descr=e_details['descr'],url=e_details['url'],trace=e_details['trace'])
            return self.api_response(error=__("message.internal_server_error"),code=status.HTTP_500_INTERNAL_SERVER_ERROR)