from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from datetime import date
from customers.models import Customer
from shares.models import ShareHolders
from shares.serializers import ShareReportsSerializer
from .serializers import USSDSerializer
from .process import UssdLib
from .models import Session
from django.http import HttpResponse
from rest_framework.permissions import AllowAny

# Create your views here. 
class UssdBankingMemberSharesView(APIView):
    authentication_classes = []
    permission_classes = []

    def post(self, request, format=None):
        data = {
            'status': 'failed',
            'shares':[]
        }
        '''
        Member shares statement.
        '''
        customer_id = self.request.data.get('id', None)
        customer = Customer.objects.get(pk=customer_id)
        if customer:
            share_holder = ShareHolders.objects.filter(customer=customer)
            if(len(share_holder) > 0):
                shares = ShareReportsSerializer(
                    share_holder[0]
                ).data
                data = {
                    'status': 'success',
                    'shares': shares['shares']
                }

        return Response(data, status=status.HTTP_200_OK)


class UssdMenu(APIView):
    authentication_classes = []  # Disable authentication
    permission_classes = [AllowAny]  # Explicitly allow public access
    serializer_class = USSDSerializer

    def post(self, request):
        response = {}
        serializer = self.serializer_class(data=request.data)
        is_valid = serializer.is_valid(raise_exception=True)

        if is_valid:
            validated_data = request.data
            ussd_response = validated_data.get('text')

            phone = "0"
            if len(validated_data.get("phoneNumber"))> 10:
                for index in range(0, len(validated_data.get("phoneNumber"))):
                    if index > 3:
                        phone +=validated_data.get("phoneNumber")[index]
            else:
                phone = validated_data.get("phoneNumber")
            
            ussd_lib = UssdLib(phone)

            print("00000............................ ussd_response")
            print(ussd_response)

            if ussd_response == '':
                print(" step 1. -->>")
                response = ussd_lib.check_user_session(validated_data, phone)
                # response = {"Hello": "Welcome"}
            else :
                print(" step 2. -->>")
                current_session = Session.objects.filter(phone=phone).first()
                print(current_session)
                if not current_session:
                    response = {"Error !!!": "No session"}
                    return HttpResponse(response)
                else:
                    # print("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  process_menu ")
                    response = ussd_lib.process_menu(validated_data, current_session)
        print("Last step -->>")
        return HttpResponse(response)