from django.shortcuts import render, redirect, get_object_or_404
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from django.http import HttpResponse
from .models import (
    Farmer, AgriculturalHolding, Region, Constituency,
    CropProduction, LivestockProduction, Forestry, Fishery, LandRegistry
)
from .forms import FarmerForm, HoldingForm
import csv
from django.db.models import Sum, Count

def dashboard(request):
    """Public National Dashboard Overview with Analytics"""
    total_farmers = Farmer.objects.count()
    total_holdings = AgriculturalHolding.objects.count()
    regions = Region.objects.all()
    
    # Simple stats per region
    region_stats = []
    for region in regions:
        farmers_in_region = Farmer.objects.filter(constituency__region=region).count()
        region_stats.append({'name': region.name, 'count': farmers_in_region})
    
    # Stats by primary activity
    activity_stats = []
    for code, label in AgriculturalHolding.HOLDING_TYPES:
        count = AgriculturalHolding.objects.filter(primary_activity=code).count()
        activity_stats.append({'label': label, 'value': count})

    # Detailed Crop Distribution
    crop_distribution = list(CropProduction.objects.values('crop_type').annotate(
        total_area=Sum('area_hectares')
    ).order_by('-total_area')[:10])

    # Detailed Livestock Distribution
    livestock_distribution = list(LivestockProduction.objects.values('animal_type').annotate(
        total_count=Sum('count')
    ).order_by('-total_count')[:10])

    # Land Tenure Distribution
    land_tenure_distribution = list(LandRegistry.objects.values('ownership_type').annotate(
        total_area=Sum('area_hectares'),
        count=Count('id')
    ))

    # New Module Stats
    crop_stats = CropProduction.objects.aggregate(total_area=Sum('area_hectares'), total_expected_yield=Sum('expected_yield'))
    livestock_stats = LivestockProduction.objects.aggregate(total_animals=Sum('count'))
    forestry_stats = Forestry.objects.aggregate(total_area=Sum('area_hectares'))
    fishery_stats = Fishery.objects.aggregate(total_units=Sum('capacity'))
    land_stats = LandRegistry.objects.aggregate(total_area=Sum('area_hectares'), total_parcels=Count('id'))

    context = {
        "project_name": "NAIMS - Namibia",
        "total_farmers": total_farmers,
        "total_holdings": total_holdings,
        "region_stats": sorted(region_stats, key=lambda x: x['count'], reverse=True),
        "activity_stats": activity_stats,
        "crop_distribution": crop_distribution,
        "livestock_distribution": livestock_distribution,
        "land_tenure_distribution": land_tenure_distribution,
        "all_regions": regions,
        "crop_stats": crop_stats,
        "livestock_stats": livestock_stats,
        "forestry_stats": forestry_stats,
        "fishery_stats": fishery_stats,
        "land_stats": land_stats,
    }
    return render(request, "core/index.html", context)

@login_required
def farmer_list(request):
    """View list of registered farmers with simple filtering."""
    region_id = request.GET.get('region')
    farmers = Farmer.objects.all().select_related('constituency__region')
    
    if region_id:
        farmers = farmers.filter(constituency__region_id=region_id)
        
    regions = Region.objects.all()
    return render(request, "core/farmer_list.html", {
        "farmers": farmers,
        "regions": regions,
        "selected_region": int(region_id) if region_id else None
    })

@login_required
def farmer_register(request):
    """Register a new farmer and their first holding."""
    if request.method == "POST":
        f_form = FarmerForm(request.POST)
        h_form = HoldingForm(request.POST)
        
        if f_form.is_valid() and h_form.is_valid():
            farmer = f_form.save()
            holding = h_form.save(commit=False)
            holding.farmer = farmer
            holding.save()
            
            messages.success(request, f"Farmer {farmer.name} registered successfully!")
            return redirect('farmer_list')
    else:
        f_form = FarmerForm()
        h_form = HoldingForm()
        
    return render(request, "core/farmer_form.html", {
        "f_form": f_form,
        "h_form": h_form
    })

@login_required
def farmer_detail(request, pk):
    """View details of a single farmer."""
    farmer = get_object_or_404(Farmer.objects.select_related('constituency__region'), pk=pk)
    return render(request, "core/farmer_detail.html", {"farmer": farmer})

def export_report(request):
    """Public export of agricultural holdings report with full module details."""
    response = HttpResponse(content_type='text/csv')
    response['Content-Disposition'] = 'attachment; filename="naims_comprehensive_agricultural_report.csv"'
    
    writer = csv.writer(response)
    # Comprehensive header
    writer.writerow([
        'Farmer Name', 'National ID', 'Region', 'Constituency', 
        'Primary Activity', 'Total Size (Ha)', 
        'Crops', 'Livestock', 'Forestry', 'Fisheries', 'Land Parcels'
    ])
    
    farmers = Farmer.objects.all().select_related('constituency__region')
    for f in farmers:
        holdings = f.holdings.all()
        total_size = holdings.aggregate(Sum('size_hectares'))['size_hectares__sum'] or 0
        
        # Summary of sub-modules
        crops = ", ".join([f"{c.crop_type}({c.area_hectares}ha)" for h in holdings for c in h.crops.all()])
        livestock = ", ".join([f"{l.animal_type}({l.count})" for h in holdings for l in h.livestock.all()])
        forestry = ", ".join([f"{fo.tree_species}({fo.area_hectares}ha)" for h in holdings for fo in h.forestry_items.all()])
        fisheries = ", ".join([f"{fi.species}({fi.capacity})" for h in holdings for fi in h.fisheries.all()])
        land = ", ".join([f"Parcel {l.parcel_number}({l.area_hectares}ha)" for l in f.land_records.all()])

        writer.writerow([
            f.name,
            f.id_number,
            f.constituency.region.name,
            f.constituency.name,
            ", ".join([h.get_primary_activity_display() for h in holdings]),
            total_size,
            crops,
            livestock,
            forestry,
            fisheries,
            land
        ])
        
    return response