from collections import defaultdict

from rest_framework import serializers, status
from api.models import TtLocation, TtLocationSuitability, TtSetting, TtWeek, TtLocationResourceMap, TtAcademicTerm
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 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 LocationCreate(AdminApiBase):
    def validate_request(self,request,settings):
        slot_per_week = settings["slot_per_week"]
        rules = {
            "code": "nullable|not_exists:api.TtLocation,code",
            "name": "required",
            "desc": "nullable",
            "capacity": "nullable",
            "area": "nullable",
            "is_online": "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.TtLocation,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="location_create" # this is for audit log only

            shared_with_all_department=False
            slot_default_pattern = get_slot_default_pattern(slot_per_week)
            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 location table
            ignore_field = ['constraint_profile','suitability','shared_with_department','avoid_concurrency','tag','timestamp','signature']
            valid_fields = {f.column for f in TtLocation._meta.fields}

            for key, val in request.data.items():
                if key not in ignore_field:
                    match key:
                        case  "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']

            # 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(TtLocation, "code", "MV", 10)
            idata['status'] = TtLocation.STATUS_TO_CODE['active']
            idata['created_by'] = request.user.id

            location = TtLocation.objects.create(**idata)

            old_new_data['new_data']=get_add_new_data(location)

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

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

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

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

            new_ids = request.data.get("tag", [])
            old_new_data = self.update_m2m_field(location,'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({
            #         "location_id": location.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 = [TtLocationResourceMap(**data) for data in resource_map_data]
            #     TtLocationResourceMap.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("location", location.id, weeks_data, slot_default_pattern['resource_map'])

            redis_location_table = "location"
            # from here make it same name as db first, got different, only do extra handle
            redis_location_column = [
                "id","code","name","department_id","zone_id","capacity","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_online",
                "shared_with_department","resource_map","allocated_activity_ids"
            ]
            redis_data = {
                "insert": {},
                "update": {}
            }
            redis_location_data = {}
            for column in redis_location_column:
                match column:
                    case "shared_with_department" | "avoid_concurrency" | "constraint_profile"|"allocated_activity_ids":
                        redis_location_data[column] = request.data.get(column) or []
                    case "resource_map":
                        redis_location_data[column] = get_resource_map_redis_data("location", [location.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(location, column)
                        redis_location_data[column] = 0 if value in (None,"") else value
                    case _:
                        redis_location_data[column] = getattr(location, column)
            redis_data["insert"].setdefault(redis_location_table, []).append(redis_location_data)

            redis_suitability_table = "suitability"
            if request.data.get("suitability"):
                suitability_relation = TtLocationSuitability.objects.filter(suitability_id__in=request.data.get("suitability")).values("suitability_id", "location_id").order_by("location_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["location_id"])
                    for suitability_id, suitability_location_ids in suitability_data.items():
                        # no need append because db already is latest record
                        redis_suitability_data = {
                            "id": suitability_id,
                            "location_id": suitability_location_ids
                        }
                        if redis_suitability_data:
                            redis_data["update"].setdefault(redis_suitability_table, []).append(redis_suitability_data)

            if request.data.get("avoid_concurrency"):
                location_avoid_concurrency = TtLocation.objects.filter(id__in=request.data.get("avoid_concurrency")).prefetch_related("avoid_concurrency")
                if location_avoid_concurrency:
                    for val in location_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_location_update_data = {
                            "id": val.id,
                            "avoid_concurrency": current_concurrency_ids
                        }
                        redis_data["update"].setdefault(redis_location_table, []).append(redis_location_update_data)
            bulk_sync_to_redis(redis_client, redis_data)

            # kafka push
            method = "location_create"
            kafka_topic = self.kafka_config['MICROSERVICES_TT_TOPIC']
            new_data = old_new_data.get("new_data")
            kafka_location = []
            if new_data and kafka_topic:
                relation_fields = [
                    "suitability",
                    "avoid_concurrency",
                    "shared_with_department",
                    "constraint_profile",
                    "tag"
                ]
                kafka_location = generate_kafka_data(new_data, relation_fields)
                kafka_request_data = {
                    "session_id": request.user.name,
                    "location": kafka_location,
                }
                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":location.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)