from collections import defaultdict

from rest_framework import serializers, status

from api.models import TtSuitability, TtStaffSuitability, TtSetting, TtWeek, TtAcademicTerm, TtStaffResourceMap
from api.models.audit_trail import AuditTrail
from api.models.audit_trail_details import AuditTrailDetails
from api.models.tt_staff import TtStaff
from api.views.admin.base import AdminApiBase
from api.validator import BaseValidator
from api.utils import create_resource_map_single, decrypt_aes_128_cbc, encrypt_aes_128_cbc, generate_kafka_data, get_resource_map_redis_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 StaffCreate(AdminApiBase):
    def validate_request(self,request,settings):
        slot_per_week = settings["slot_per_week"]
        rules = {
            "code": "nullable|not_exists:api.TtStaff,code",
            "name": "required",
            "desc": "nullable",
            "email": "nullable",
            "is_part_time": "nullable",
            "shared_with_all_department": "nullable",
            "department_id": "nullable|exists:api.TtDepartment,id",
            "maximum_period": "required|integer",
            "contract_period": "required|integer",
            "constraint_profile": "nullable|array|exists:api.TtConstraintProfile,id",
            "suitability": "nullable|array|exists:api.TtSuitability,id",
            "zone_id": "nullable|exists:api.TtZone,id",
            "avoid_concurrency": "nullable|array|exists:api.TtStaff,id",
            "shared_with_department": "nullable|array|exists:api.TtDepartment,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

    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"])
            self.validate_request(request,settings)

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

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

            slot_default_pattern = get_slot_default_pattern(slot_per_week)
            is_part_time=False
            shared_with_all_department=False
            if request.data.get("is_part_time"):
                is_part_time = request.data.get('is_part_time')

            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 staff table
            ignore_field = ['constraint_profile','suitability','shared_with_department','avoid_concurrency','tag','timestamp','signature']
            valid_fields = {f.column for f in TtStaff._meta.fields}

            for key, val in request.data.items():
                if key not in ignore_field:
                    match key:
                        case "is_part_time" | "shared_with_all_department" | "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 FE no pass in this 2 value, the loop wont add default pattern, so having this code to handle default pattern
            if not availability_id and not availability_pattern:
                idata['availability_pattern'] = slot_default_pattern['availability']
            if not usage_preference_id and not usage_preference_pattern:
                idata['usage_preference_pattern'] = slot_default_pattern['usage_preference']
            if not start_preference_id and not start_preference_pattern:
                idata['start_preference_pattern'] = slot_default_pattern['start_preference']
            idata['status'] = TtStaff.STATUS_TO_CODE['active']
            idata['created_by'] = request.user.id

            code = request.data.get("code")
            if not code:
                idata["code"] = get_next_available_code(TtStaff,"code","MV",10)

            staff = TtStaff.objects.create(**idata)

            old_new_data['new_data']=get_add_new_data(staff)

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

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

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

            new_ids = request.data.get("avoid_concurrency", [])
            old_new_data = self.update_m2m_field(staff,'avoid_concurrency',None,new_ids,old_new_data,True)

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

            # handle resource map [old, 22/1/2026]
            # weeks_data = TtWeek.objects.filter(ttacademicterm__status=TtAcademicTerm.STATUS_TO_CODE['active']).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({
            #         "staff_id": staff.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 = [TtStaffResourceMap(**data) for data in resource_map_data]
            #     TtStaffResourceMap.objects.bulk_create(resource_map_object)

            #new resource map handler
            weeks_data = list(TtWeek.objects.values_list("id", flat=True))
            if weeks_data:
                create_resource_map_single("staff", staff.id, weeks_data, slot_default_pattern['resource_map'])

            redis_staff_table = "staff"
            # from here make it same name as db first, got different, only do extra handle
            redis_staff_column = [
                "id","code", "name", "department_id", "zone_id",
                "maximum_period", "contract_period", "constraint_profile","avoid_concurrency",
                "availability_id", "availability_pattern","start_preference_id",
                "start_preference_pattern", "usage_preference_id", "usage_preference_pattern",
                "shared_with_all_department",
                "shared_with_department","is_part_time","resource_map","allocated_activity_ids"
            ]
            redis_data = {
                "insert": {},
                "update": {}
            }
            redis_staff_data = {}
            for column in redis_staff_column:
                match column:
                    case "shared_with_department" | "avoid_concurrency" | "constraint_profile"|"allocated_activity_ids":
                        redis_staff_data[column] = request.data.get(column) or []
                    case "resource_map":
                        redis_staff_data[column] = get_resource_map_redis_data("staff", [staff.id])[0]["resource_map"]
                    case "usage_preference_id" | "availability_id" | "start_preference_id":
                        # jose request if no id, dun store null, want to store "0"
                        value = getattr(staff, column)
                        redis_staff_data[column] = 0 if value in (None,"") else value
                    case _:
                        redis_staff_data[column] = getattr(staff, column)
            redis_data["insert"].setdefault(redis_staff_table, []).append(redis_staff_data)

            redis_suitability_table = "suitability"
            # # get suitability from redis/db and update staff value
            # if request.data.get("suitability"):
            #     redis_suitability_key = [f"Suitability:{sid}" for sid in request.data.get("suitability")]
            #     redis_suitability = redis_client.json().mget(redis_suitability_key,"$")
            #     for val in redis_suitability:
            #         # always reset the suitability data, so will update to correct redis column
            #         suitability_staff_ids = []
            #         redis_suitability_data = {}
            #         # using mget the data always like {[{"id":1}]}
            #         data = val[0]
            #         suitability_staff_ids = data.get("staff_id", [])
            #
            #         # make sure is array, if not make a custom array
            #         if not isinstance(suitability_staff_ids, list):
            #             suitability_staff_ids = [suitability_staff_ids]
            #
            #         # by right wont have this problem, cause is new user, just put same as update staff first
            #         if staff.id not in suitability_staff_ids:
            #             suitability_staff_ids.append(staff.id)
            #
            #         redis_suitability_data = {
            #             "id": data.get("id"),
            #             "staff_id": suitability_staff_ids,
            #         }
            #         redis_data["update"].setdefault(redis_suitability_table, []).append(redis_suitability_data)
            
            # different from update, because update can set the suitability to None, but insert if no then no need update redis
            if request.data.get("suitability"):
                suitability_relation = TtStaffSuitability.objects.filter(suitability_id__in=request.data.get("suitability")).values("suitability_id", "staff_id").order_by("staff_id")
                suitability_data = defaultdict(list)
                if suitability_relation:
                    # form the record for suitability to insert redis
                    for row in suitability_relation:
                        suitability_data[row["suitability_id"]].append(row["staff_id"])
                    for suitability_id, suitability_staff_ids in suitability_data.items():
                        # no need append because db already is latest record
                        redis_suitability_data = {
                            "id": suitability_id,
                            "staff_id": suitability_staff_ids
                        }
                        if redis_suitability_data:
                            redis_data["update"].setdefault(redis_suitability_table, []).append(redis_suitability_data)

            if request.data.get("avoid_concurrency"):
                staff_avoid_concurrency = TtStaff.objects.filter(id__in=request.data.get("avoid_concurrency")).prefetch_related("avoid_concurrency")
                if staff_avoid_concurrency:
                    for val in staff_avoid_concurrency:
                        # relation already create, no need to add again, this is the latest relation ids
                        current_concurrency_ids = [ac.id for ac in val.avoid_concurrency.all()]
                        redis_staff_update_data = {
                            "id": val.id,
                            "avoid_concurrency": current_concurrency_ids
                        }
                        redis_data["update"].setdefault(redis_staff_table, []).append(redis_staff_update_data)
            bulk_sync_to_redis(redis_client, redis_data)

            #kafka push
            method = "staff_create"
            kafka_topic = self.kafka_config['MICROSERVICES_TT_TOPIC']
            new_data = old_new_data.get("new_data")
            kafka_staff = []
            if new_data and kafka_topic:
                relation_fields = [
                    "suitability",
                    "avoid_concurrency",
                    "shared_with_department",
                    "constraint_profile",
                    "tag",
                ]
                kafka_staff = generate_kafka_data(new_data, relation_fields)
                
                # hide resource map
                # if weeks_data:
                #     kafka_staff['resource_map'] = slot_default_pattern.get("resource_map")
                kafka_request_data = {
                    "session_id": request.user.name,
                    "staff": kafka_staff,
                }
                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":staff.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)