from collections import defaultdict
from rest_framework import serializers, status

from api.models import TtStaffSuitability, TtSetting

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 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.models.role import Role
from api.models.role_permission import RolePermission
from api.translation import __
from api.services.integration.tracking import track_resource_queryset
from backend.kafka import send_request
from backend.redis_client import redis_client


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

        edit_staff = (
            TtStaff.objects
            .select_related("department","zone","availability","start_preference","usage_preference")
            .prefetch_related(
                "shared_with_department", #1
                "suitability", #1
                "constraint_profile",#1
                "avoid_concurrency",#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",
            "email": "nullable",
            "is_part_time": "nullable",
            "shared_with_all_department": "nullable",
            "department_id": "nullable|exists:api.TtDepartment,id",
            "maximum_period": "nullable|integer",
            "contract_period": "nullable|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 request.data.get("code") and edit_staff.count() == 1:
            staff = edit_staff.first()
            if request.data.get("code") != staff.code:
                rules['code'] = "nullable|not_exists:api.TtStaff,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)

        return edit_staff


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

            action="staff_update" # this is for audit log only
            slot_default_pattern = get_slot_default_pattern(slot_per_week)
            is_part_time = False
            if request.data.get("is_part_time"):
                is_part_time = request.data.get('is_part_time')

            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

            updated = False
            update_fields = {}
            ignore_fields = ['id','constraint_profile','suitability','shared_with_department','avoid_concurrency','tag','timestamp','signature']
            # if more than 1 staff edit, ignore code
            if edit_staff.count() > 1 or not request.data.get("code"):
                ignore_fields.append('code')
            valid_fields = {f.column for f in TtStaff._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 "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
                            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 = slot_default_pattern["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 = slot_default_pattern["start_preference"]
                    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 = slot_default_pattern["usage_preference"]
                    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_staff,request.data.items(),ignore_fields,valid_fields)
            track_resource_queryset(edit_staff)
            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_staff.update(**update_fields)

            # new_ids change to set so it cam used to compare
            if "suitability" in request.data:
                new_ids = request.data.get("suitability", [])
                old_new_data = self.update_m2m_field_bulk(edit_staff,'suitability',new_ids,old_new_data)

            if "shared_with_department" in request.data:
                new_ids = request.data.get("shared_with_department", [])
                old_new_data = self.update_m2m_field_bulk(edit_staff,'shared_with_department',new_ids,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_staff,'constraint_profile',new_ids,old_new_data)

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

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

            redis_data = {
                "update": {}
            }

            redis_staff_table = "staff"
            redis_staff_data = {}
            redis_staff_column = [
                "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","is_part_time",
                "shared_with_department"
            ]

            # if only 1 staff, can edit code
            if edit_staff.count() == 1:
                redis_staff_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 staff reset the redis_staff_data and append to redis_data['update']
                    redis_staff_data = {"id": edit_id}
                    for key, val in request.data.items():
                        if key in redis_staff_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_staff_data[key] = 0 if val in (None,"") else val
                            elif key in ["shared_with_department","avoid_concurrency","constraint_profile"]:
                                redis_staff_data[key] = val or []
                            else:
                                redis_staff_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_staff_data["availability_pattern"] = availability_pattern
                    if "start_preference_id" in request.data or "start_preference_pattern" in request.data:
                        redis_staff_data["start_preference_pattern"] = start_preference_pattern
                    if "usage_preference_id" in request.data or "usage_preference_pattern" in request.data:
                        redis_staff_data["usage_preference_pattern"] = usage_preference_pattern
                    if len(redis_staff_data)>1:# > 1 means not only id
                        redis_data["update"].setdefault(redis_staff_table, []).append(redis_staff_data)

            redis_suitability_table = "suitability"
            if "suitability" in request.data:
                suitability_ids = search_old_new_data(old_new_data,"suitability")

                if suitability_ids:
                    suitability_relation = TtStaffSuitability.objects.filter(suitability_id__in=suitability_ids).values("suitability_id", "staff_id").order_by("staff_id")
                    suitability_data = defaultdict(list)
                    # 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)

            # update need using in request.data, cause can update to null
            if "avoid_concurrency" in request.data:
                avoid_concurrency_ids = search_old_new_data(old_new_data,"avoid_concurrency")

                staff_avoid_concurrency = TtStaff.objects.filter(id__in=avoid_concurrency_ids).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)

            if old_new_data['old_data'] or old_new_data['new_data']:
                #kafka update/ split by id
                kafka_staff = []
                method = "staff_update"
                kafka_topic = self.kafka_config['MICROSERVICES_TT_TOPIC']
                if kafka_topic:
                    new_data = old_new_data.get("new_data")
                    relation_fields = ["suitability", "shared_with_department", "constraint_profile", "avoid_concurrency" "tag"]
                    relation_data = generate_kafka_data_update(new_data, relation_fields)
                    update_fields.pop("updated_by", None)
                    for staff in edit_staff:
                        data = {"id": staff.id, **update_fields, **relation_data}
                        kafka_staff.append(data)
                    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)
                )

                staff_name = ",".join(edit_staff.values_list("name", flat=True))
                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'],
                    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)
