from django.shortcuts import render,redirect
from django.contrib import messages
from django.http import HttpResponse
from patient.forms import *
from .models import *
from .forms import *
from appointment.models import *
from appointment.forms import *
from report.models import *
from bill.models import *
from django.shortcuts import get_object_or_404
from django.contrib.auth.decorators import login_required
import datetime

def home(request):
    return render(request,'index.html',locals())


def register_patient(request):
    if request.method == 'POST':
        form = Patient_register(request.POST)
        profile_form = Patient_Profile(request.POST)
        if form.is_valid():
            user = form.save()
            profile = profile_form.save(commit = False)
            profile.user = user
            profile.save()
            messages.success(request, f'Your account has been created! You are now able to log in')
            return redirect('login')
    else:
        form = Patient_register()
        profile_form = Patient_Profile()
    return render(request, 'patient/register.html', locals())
     
@login_required
def book_appointment(request):
    if request.method == 'POST':
        book = Patient_Appointment(request.POST)
        check = Appointment.objects.filter(status = 'pending')
        patient_id = Patient.objects.get(user_id = request.user.id)
        check = check.filter(patient_id = patient_id)
        if book.is_valid():
            if not check:
                reason = book.cleaned_data['reason']
                doctor = book.cleaned_data['doctor_id']
                new_appointment = Appointment()
                new_appointment.patient_id = patient_id
                new_appointment.status = 'pending'
                new_appointment.reason = reason
                new_appointment.doctor_id = doctor
                new_appointment.save()
                messages.success(request, f'Asked for appointment. Keep checking for your timings')
                return redirect('home')
            else:
                messages.error(request,f'Wait for your previous appointment to be confirmed')
                return redirect('home')
    else:
        book = Patient_Appointment()
    return render(request,'patient/book_appointment.html',locals())


@login_required
def my_appointments(request):
    patient_id = Patient.objects.get(user_id = request.user.id)
    booked_appointments = Appointment.objects.filter(patient_id = patient_id)
    booked_appointments = booked_appointments.filter(status='booked')
    completed_appointments = booked_appointments.filter(time__lte = datetime.datetime.now())
    upcoming_appointments = booked_appointments.filter(time__gte = datetime.datetime.now())
    pending_appointments = Appointment.objects.filter(patient_id = patient_id)
    pending_appointments =  pending_appointments.filter(status='pending')
    return render(request,'patient/my_appointments.html',locals())


@login_required
def my_reports(request):
    patient_id = Patient.objects.get(user_id = request.user.id)
    report = Report.objects.filter(patient_id= patient_id)
    return render(request,'patient/my_reports.html',locals())

@login_required
def my_bill(request,pk):
    check = Bill.objects.filter(report_id = pk).exists()
    if check:
        bill = Bill.objects.get(report_id=pk)
        return render(request,'patient/my_bill.html',locals())
    else:
        messages.warning(request,f'Bill not generated by management yet')
        return redirect('patient-report')


@login_required
def patient_list(request):
    if request.user.is_superuser:
        list = Patient.objects.all()
        return render(request,'admin/patient_list.html',locals())
    else:
        messages.warning(request,f'Request Denied')
        return redirect('home')