import copy
from collections import defaultdict

from django.db.models import Q, Func, Value, F
from django.utils import timezone
from rest_framework import serializers, status

from api.models import TtLocationSuitability, TtSetting, TtModule, TtWeekPattern, TtAvailability, TtStartPreference, \
    TtUsagePreference, TtPos, TtPosModuleGroupModule, TtStudentSetModule, TtPathwayPosModuleGroupModule, TtPathway, \
    TtStudentSet, TtPosModuleGroup
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, \
    get_missing_code_from_db, get_auto_generate_name, get_auto_generate_code
from rest_framework.exceptions import ValidationError
from api.translation import __
from backend.kafka import send_request
from backend.redis_client import redis_client


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

        edit_pos = (
            TtPos.objects
            .select_related("department","academic_term")
            .prefetch_related(
                "module_group",#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"))
        )


        rules = {
            "name": "nullable",
            "desc": "nullable",
            "department_id": "nullable|exists:api.TtDepartment,id",
            # "academic_term_id": "nullable|exists:api.TtAcademicTerm,id",
            "planned_size": "nullable|integer",
            "max_credit": "nullable|integer",
            "module_group": "nullable|array|exists:api.TtModuleGroup,id",
            "tag": "nullable|array|exists:api.TtTag,id",
        }

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

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

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

        if error:
            raise ValidationError(error)

        # custom error after basic validation
        # if custom pattern, get the start time and end time, and compare with academic_term start time and end time, if crash the rules, return validation error

        # if using preset pattern, also check the preset pattern start_time and and end_time compare with academic term start_time and end time

        return edit_pos

    def post(self, request):
        # Validate input
        try:
            edit_pos = self.validate_request(request)
            pos_ids = request.data.get("id")

            action="pos_update" # this is for audit log only
            # remain the old data just for handle update activity name
            edit_pos_copy = copy.deepcopy(list(edit_pos))
            # used to find all affected activity name record
            now = timezone.now()

            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 pos edit, ignore code
            if edit_pos.count() > 1 or not request.data.get("code"):
                ignore_fields.append('code')
            valid_fields = {f.column for f in TtPos._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 _:
                            # default the key will same as db column name
                            update_fields[key] = val

            # 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_pos,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_pos.update(**update_fields)

            # if got update module_group, need check which student_set_module need to delete
            if "module_group" in request.data:
                pos_module_group_idata = []
                # if got pass in module group only need to action
                module_group_ids = request.data.get("module_group", [])
                pos_existing_module_group = {}
                # based on edit pos get old module_group ids first, and check which id need remove, which remain, and which need to add
                for pos_obj in edit_pos:
                    old_module_group_ids = {pos_mg.id for pos_mg in pos_obj.module_group.all()}
                    old_new_data["old_data"].setdefault(pos_obj.id,{})["module_group"] = list(old_module_group_ids)
                    old_new_data["new_data"].setdefault(pos_obj.id,{})["module_group"] = module_group_ids
                    pos_existing_module_group[pos_obj.id] = old_module_group_ids & set(module_group_ids)
                TtPosModuleGroup.objects.filter(pos_id__in=pos_ids).exclude(module_group_id__in=module_group_ids).delete()

                missing_code = get_missing_code_from_db(self.default_code_prefix, "tt_pos_module_group", 10)
                last_code = (
                    TtPosModuleGroup.objects
                    .filter(code__startswith=self.default_code_prefix)
                    .values_list('code', flat=True)
                    .order_by('-code')
                    .first()
                )
                for pos_id in pos_ids:
                    existing_module_group = pos_existing_module_group.get(pos_id, set())
                    for module_group_id in module_group_ids:
                        if module_group_id not in existing_module_group:
                            if missing_code:
                                new_code = missing_code[0]
                                missing_code.pop(0)
                            else:
                                last_code = get_auto_generate_code(last_code,self.default_code_prefix,10)
                                new_code = last_code
                            pos_module_group_idata.append(TtPosModuleGroup(code=new_code, pos_id=pos_id, module_group_id=module_group_id))
                if pos_module_group_idata:
                    TtPosModuleGroup.objects.bulk_create(pos_module_group_idata)
                # get all latest module in every edited pos
                module_data = (
                    TtPosModuleGroupModule.objects
                    .filter(pos_module_group__pos_id__in=pos_ids)
                    .values_list("pos_module_group__pos_id", "module_id")
                )
                # form the data for delete student_set
                module_to_keep = defaultdict(set)
                for p_id, m_id in module_data:
                    module_to_keep[p_id].add(m_id)

                # this relation table pos_module_group_module is allow to null, so when having value null, means this pathway need to remove
                remove_pathway_ids = list(
                    TtPathwayPosModuleGroupModule.objects.filter(pos_module_group_module__isnull=True).values_list("pathway_id", flat=True)
                )
                TtPathway.objects.filter(id__in=remove_pathway_ids).delete()

                q_delete = Q()
                for pos_id in pos_ids:
                    # if can find in module_to_keep, then will exclude it, if not found in module_to_keep, means can delete all
                    if pos_id in module_to_keep:
                        q_delete |= Q(student_set__pos_id=pos_id) & ~Q(module_id__in=module_to_keep[pos_id])
                    else:
                        q_delete |= Q(student_set__pos_id=pos_id)
                """
                The sample query for this q_delete is somethigns like this:
                DELETE FROM tt_student_set_module
                WHERE
                    (
                        (student_set_id IN (SELECT id FROM tt_student_set WHERE pos_id = 1)
                         AND module_id NOT IN (1, 2, 3))
                    )
                    OR
                    (
                        (student_set_id IN (SELECT id FROM tt_student_set WHERE pos_id = 2)
                         AND module_id NOT IN (5, 6))
                    )
                    OR
                    (
                        student_set_id IN (SELECT id FROM tt_student_set WHERE pos_id = 3)
                    );
                """
                TtStudentSetModule.objects.filter(q_delete).delete()

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

            redis_data = {
                "update": {}
            }

            redis_student_set_table = "student_set"
            # if got update name, need update all student_set name prefix same as pos
            # only update redis student_set table name column
            if "name" in request.data:
                for val in edit_pos_copy:
                    # update per template
                    old_prefix = val.name + self.auto_generate_name_seperator
                    new_prefix = request.data['name'] + self.auto_generate_name_seperator
                    (
                        TtStudentSet.objects
                        .filter(pos_id=val.id, name__startswith=old_prefix)
                        .update(
                            name=Func(# replace old prefix become new prefix but not changing the number for auto generate name
                                Value(new_prefix),
                                Func(F('name'), Value(len(old_prefix) + 1), function='SUBSTR'),
                                function='CONCAT'
                            ),
                            updated_by=request.user.id,
                            updated_at=timezone.now(),
                        )
                    )
                affected_student_set = TtStudentSet.objects.filter(updated_at__gte=now)
                for val in affected_student_set:
                    redis_data["update"].setdefault(redis_student_set_table, []).append({
                        "id": val.id,
                        "name": val.name
                    })

            redis_pos_table = "pos"
            redis_pos_data = {}

            redis_pos_column = [
                "name","desc","department_id","planned_size","max_credit"
            ]

            # if only 1 pos, can edit code
            if edit_pos.count() == 1:
                redis_pos_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 pos reset the redis_pos_data and append to redis_data['update']
                    redis_pos_data = {"id": edit_id}
                    for key, val in request.data.items():
                        if key in redis_pos_column:
                            redis_pos_data[key] = val

                    if "module_group" in request.data:
                        redis_pos_data['module_group_id'] = request.data.get("module_group") or []
                    if len(redis_pos_data)>1:# > 1 means not only id
                        redis_data["update"].setdefault(redis_pos_table, []).append(redis_pos_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_pos = []
                method = "pos_update"
                kafka_topic = self.kafka_config['MICROSERVICES_TT_TOPIC']
                if kafka_topic:
                    new_data = old_new_data.get("new_data")
                    relation_fields = [
                        "module_group",
                        "tag"
                    ]
                    relation_data = generate_kafka_data_update(new_data, relation_fields)
                    update_fields.pop("updated_by", None)
                    for pos in edit_pos:
                        data = {"id": pos.id, **update_fields, **relation_data}
                        kafka_pos.append(data)
                    kafka_request_data = {
                        "session_id": request.user.name,
                        "pos": kafka_pos,
                    }
                    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_pos.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)