import os
import platform

from django import get_version as django_version
from django.shortcuts import render, get_object_or_404
from django.utils import timezone
from .models import Member

def home(request):
    """Render the landing screen with directory stats."""
    host_name = request.get_host().lower()
    agent_brand = "AppWizzy" if host_name == "appwizzy.com" else "Flatlogic"
    
    stats = {
        'total': Member.objects.count(),
        'staff': Member.objects.filter(role='STAFF').count(),
        'board': Member.objects.filter(role='BOARD').count(),
        'volunteers': Member.objects.filter(role='YOUTH_VOLUNTEER').count(),
    }

    context = {
        "project_name": "TeamLink",
        "agent_brand": agent_brand,
        "django_version": django_version(),
        "python_version": platform.python_version(),
        "current_time": timezone.now(),
        "stats": stats,
    }
    return render(request, "core/index.html", context)

def member_list(request):
    """View to list all members with filtering and search."""
    query = request.GET.get('q')
    role_filter = request.GET.get('role')
    
    members = Member.objects.all()
    
    if query:
        members = members.filter(full_name__icontains=query) | members.filter(email__icontains=query)
    
    if role_filter:
        members = members.filter(role=role_filter)
        
    context = {
        "members": members,
        "query": query,
        "role_filter": role_filter,
    }
    return render(request, "core/member_list.html", context)

def member_detail(request, pk):
    """View for individual member details."""
    member = get_object_or_404(Member, pk=pk)
    return render(request, "core/member_detail.html", {"member": member})