from django.shortcuts import get_object_or_404
from django.contrib.auth.models import User
from childmonitoring.models import (
    SunChild, AppointmentStatus, Appointment, Site, Region,
)


# Get List of all Appointments
def all_appointments():
    all_appointments = Appointment.objects.all()
    appointments_output = []

    for appointment in all_appointments:

        # Assuming there is a related class object
        specialist = appointment.specialist
        sun_child = appointment.sun_child
        status = appointment.status
        region = appointment.region
        site = appointment.site

        appointments_output.append({
            'id': appointment.id,
            'title': appointment.name,
            'start': appointment.start.strftime("%m/%d/%Y, %H:%M:%S"),
            'end': appointment.end.strftime("%m/%d/%Y, %H:%M:%S"),
            'specialist': {
                'id': specialist.id if specialist else None,
                'full_name': f"{specialist.first_name}" if specialist else None,  # First name of the specialist
                'speciality':  f"{specialist.last_name}" if specialist else None,  # Last name of the specialist
            },
            'sun_child': {
                'id': sun_child.id if sun_child else None,
                'full_name': f"{sun_child.first_name} {sun_child.last_name}" if sun_child else None,  # Full name of the child
            },
            'region': {
                'id': region.id if region else None,
                'name': region.name if region else None,
            },
            'site': {
                'id': site.id if site else None,
                'name': site.name if site else None,
            },
            'status': {
                'id': status.id if status else None,
                'status': status.status if status else None,
            },
            'comment': appointment.comment,
        })

    return appointments_output


def list_appointments():
    all_appointments = Appointment.objects.all()
    specialists = User.objects.all()
    children_sun = SunChild.objects.all()
    regions = Region.objects.all()
    site = Site.objects.all()
    all_status = AppointmentStatus.objects.all()

    context = {
        'events': all_appointments,
        'specialists': specialists,
        'children_sun': children_sun,
        'regions': regions,
        'sites': site,
        'all_status': all_status,
    }
    return context


# Add appointment to the calendar
def create_appointment(request):
    title = request.GET.get('title', None)
    start = request.GET.get('start', None)
    end = request.GET.get('end', None)
    comment = request.GET.get('comment', None)
    specialist_id = request.GET.get('specialist', None)
    sun_child_id = request.GET.get('sunChild', None)
    region_id = request.GET.get('region', None)
    site_id = request.GET.get('site', None)
    status_id = request.GET.get('status', None)

    specialist = get_object_or_404(User, id=specialist_id)
    sun_child = get_object_or_404(SunChild, id=sun_child_id)
    region = get_object_or_404(Region, id=region_id)
    site = get_object_or_404(Site, id=site_id)
    status = get_object_or_404(AppointmentStatus, id=status_id)

    appointment = Appointment(
        name=str(title), start=start, end=end, comment=comment, specialist=specialist, sun_child=sun_child, region=region, 
        site=site, status=status,
    )
    return appointment
