from django.shortcuts import render, redirect
from django.urls import reverse
from django.templatetags.static import static
from django.views import View
from django.views.generic import TemplateView, DetailView
from datetime import date

from doctors.models import Doctor
from common.models import Departments
from appointments.models import Appointments
from blogs.models import Blogs
from utilities.email_wrapper import EmailWrapper
from utilities.models import GlobalSettings


class HomePageView(TemplateView):
    template_name = "website/index.html"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        gs = GlobalSettings.objects.first()
        bg = static('assets/images/bg_main.png')
        today = date.today().isoformat()
        departments = Departments.objects.filter(status=True)
        doctors = Doctor.objects.filter(status=True)
        blogs = Blogs.objects.filter(status=True)

        context.update({
            "hospital_name": gs.hospital_name,
            "hospital_location": gs.hospital_location,
            "emergency_contact": gs.emergency_contact,
            "hospital_email": gs.hospital_email,
            "bg": bg,
            "departments": departments,
            "doctors": doctors,
            "today": today,
            "blogs": blogs,
        })
        return context


class BookAppointmentView(View):
    def post(self, request, *args, **kwargs):
        phone = request.POST.get("phone")
        name = request.POST.get("name")
        email = request.POST.get("email")
        department = request.POST.get("department")
        appointment_date = request.POST.get("appointment_date")

        # Create Appointment
        Appointments.objects.create(
            department_id=department,
            patient_name=name,
            patient_phone=phone,
            appointment_date=appointment_date,
            patient_email=email
        )

        # Prepare Redirect URL
        url = reverse("home_page") + "?booking_confirmation=ok"

        # Send Email
        gs = GlobalSettings.objects.first()
        email_obj = EmailWrapper(
            subject="Grace Ortho Care Appointment Received",
            to=email,
        )
        context = {
            "user_name": name,
            "department_name": department,
            "appointment_date": appointment_date,
            "hospital_logo": gs.hospital_logo.url,
            "hospital_name": gs.hospital_name,
            "hospital_location": gs.hospital_location,
            "emergency_contact": gs.emergency_contact,
            "hospital_email": gs.hospital_email,
        }
        email_obj.send_with_template('email_templates/appointment_confirmation.html', context)
        return redirect(url)


class AboutUsView(TemplateView):
    template_name = "website/about_us.html"


class OrthoView(TemplateView):
    template_name = "website/ortho.html"


class GalleryView(TemplateView):
    template_name = "website/gallery.html"


class BlogDetailView(DetailView):
    model = Blogs
    template_name = "website/blogs.html"
    context_object_name = "blog"

    def get_object(self, queryset=None):
        blog_id = self.kwargs.get("blog_id")
        return Blogs.objects.filter(id=blog_id).first()
