import os
import platform

from django import get_version as django_version
from django.shortcuts import render
from django.utils import timezone
from .models import Hospital, DepartmentChief

def home(request):
    """Render the landing screen with hospital directory."""
    query = request.GET.get('q', '')
    state_filter = request.GET.get('state', '')
    dept_filter = request.GET.get('dept', '')

    chiefs = DepartmentChief.objects.select_related('hospital').all()

    if query:
        chiefs = chiefs.filter(name__icontains=query) | chiefs.filter(hospital__name__icontains=query)
    
    if state_filter:
        chiefs = chiefs.filter(hospital__state=state_filter)
    
    if dept_filter:
        chiefs = chiefs.filter(department=dept_filter)

    context = {
        "project_name": "MedConnect Outreach",
        "chiefs": chiefs,
        "states": Hospital.STATES_CHOICES,
        "departments": DepartmentChief.DEPARTMENT_CHOICES,
        "query": query,
        "selected_state": state_filter,
        "selected_dept": dept_filter,
    }
    return render(request, "core/index.html", context)