from django.shortcuts import render
from .models import *
from django.core.paginator import Paginator

def index(request):
    context = {
        'title': 'Water, Environment, and Peace',
        'description': 'Discover MEDRC work at the intersection of water, environment, and peace. We provide innovative solutions for freshwater scarcity, climate change, and conflict resolution in the MENA region through research, training, and collaboration.',
        'content': LandingContent.objects.all(),
    }

    return render(request, 'info/index.html', context)

def about(request):
    context = {
        'title': 'About Us',
        'description': 'Explore how MEDRC addresses global challenges at the intersection of environment and peace, conducting research, training, and peacebuilding to tackle cross-border environmental and climate pressures.',
        'content': AboutContent.objects.all(),
    }

    return render(request, 'info/about.html', context)

def publications(request):
    qs = Publications.objects.all().order_by('-id')
    selected_category = request.GET.get('category', 'All')

    if selected_category == 'Water':
        qs = qs.filter(publication_water=True)
    elif selected_category == 'Environment':
        qs = qs.filter(publication_environment=True)
    elif selected_category == 'Peace':
        qs = qs.filter(publication_peace=True)

    paginator = Paginator(qs, 8)
    page = request.GET.get('page')
    page_obj = paginator.get_page(page)
        
    context = {
        'title': 'Publications',
        'description': 'Explore MEDRC extensive collection of research publications on water scarcity, environmental conflict resolution, desalination technologies, and sustainable development in the MENA region.',
        'content': page_obj,
        'selected_category': selected_category,
        'page_obj': page_obj,
    }

    return render(request, 'info/publications.html', context)

def water(request):
    context = {
        'title': 'Water',
        'description': 'Discover MEDRC training and applied research programs, focused on innovative desalination, water reuse, and sustainable solutions to tackle freshwater scarcity in the MENA region.',
        'content': WaterContent.objects.all(),
    }

    return render(request, 'info/water.html', context)

def applied_research(request):
    context = {
        'title': 'Applied Research',
        'description': 'Explore MEDRC cutting-edge research in reverse osmosis technologies, renewable energy desalination, and environmental impact, supporting innovation across the MENA region.',
        'content': WaterContent.objects.all(),
    }

    return render(request, 'info/water_applied_research.html', context)

def training(request):
    context = {
        'title': 'Training & Consultancy',
        'description': 'Discover MEDRC Water Training Centre, a unique regional facility offering training, research, and consultancy in desalination and wastewater reuse.',
        'content': WaterContent.objects.all(),
    }

    return render(request, 'info/water_training.html', context)

def environment(request):
    context = {
        'title': 'Environment',
        'description': 'Learn how MEDRC expanded mandate integrates water, energy, and climate issues, fostering research and dialogue on sustainability in the MENA region.',
        'content': EnvironmentContent.objects.all(),
    }

    return render(request, 'info/environment.html', context)

def climate_change(request):
    context = {
        'title': 'Climate Change',
        'description': 'Explore how MEDRC supports climate resilience and transboundary dialogue, helping governments with adaptation, mitigation, and conflict-sensitive climate action.',
        'content': EnvironmentContent.objects.all(),
    }

    return render(request, 'info/environment_climate_change.html', context)

def WEF_nexus(request):
    context = {
        'title': 'WEF Nexus',
        'description': 'Discover MEDRC work on the Water-Energy-Food-Ecosystems Nexus, balancing technical expertise and conflict-sensitive approaches in the MENA region.',
        'content': EnvironmentContent.objects.all(),
    }

    return render(request, 'info/environment_WEF_nexus.html', context)

def peace(request):
    context = {
        'title': 'Peace',
        'description': 'Explore how MEDRC leverages transboundary environmental issues to foster peace, conduct conflict resolution, and enhance international cooperation.',
        'content': PeaceContent.objects.all(),
    }

    return render(request, 'info/peace.html', context)

def history(request):
    context = {
        'title': 'History',
        'description': 'Learn about MEDRC, a unique multilateral organization from the Middle East Peace Process, dedicated to conflict resolution through diplomacy and cooperation.',
        'content': PeaceContent.objects.all(),
    }

    return render(request, 'info/peace_history.html', context)

def MEDRC_model(request):
    context = {
        'title': 'MEDRC Model',
        'description': 'Discover how the MEDRC Conflict Resolution Model leverages environmental cooperation to support peace processes in the Middle East and beyond.',
        'content': PeaceContent.objects.all(),
    }

    return render(request, 'info/peace_MEDRC_model.html', context)
