from django.shortcuts import render,redirect
from django.contrib.auth import authenticate,login,logout
from django.contrib import messages
from django.contrib.auth.forms import  UserCreationForm
from .decorators import *
from django.contrib.auth.decorators import login_required
from django.views.decorators.csrf import csrf_exempt

from django.http import HttpResponseRedirect
from .forms import *
from .models import *


@login_required
def pharmacistHome(request):
    patients_total=Patients.objects.all().count()
    exipred=Stock.objects.annotate(
    expired=ExpressionWrapper(Q(valid_to__lt=Now()), output_field=BooleanField())
    ).filter(expired=True).count()
 
    out_of_stock=Stock.objects.filter(quantity__lte=0).count()
    total_stock=Stock.objects.all().count()

    context={
        "patients_total":patients_total,
        "expired_total":exipred,
        "out_of_stock":out_of_stock,
        "total_drugs":total_stock,  
    }
    return render(request,'pharmacist_templates/pharmacist_home.html',context)

@login_required
def userProfile(request):
    staff=Pharmacist.objects.all()
    form=CustomerForm()
    if request.method == "POST":
        first_name = request.POST.get('first_name')
        last_name = request.POST.get('last_name')
        password = request.POST.get('password')
        address = request.POST.get('address')
        customuser = CustomUser.objects.get(id=request.user.id)
        customuser.first_name = first_name
        customuser.last_name = last_name
        customuser.save()
        staff = Pharmacist.objects.get(admin=customuser.id)
        form=CustomerForm(request.POST,request.FILES,instance=staff)

        staff.address = address
        if form.is_valid():
            form.save()
        staff.save()       
        messages.success(request, "Profile Updated Successfully")
        return redirect('pharmacist_profile')

    context={
        "staff":staff,
        "form":form
    }
    return render(request,'pharmacist_templates/staff_profile.html',context)

def managePatientsPharmacist(request):
    patient=Patients.objects.all()
    context={
        "patients":patient
    }
    return render(request,'pharmacist_templates/manage_patients.html',context)


def managePrescription(request):
    precrip=Dispense.objects.all()
    context={
        "prescrips":precrip,
    }
    return render(request,'pharmacist_templates/patient_prescrip.html',context)

    
def manageStock(request):
    stocks = Stock.objects.all()
    stocks = Stock.objects.all().order_by("-id")
    ex=Stock.objects.annotate(
    expired=ExpressionWrapper(Q(valid_to__lt=Now()), output_field=BooleanField())
    ).filter(expired=True)
    eo=Stock.objects.annotate(
    expired=ExpressionWrapper(Q(valid_to__lt=Now()), output_field=BooleanField())
    ).filter(expired=False)
    context = {
        "stocks": stocks,
                "expired":ex,

    }
    return render(request,'pharmacist_templates/manage_stock.html',context)


def manageDispense(request,pk):
    queryset=Patients.objects.get(id=pk)
    prescrips=queryset.prescription_set.all()
    
    print(prescrips)
    form=DispenseForm(request.POST or None,initial={'patient_id':queryset} )
    drugs=Stock.objects.all()
    ex=Stock.objects.annotate(
    expired=ExpressionWrapper(Q(valid_to__lt=Now()), output_field=BooleanField())
    ).filter(expired=True)
    eo=Stock.objects.annotate(
    expired=ExpressionWrapper(Q(valid_to__lt=Now()), output_field=BooleanField())
    ).filter(expired=False)

    try:  
        
        if request.method == 'POST':
            if form.is_valid(): 
                username = form.cleaned_data['taken']
                qu=form.cleaned_data['dispense_quantity']
                ka=form.cleaned_data['drug_id']
                stock= eo=Stock.objects.annotate(
                expired=ExpressionWrapper(Q(valid_to__lt=Now()), output_field=BooleanField())
                ).filter(expired=False).get(id=username)
                form=DispenseForm(request.POST or None, instance=stock)
                instance=form.save()
                # print(instance)
                instance.quantity-=qu
                instance.save()
                form=DispenseForm(request.POST or None ,initial={'patient_id':queryset})
                form.save()
                messages.success(request, "Drug Has been Successfully Dispensed")
                return redirect('manage_patient_pharmacist')
            else:
                messages.error(request, "Validty Error")
                return redirect('manage_patient_pharmacist')

        context={
            "patients":queryset,
            "form":form,
            "drugs":drugs,
            "prescrips":prescrips,
            "expired":ex,
            "expa":eo,
            }
        if request.method == 'POST':
            context={
                "drugs":drugs,
                form:form,
                "prescrips":prescrips,
                "patients":queryset,
                "expired":ex,
                "expa":eo,

            }
    except:
        messages.error(request, "Dispensing Not Allowed! The Drug is Expired ,please contanct the admin for re-stock ")
        return redirect('manage_patient_pharmacist')
    context={
            "patients":queryset,
            "form":form,
            "drugs":drugs,
            "prescrips":prescrips,
            "expired":ex,
            "expa":eo,
            }
    return render(request,'pharmacist_templates/manage_dispense.html',context)

