# By: Riasat Ullah
# This file contains code for handling Freshservice integration related views.
import logging

from constants import api_paths, label_names as lnm, var_names
from django.http import JsonResponse
from django.views.decorators.http import require_http_methods
from integrations import freshservice
from taskcallweb import settings
from translators import label_translator as lt
from utils import helpers
from validations import request_validator
import json


@require_http_methods(['POST'])
def get_freshservice_account_info(request):
    '''
    Get the domain and api key of the Freshservice account associated to an integration.
    This will be displayed on the Freshservice integration modal.
    :param request: Http request
    :return: JSON response
    '''
    if request.method == 'POST':
        lang = request_validator.get_user_language(request)
        if request_validator.user_in_session(request):
            body = json.loads(request.body.decode())
            if settings.TEST_MODE:
                return JsonResponse([{var_names.vendor_endpoint_name: 'taskcallapp.freshservice.com',
                                      var_names.secret_token: 'api_key_xx'}], safe=False)
            else:
                status, output = helpers.post_api_request(api_paths.integrations_freshservice_account, body,
                                                          request, lang=lang)
                return JsonResponse(output, status=status, safe=False)
        else:
            return JsonResponse(lt.get_label(lnm.err_unauthorized_access, lang), status=401, safe=False)


@require_http_methods(['POST'])
def update_freshservice_account_info(request):
    '''
    Updates the Freshservice domain and api key stored for a Freshservice integration.
    :param request: Http request
    :return: Http response
    '''
    if request.method == 'POST':
        lang = request_validator.get_user_language(request)
        if request_validator.user_in_session(request):
            body = json.loads(request.body.decode())
            if settings.TEST_MODE:
                return JsonResponse('Success', safe=False)
            else:
                status, output = helpers.post_api_request(api_paths.integrations_freshservice_account_update, body,
                                                          request, lang=lang)
                return JsonResponse(output, status=status, safe=False)
        else:
            return JsonResponse(lt.get_label(lnm.err_unauthorized_access, lang), status=401, safe=False)


@require_http_methods(['POST'])
def get_freshservice_meta_data(request):
    '''
    Get the ticket types allowed on a Freshservice account.
    This will be displayed on the Freshservice integration modal.
    :param request: Http request
    :return: JSON response
    '''
    if request.method == 'POST':
        lang = request_validator.get_user_language(request)
        if request_validator.user_in_session(request):
            body = json.loads(request.body.decode())
            if settings.TEST_MODE:
                return JsonResponse({
                    var_names.ticket_type: [
                        ['Case', 'Case'], ['Incident', 'Incident'], ['Issue', 'Issue'],
                        ['Major Incident', 'Major Incident'], ['Query', 'Query'], ['Request', 'Request'],
                        ['Service Request', 'Service Request']
                    ],
                    var_names.workspace: [['IT', 2]]
                }, safe=False)
            else:
                status, output = freshservice.get_freshservice_meta_data(body[var_names.domain], body[var_names.key])
                if status != 200:
                    logging.error(output)
                return JsonResponse(output, status=status, safe=False)
        else:
            return JsonResponse(lt.get_label(lnm.err_unauthorized_access, lang), status=401, safe=False)
