from collections import defaultdict

from rest_framework import serializers, status
from api.models import TtLocation, TtLocationSuitability, TtSetting, TtPos, TtPosModuleGroupModule, TtWeek, \
    TtStudentSetResourceMap, TtAcademicTerm, TtModule, TtPathway
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, log_critical_error, get_exception_detail, \
    get_add_new_data, get_ip, bulk_sync_to_redis, get_slot_default_pattern, get_next_available_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 StudentSetCreate(AdminApiBase):
    def validate_request(self,request,settings):
        slot_per_week = settings["slot_per_week"]
        rules = {
            "code": "nullable|not_exists:api.TtStudentSet,code",
            "name": "required",
            "desc": "nullable",
            "department_id": "nullable|exists:api.TtDepartment,id",
            "academic_term_id": "required|exists:api.TtAcademicTerm,id",
            "pos_id": "nullable|exists:api.TtPos,id",
            "module": "nullable|array|exists:api.TtModule,id",
            "planned_size": "required|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 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
        pos_id = request.data.get("pos_id")
        input_module = set(request.data.get("module", []))

        if pos_id:
            selected_pos = TtPos.objects.get(id=pos_id)
            if input_module:
                selected_modules = TtModule.objects.filter(id__in=input_module)
                module_ids = set(
                    TtPosModuleGroupModule.objects
                    .filter(pos_module_group__pos_id=pos_id)
                    .values_list("module_id", flat=True)
                )
                not_exist_modules = input_module - module_ids

                if not_exist_modules:
                    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)}
                    })
        else:
            if input_module:
                raise ValidationError({
                    "error": __('validation.must_have_pos_only_can_select_module'),
                    "errors": {"module": __('validation.must_have_pos_only_can_select_module')}
                })


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

            old_new_data = {
                "old_data": None,
                "new_data": None,
            }

            slot_default_pattern = get_slot_default_pattern(slot_per_week)
            action="student_set_create" # this is for audit log only
            academic_term_id = request.data['academic_term_id']
            academic_term = TtAcademicTerm.objects.get(id=academic_term_id)
            shared_with_all_department=False

            if request.data.get("shared_with_all_department"):
                shared_with_all_department = request.data.get('shared_with_all_department')

            # 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

            idata = {}
            # not insert to student_set table
            ignore_field = ['constraint_profile','tag','timestamp','signature']
            valid_fields = {f.column for f in TtStudentSet._meta.fields}

            for key, val in request.data.items():
                if key not in ignore_field:
                    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
                            idata[key] = locals()[key]
                        case _:
                            if key in valid_fields:#for insert, key can check in default part, cause if got special handle, i can check the way to use key also
                                # default the key will same as db column name
                                idata[key] = val

            if not availability_id and not availability_pattern:
                idata['availability_pattern'] = academic_term.default_availability
            if not usage_preference_id and not usage_preference_pattern:
                idata['usage_preference_pattern'] = academic_term.default_usage
            if not start_preference_id and not start_preference_pattern:
                idata['start_preference_pattern'] = academic_term.default_start
            # if FE no pass in code or is null or empty, will used default code
            code = request.data.get("code")
            if not code:
                idata["code"] = get_next_available_code(TtStudentSet, "code", "MV", 10)
            idata['status'] = TtStudentSet.STATUS_TO_CODE['active']
            idata['created_by'] = request.user.id

            student_set = TtStudentSet.objects.create(**idata)

            old_new_data['new_data']=get_add_new_data(student_set)

            new_ids = request.data.get("module", [])
            old_new_data = self.update_m2m_field(student_set, 'module', None, new_ids, old_new_data)

            new_ids = request.data.get("constraint_profile", [])
            old_new_data = self.update_m2m_field(student_set,'constraint_profile',None,new_ids,old_new_data)

            new_ids = request.data.get("tag", [])
            old_new_data = self.update_m2m_field(student_set,'tag',None,new_ids,old_new_data,False,True)

            # handle resource map
            weeks_data = TtWeek.objects.filter(ttacademicterm__id=academic_term_id).order_by("week").distinct()
            resource_map_data = []
            resource_map_redis_data = {}
            default_resource_map_pattern = "0" * int(slot_per_week)
            for val in weeks_data:
                resource_map_data.append({
                    "student_set_id": student_set.id,
                    "week_id": val.id,
                    "pattern": default_resource_map_pattern
                })
                # redis week is start from 0, 0 means week 1
                redis_week = int(val.week) - 1
                resource_map_redis_data[redis_week] = default_resource_map_pattern

            if resource_map_data:
                resource_map_object = [TtStudentSetResourceMap(**data) for data in resource_map_data]
                TtStudentSetResourceMap.objects.bulk_create(resource_map_object)

            redis_student_set_table = "student_set"
            # from here make it same name as db first, got different, only do extra handle
            redis_student_set_column = [
                "id","code","name","department_id","academic_term_id","constraint_profile","pos_id","module_id","planned_size","availability_id","availability_pattern","start_preference_id",
                "start_preference_pattern","usage_preference_id","usage_preference_pattern","resource_map","allocated_activity_ids"
            ]
            redis_data = {
                "insert": {}
            }
            redis_student_set_data = {}
            for column in redis_student_set_column:
                match column:
                    case "module_id":
                        redis_student_set_data[column] = request.data.get("module") or []
                    case "allocated_activity_ids" | "constraint_profile":
                        redis_student_set_data[column] = request.data.get(column) or []
                    case "resource_map":
                        redis_student_set_data[column] = resource_map_redis_data
                    case "usage_preference_id" | "availability_id" | "start_preference_id":
                        # jose request if no id, dun store null, want to store "0"
                        value = getattr(student_set, column)
                        redis_student_set_data[column] = 0 if value in (None, "") else value
                    case _:
                        redis_student_set_data[column] = getattr(student_set, column)
            redis_data["insert"].setdefault(redis_student_set_table, []).append(redis_student_set_data)

            bulk_sync_to_redis(redis_client, redis_data)

            # kafka push
            method = "student_set_create"
            kafka_topic = self.kafka_config['MICROSERVICES_TT_TOPIC']
            new_data = old_new_data.get("new_data")
            kafka_student_set = []
            if new_data and kafka_topic:
                relation_fields = [
                    "module",
                    "constraint_profile"
                    "tag",
                ]
                kafka_student_set = generate_kafka_data(new_data, relation_fields)
                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)
            )
            remark_param = {
                "name":student_set.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'],
            )
            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)