from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from rest_framework.decorators import api_view, authentication_classes, permission_classes
from rest_framework.permissions import IsAuthenticated
from payrollservice.service.emppaystructuredetailsservice import EmployeePaystructure_detailsService
from payrollservice.service.emppaystructureservice import EmployeePaystructureService
from utilityservice.data.response.nwisefinlist import NWisefinList
from utilityservice.data.response.nwisefinpage import NWisefinPage
from common_middleware.request_middleware import NWisefinAuthentication
# from utilityservice.service.nwisefinauthenticate import NWisefinAuthentication
from utilityservice.service.nwisefinpermission import NWisefinPermission


@csrf_exempt
@api_view(['GET'])
@authentication_classes([NWisefinAuthentication])
@permission_classes([IsAuthenticated, NWisefinPermission])
def employeepay_summary(request):
    scope=request.scope
    page = request.GET.get('page',1)
    page = int(page)
    vys_page = NWisefinPage(page, 10)
    paystruct_detail = EmployeePaystructure_detailsService(scope)
    resp=paystruct_detail.employeepaystrct_summary(vys_page)
    response = HttpResponse(resp.get(), content_type="application/json")
    return response



# get and delete Employeemonthly_payinfo,Employeemonthlypay_details
@csrf_exempt
@api_view(['DELETE','GET'])
@authentication_classes([NWisefinAuthentication])
@permission_classes([IsAuthenticated, NWisefinPermission])
def employee_paystrct_inactive(request, struct_id):
    scope = request.scope
    action = request.GET.get('action', None)
    if request.method == 'DELETE':
        if action == '0':
            user_id = request.employee_id
            payroll_serv = EmployeePaystructureService(scope).employee_paystrct_inactive(struct_id, user_id)
            response = HttpResponse(payroll_serv.get(), content_type='application/json')
            return response
        elif action == '1':
            user_id = request.employee_id
            payroll_serv = EmployeePaystructure_detailsService(scope).paystrct_detail_inactive(struct_id, user_id)
            response = HttpResponse(payroll_serv.get(), content_type='application/json')
            return response
    elif request.method == 'GET':
        arr = NWisefinList()
        employee_pay = EmployeePaystructureService(scope).employee_pay_get(struct_id)
        employee_details = EmployeePaystructure_detailsService(scope).employee_level_detail(employee_pay.id)
        employee_pay.employeepay_detail = employee_details
        arr.append(employee_pay)
        response = HttpResponse(arr.get(), content_type='application/json')
        return response

@csrf_exempt
@api_view(['GET'])
@authentication_classes([NWisefinAuthentication])
@permission_classes([IsAuthenticated, NWisefinPermission])
def employee_sancturaies_pf_calculation(request):
    scope = request.scope
    if request.method == 'GET':
        employee_id = request.GET.get('employee_id')
        payroll_serv = EmployeePaystructure_detailsService(scope).employee_sancturaies_pf_calculation(employee_id)
        response = HttpResponse(payroll_serv.get(), content_type='application/json')
        return response

@csrf_exempt
@api_view(['GET'])
@authentication_classes([NWisefinAuthentication])
@permission_classes([IsAuthenticated, NWisefinPermission])
def employee_deduction_calculation(request):
    scope = request.scope
    if request.method == 'GET':
        employee_id = request.GET.get('employee_id')
        paycomponent_id = request.GET.get('paycomponent_id')
        payroll_serv = EmployeePaystructure_detailsService(scope).employee_deduction_calculation(employee_id, paycomponent_id)
        response = HttpResponse(payroll_serv.get(), content_type='application/json')
        return response

@csrf_exempt
@api_view(['GET'])
@authentication_classes([NWisefinAuthentication])
@permission_classes([IsAuthenticated, NWisefinPermission])
def company_contribution_calculation(request):
    scope = request.scope
    if request.method == 'GET':
        employee_id = request.GET.get('employee_id')
        paycomponent_id = request.GET.get('paycomponent_id')
        payroll_serv = EmployeePaystructure_detailsService(scope).company_contribution_calculation(paycomponent_id, employee_id)
        response = HttpResponse(payroll_serv.get(),content_type='application/json')
        return response