def patient_feedback_message(request):
    feedbacks = PatientFeedback.objects.all()
    context = {
        "feedbacks": feedbacks
    }
    return render(request, 'pharmacist_templates/patient_feedback.html', context)

@csrf_exempt
def patient_feedback_message_reply(request):
    feedback_id = request.POST.get('id')
    feedback_reply = request.POST.get('reply')
    try:
        feedback =  PatientFeedback.objects.get(id=feedback_id)
        feedback.feedback_reply = feedback_reply
        feedback.save()
        return HttpResponse("True")
    except:
        return HttpResponse("False")

def deletefeedback(request,pk):
    try:
        fed=PatientFeedback.objects.get(id=pk)
        if request.method == 'POST':
            fed.delete()
            messages.success(request, "Feedback deleted successfully")
            return redirect('patient_feedback_message')
    except:
        messages.error(request, "Feedback Error, Please Check again")
        return redirect('patient_feedback_message')
    return render(request,'pharmacist_templates/sure_delete.html')


def drugDetails(request,pk):
    stocks=Stock.objects.get(id=pk)
    context={
        "stocks":stocks,
    }
    return render(request,'pharmacist_templates/view_drug.html',context)

def deleteDispense4(request,pk):
    try:
        fed=Dispense.objects.get(id=pk)
        if request.method == 'POST':
            fed.delete()
            messages.success(request, "Dispense  deleted successfully")
            return redirect('pharmacist_prescription')
    except:
        messages.error(request, "Delete Error, Please Check again")
        return redirect('pharmacist_prescription')
    return render(request,'pharmacist_templates/sure_delete.html')

def createPatient(request):
    form=PatientForm(request.POST, request.FILES)
    try:
        if request.method == "POST":
            if form.is_valid():
                first_name = form.cleaned_data['first_name']
                last_name = form.cleaned_data['last_name']
                username = form.cleaned_data['username']
                email = form.cleaned_data['email']
                password = form.cleaned_data['password']
                address = form.cleaned_data['address']
                phone_number = form.cleaned_data['phone_number']
                dob = form.cleaned_data['dob']
                gender = form.cleaned_data['gender']
        
            user = CustomUser.objects.create_user(username=username, email=email,password=password, first_name=first_name, last_name=last_name,user_type=5)
            user.patients.address = address
            user.patients.phone_number = phone_number
            user.patients.dob=dob
            user.patients.gender=gender
            user.save()
            messages.success(request, "Patient Added Successfully!")
            return redirect('pharmacist_patient_form')            
    except:        
        messages.error(request,'Patient Not Saved')
        return redirect('pharmacist_patient_form')
    context={
        "form":form
    }       
    return render(request,'pharmacist_templates/add_patient.html',context)

@login_required
def allPatients(request):
    patients=Patients.objects.all()
    context={
        "patients":patients,

    }
    return render(request,'pharmacist_templates/manage_patient.html',context)
    

@login_required
def editPatient(request,patient_id):
    request.session['patient_id'] = patient_id
    patient = Patients.objects.get(admin=patient_id)
    form = EditPatientForm()

    form.fields['email'].initial = patient.admin.email
    form.fields['username'].initial = patient.admin.username
    form.fields['first_name'].initial = patient.admin.first_name
    form.fields['last_name'].initial = patient.admin.last_name
    form.fields['address'].initial = patient.address
    form.fields['gender'].initial = patient.gender
    form.fields['phone_number'].initial = patient.phone_number
    form.fields['dob'].initial = patient.dob
    try:
        if request.method == "POST":
            if patient_id == None:
                return redirect('all_pharmacist_patients')
            form = EditPatientForm( request.POST)
            if form.is_valid():                
                email = form.cleaned_data['email']
                username = form.cleaned_data['username']
                first_name = form.cleaned_data['first_name']
                last_name = form.cleaned_data['last_name']
                address = form.cleaned_data['address']
                gender = form.cleaned_data['gender']
                dob=form.cleaned_data['dob']
                phone_number = form.cleaned_data['phone_number']
                try:
                    user = CustomUser.objects.get(id=patient_id)
                    user.first_name = first_name
                    user.last_name = last_name
                    user.email = email
                    user.username = username
                    user.save()

                    patients_edit = Patients.objects.get(admin=patient_id)
                    patients_edit.address = address
                    patients_edit.gender = gender
                    patients_edit.dob=dob
                    patients_edit.phone_number=phone_number     
                    patients_edit.save()
                    messages.success(request, "Patient Updated Successfully!")
                    return redirect('all_pharmacist_patients')
                except:
                    messages.error(request, "Failed to Update Patient.")
                    return redirect('all_pharmacist_patients')
    except:
         messages.error(request, "Invalid Error!")
         return redirect('all_pharmacist_patients')

    context = {
        "id": patient_id,
        "form": form
    }
    return render(request, "pharmacist_templates/edit_patient.html", context)

