from rest_framework import serializers, status
from django.utils import timezone
from api.views.admin.base import AdminApiBase
from api.utils import decrypt_aes_128_cbc, encrypt_aes_128_cbc, log_critical_error, get_exception_detail
from rest_framework.exceptions import ValidationError
from api.translation import __

class GetTimestamp(AdminApiBase):
    authentication_classes = []
    def post(self, request):
        try:
            self.enable_log=False
            """
            FE will used this api to check timestamp correct or not, FE date.now() is getting user own device datetime, might have different with exact datetime
            """
            now = timezone.now()
            timestamp = int(now.timestamp())
            response = {
                "timestamp": timestamp
            }

            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)