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 Plant, Product, Inventory, Machine, WorkOrder, Inspection

def home(request):
    """Render the ERP dashboard with summary statistics."""
    host_name = request.get_host().lower()
    agent_brand = "AppWizzy" if host_name == "appwizzy.com" else "Flatlogic"
    now = timezone.now()

    # ERP Stats
    stats = {
        "plants_count": Plant.objects.count(),
        "products_count": Product.objects.count(),
        "inventory_count": Inventory.objects.count(),
        "machines_count": Machine.objects.count(),
        "active_work_orders": WorkOrder.objects.filter(status='in_progress').count(),
        "recent_inspections": Inspection.objects.order_by('-timestamp')[:5],
    }

    context = {
        "project_name": "Multi-Plant ERP",
        "agent_brand": agent_brand,
        "django_version": django_version(),
        "python_version": platform.python_version(),
        "current_time": now,
        "host_name": host_name,
        "stats": stats,
    }
    return render(request, "core/index.html", context)