import copy
from collections import defaultdict

from rest_framework import serializers, status

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


class PosUpdateModule(AdminApiBase):
    def validate_request(self,request):
        rules = {
            "id": "required|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( # no need use prefetch_related, cause it cant get the relation table id, will have another query to get it
            #     "module_group",#1
            # )
            .filter(id=request.data.get("id"))
            .first()
        )

        rules = {
            "modules": "required|array",
        }

        # 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)

        try:
            # keep track the used module id, so not allow repeat
            used_module_ids = set()
            input_modules = request.data.get("modules")
            # prefetch_related("module") not used for validation, just pre get it so no need add a query to get again when doing action
            pos_module_groups = TtPosModuleGroup.objects.filter(pos_id=edit_pos.id).prefetch_related("module")
            pos_module_group_ids = list(pos_module_groups.values_list("id", flat=True))
            modules = TtModule.objects.filter(academic_term=edit_pos.academic_term)
            check_module = list(modules.values_list("id", flat=True))
            # foreach input_modules
            for module_data in input_modules:
                # check pos_module_group_id is a valid id
                if module_data.get("pos_module_group_id") not in pos_module_group_ids:
                    raise ValidationError()

                # check input module is a valid module to choose
                module_ids = module_data.get("module_id", [])
                if not all(module_id in check_module for module_id in module_ids):
                    raise ValidationError()

                # check got duplicate module id in same group or not
                # set(modules_ids) will remove duplicate id, so if not same length, means got duplicate
                if len(module_ids) != len(set(module_ids)):
                    raise ValidationError()

                # Check for module_ids already used in previous groups
                # overlap = used_module_ids.intersection(module_ids)
                # if overlap:
                #     raise ValidationError()

        except Exception as e:
            # any error get from here throw invalid format error
            raise ValidationError({
                "error": __('validation.invalid_format'),
                "errors": {"modules": __("validation.invalid_format")}
            })

        return {
            "edit_pos": edit_pos,
            "modules": modules,
            "pos_module_groups": pos_module_groups,
        }

    def post(self, request):
        # Validate input
        try:
            validate_data = self.validate_request(request)
            edit_pos = validate_data['edit_pos']
            module_dict = {m.id: m.name for m in validate_data['modules']}

            action="pos_update_module" # this is for audit log only
            old_new_data = {
                "old_data": {},
                "new_data": {},
            }
            input_modules = request.data.get("modules")

            # make it key by id
            pos_module_groups = {g.id: g for g in validate_data['pos_module_groups']}

            for module_data in input_modules:
                pos_module_group = pos_module_groups.get(module_data.get("pos_module_group_id"))

                old_ids = list(pos_module_group.module.all().values_list("id", flat=True))
                new_ids = module_data.get("module_id", [])

                temp_old_new_data = copy.deepcopy(old_new_data)
                temp_old_new_data = self.update_m2m_field(pos_module_group, 'module', old_ids, new_ids, temp_old_new_data)

                # 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()

                if temp_old_new_data['old_data'] or temp_old_new_data['new_data']:
                    for key in ["old_data", "new_data"]:
                        old_new_data[key].setdefault(pos_module_group.id, {"module": []})
                        # if from no data become got data, old_data will be empty
                        if temp_old_new_data[key].get('module'):
                            for module_id in temp_old_new_data[key]['module']:
                                # not sure want to store name or store id first, so now store id
                                # old_new_data[key][pos_module_group.id]["module"].append(module_dict[module_id])
                                old_new_data[key][pos_module_group.id]["module"].append(module_id)

            if old_new_data['old_data'] or old_new_data['new_data']:
                # extra handle if got updated, delete relation from student_set_module
                module_ids =(
                    TtPosModuleGroupModule.objects
                    .filter(pos_module_group__pos_id=edit_pos.id)
                    .values_list("module_id", flat=True)
                )
                # delete student set module relation where pos id = current edit pos_id and module_id not in latest modules for the pos
                TtStudentSetModule.objects.filter(student_set__pos_id=edit_pos.id).exclude(module_id__in=module_ids).delete()

                # 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 = edit_pos.name
                remark_param = {
                    "name":name
                }

                pos_mg_ids = old_new_data['new_data'].keys()

                pos_mg_map = {
                    obj.id: obj
                    for obj in TtPosModuleGroup.objects.filter(id__in=pos_mg_ids)
                }

                # kafka push
                kafka_modules = [
                    {
                        "pos_module_group_id": pos_mg_id,
                        "pos_id": pos_mg_map[pos_mg_id].pos_id if pos_mg_id in pos_mg_map else None,
                        "module_group": pos_mg_map[pos_mg_id].module_group_id if pos_mg_id in pos_mg_map else None,
                        "module_id": data.get('module', [])
                    }
                    for pos_mg_id, data in old_new_data['new_data'].items()
                ]
                method = "pos_update_module"
                kafka_topic = self.kafka_config['MICROSERVICES_TT_TOPIC']
                if kafka_topic:
                    kafka_request_data = {
                        "session_id": request.user.name,
                        "modules": kafka_modules 
                    }
                    send_request(kafka_topic, kafka_request_data, None, method)
                # 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)