from collections import defaultdict

from rest_framework import serializers, status

from api.models import TtLocationSuitability, TtSetting, TtPosModuleGroupModule, TtAcademicTerm, TtPos, TtModule
from api.models.audit_trail import AuditTrail
from api.models.audit_trail_details import AuditTrailDetails
from api.models.tt_student_set import TtStudentSet
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, \
    get_slot_default_pattern
from rest_framework.exceptions import ValidationError
from api.translation import __
from backend.kafka import send_request
from backend.redis_client import redis_client


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

        edit_student_set = (
            TtStudentSet.objects
            .select_related("department","academic_term","pos","availability","start_preference","usage_preference")
            .prefetch_related(
                "module",
                "constraint_profile",
                "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",
            "pos_id": "nullable|exists:api.TtPos,id",
            "module": "nullable|array|exists:api.TtModule,id",
            "planned_size": "nullable|integer",
            "constraint_profile": "nullable|array|exists:api.TtConstraintProfile,id",
            "tag": "nullable|array|exists:api.TtTag,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]+",
        }

        if request.data.get("code") and edit_student_set.count() == 1:
            student_set = edit_student_set.first()
            if request.data.get("code") != student_set.code:
                rules['code'] = "nullable|not_exists:api.TtStudentSet,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)

        pos_id = ""
        module_val = request.data.get("module")
        # if FE got pass in pos_id, module cant be empty or FE got pass param module, also not allow [],""
        # 2026-04-24 pos_id allow empty, so become module also allow empty, no need this checking
        # if (request.data.get("pos_id") and not module_val) or ("module" in request.data and not module_val):
        #     raise ValidationError({
        #         "error": __('validation.cannot_be_empty', field=__("attr.module")),
        #         "errors": {"module": [__('validation.cannot_be_empty', field=__("attr.module"))]}
        #     })

        pos_ids = set(edit_student_set.values_list("pos_id", flat=True))
        # if all student set pos_id is null, wont do checking, at the end also wont update for module
        if (len(pos_ids) == 1 and None not in pos_ids) or request.data.get("pos_id"):
            # if only change pos but no pass in module, will become true and reset all related student_set_module relation data
            update_module = False
            # first id in pos_ids
            pos_id = next(iter(pos_ids), None)
            if request.data.get("pos_id"):
                pos_id = request.data.get("pos_id")
                update_module = True

            input_module = set(request.data.get("module", []))
            if update_module or input_module:
                module_ids = set(
                    TtPosModuleGroupModule.objects
                    .filter(pos_module_group__pos_id=pos_id)
                    .values_list("module_id", flat=True)
                )
                # find missing ones
                not_exist_modules = input_module - module_ids
                selected_pos = TtPos.objects.get(id=pos_id)
                if not_exist_modules:
                    selected_modules = TtModule.objects.filter(id__in=request.data.get("module"))
                    pos_name = selected_pos.name
                    not_found_names = [
                        m.name for m in selected_modules if m.id in not_exist_modules
                    ]
                    not_found_module_name = ", ".join(not_found_names)
                    raise ValidationError({
                        "error": __('validation.module_not_in_pos', module_name=not_found_module_name,pos_name=pos_name),
                        "errors": {"module": [__('validation.module_not_in_pos', module_name=not_found_module_name,pos_name=pos_name)]}
                    })

        return edit_student_set


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

            slot_default_pattern = get_slot_default_pattern(slot_per_week)

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

            # since before update FE site will choose adademic term to display, all the data will in same academic term, just get either 1 to get academic_term
            # this academic_term data is just for default pattern
            first_student_set = edit_student_set.first()
            academic_term = TtAcademicTerm.objects.get(id=first_student_set.academic_term_id)

            # 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 = {}
            ignore_fields = ['id','constraint_profile','academic_term_id','tag','timestamp','signature']
            # if more than 1 student_set edit, ignore code
            if edit_student_set.count() > 1 or not request.data.get("code"):
                ignore_fields.append('code')
            valid_fields = {f.column for f in TtStudentSet._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 = academic_term.default_availability
                    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 = academic_term.default_start
                    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 = academic_term.default_usage
                    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_student_set,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_student_set.update(**update_fields)

            # all in same pos_id only can update module, if not will not allow update module
            # if got update pos_id, also allow to update module
            pos_ids = set(edit_student_set.values_list("pos_id", flat=True))
            input_module = None
            if (len(pos_ids) == 1 and None not in pos_ids) or request.data.get("pos_id"):
                # got pass in module from post api only update module
                if "module" in request.data:
                    input_module = request.data.get("module", [])
                    old_new_data = self.update_m2m_field_bulk(edit_student_set,'module',input_module,old_new_data)
            elif "pos_id" in request.data:
                input_module = []
                old_new_data = self.update_m2m_field_bulk(edit_student_set, 'module', input_module, old_new_data)

            if "constraint_profile" in request.data:
                new_ids = request.data.get("constraint_profile", [])
                old_new_data = self.update_m2m_field_bulk(edit_student_set,'constraint_profile',new_ids,old_new_data)

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

            redis_data = {
                "update": {}
            }

            redis_student_set_table = "student_set"
            redis_student_set_data = {}
            redis_student_set_column = [
                "name","department_id","pos_id","constraint_profile","planned_size","module_id","availability_id","availability_pattern","start_preference_id",
                "start_preference_pattern","usage_preference_id","usage_preference_pattern"
            ]

            # if only 1 student_set, can edit code
            if edit_student_set.count() == 1:
                redis_student_set_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 student_set reset the redis_student_set_data and append to redis_data['update']
                    redis_student_set_data = {"id": edit_id}
                    for key, val in request.data.items():
                        if key in redis_student_set_column:
                            if key in ["usage_preference_id", "availability_id", "start_preference_id"]:
                                # jose request if no id, dun store null, want to store "0"
                                redis_student_set_data[key] = 0 if val in (None, "") else val
                            elif key in ["constraint_profile"]:
                                redis_student_set_data[key] = val or []
                            else:
                                redis_student_set_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_student_set_data["availability_pattern"] = availability_pattern
                    if "start_preference_id" in request.data or "start_preference_pattern" in request.data:
                        redis_student_set_data["start_preference_pattern"] = start_preference_pattern
                    if "usage_preference_id" in request.data or "usage_preference_pattern" in request.data:
                        redis_student_set_data["usage_preference_pattern"] = usage_preference_pattern
                    if input_module is not None:
                        redis_student_set_data['module_id'] = input_module or []
                    if len(redis_student_set_data)>1:# > 1 means not only id
                        redis_data["update"].setdefault(redis_student_set_table, []).append(redis_student_set_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_student_set = []
                method = "student_set_update"
                kafka_topic = self.kafka_config['MICROSERVICES_TT_TOPIC']
                if kafka_topic:
                    new_data = old_new_data.get("new_data")
                    relation_fields = ["module","constraint_profile","tag"]
                    relation_data = generate_kafka_data_update(new_data, relation_fields)
                    update_fields.pop("updated_by", None)
                    for stud in edit_student_set:
                        data = {"id": stud.id, **update_fields, **relation_data}
                        kafka_student_set.append(data)
                    kafka_request_data = {
                        "session_id": request.user.name,
                        "student_set": kafka_student_set,
                    }
                    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_student_set.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)