@login_required
def patient_personalRecords(request,pk):
    patient=Patients.objects.get(id=pk)
    prescrip=patient.prescription_set.all()
    context={
        "patient":patient,
        "prescription":prescrip

    }
    return render(request,'pharmacist_templates/patient_personalRecords.html',context)

@login_required
def confirmDelete(request,pk):
    try:
        patient=Patients.objects.get(id=pk)
        if request.method == 'POST':
            patient.delete()
            messages.success(request, "Staff deleted")
            return redirect('all_pharmacist_patients')
    except:
        messages.error(request, "Patient Cannot be deleted, Patient is still on medication or an error occured")
        return redirect('all_pharmacist_patients')

    context={
        "patient":patient,
    }    
    return render(request,'pharmacist_templates/delete_patient.html',context)

@login_required
def addStock(request):
    form=StockForm(request.POST,request.FILES)
    if form.is_valid():
        form=StockForm(request.POST,request.FILES)
        form.save()
        return redirect('add_stock')
    context={
        "form":form,
        "title":"Add New Drug"
    }
    return render(request,'pharmacist_templates/add_stock.html',context)

@login_required
def addCategory(request):
    try:
        form=CategoryForm(request.POST or None)
        if request.method == 'POST':
            if form.is_valid():
                form.save()
                messages.success(request, "Category added Successfully!")
                return redirect('add_category')
    except:
        messages.error(request, "Category Not added! Try again")
        return redirect('add_category')   
    context={
        "form":form,
        "title":"Add a New Drug Category"
    }
    return render(request,'pharmacist_templates/add_category.html',context)

@login_required
def manageStock(request):
    stocks = Stock.objects.all().order_by("-id")
    ex = Stock.objects.annotate(
        expired=ExpressionWrapper(Q(valid_to__lt=Now()), output_field=BooleanField())
        ).filter(expired=True)
    eo = Stock.objects.annotate(
        expired=ExpressionWrapper(Q(valid_to__lt=Now()), output_field=BooleanField())
        ).filter(expired=False)
    context = {
        "stocks": stocks,
        "expired":ex,
        "expa":eo,
        "title":"Manage Stocked Drugs"
    }
    return render(request,'pharmacist_templates/manage_stock.html',context)

# # def dispenseDrug(request,pk):
# #     queryset=Patients.objects.get(id=pk)
# #     form=DispenseForm(initial={'patient_id':queryset})
# #     if request.method == 'POST':
# #         form=DispenseForm(request.POST or None)
# #         if form.is_valid():
# #             form.save()
            
    
# #     context={
# #         # "title":' Issue' + str(queryset.item_name),
# #         "queryset":queryset,
# #         "form":form,
# #         # "username":" Issue By" + str(request.user),
# #     }
# #     return render(request,"pharmacist_templates/dispense_drug.html",context)

# # def manageDispense(request):
# #     disp=De.objects.all()
# #     context={
# #         "prescrips":disp,
# #     }
# #     return render(request,'pharmacist_templates/manage_dispense.html',context)

# def dispense(request,pk):
#     queryset=Stock.objects.get(id=pk)
#     form=DispenseForm2(request.POST or None,instance=queryset)
#     if form.is_valid():
#         instance=form.save(commit=False)
#         instance.quantity-=instance.dispense_quantity
#         print(instance.drug_id.quantity)
#         print(instance.dispense_quantity)
#         instance.save()

#         return redirect('pharmacist_disp')

       
    
#     context={
#         "queryset":queryset,
#         "form":form,
#     }
#     return render(request,'pharmacist_templates/dispense_form.html',context)

