from django.shortcuts import render
from django.urls import reverse_lazy, reverse
from django.views.generic.base import TemplateView
from django.views.generic.edit import FormView, CreateView, UpdateView, DeleteView
from django.views.generic.detail import DetailView
from django.views.generic.list import ListView

from charts.models import Chart, Entry
from charts.forms import ChartForm


from django.db.models import (Exists, OuterRef,
    Prefetch, Subquery, Count, Sum, F, Q, Value)
from django.db.models.functions import Length, Upper


class ListView(ListView):
    model = Chart

class CreateView(CreateView):
    model = Chart
    template_name_suffix = '_create_form'
    form_class = ChartForm

class DetailView(DetailView):
    model = Chart

    def get_object(self):
        queryset = Chart.objects.filter(
            pk=self.kwargs.get(self.pk_url_kwarg)
        ).prefetch_related(
            Prefetch('entry_set', queryset=Entry.objects.select_related(
                'oralcare',
                'bathing',
                'toileting',
                'dressing',
                'meal',
                'rangeofmotion',
                'ambulation',
                'vitals',
                'fluidintake',
                'urine',
                'emesis',
                'bowelmovement',
                'painlevel',
                'oxygenlevel',
                'restraint',
                'safety',
            ))
        )
        obj = queryset.get()
        return obj

class UpdateView(UpdateView):
    model = Chart
    form_class = ChartForm

class DeleteView(DeleteView):
    model = Chart
    success_url = reverse_lazy('chart-list')