from django.shortcuts import render, redirect, get_object_or_404
from django.contrib.auth.decorators import login_required
from .models import Fanpage, Flow, MessageLog, ChatSession

def home(request):
    if request.user.is_authenticated:
        return redirect('dashboard')
    return render(request, 'core/index.html')

@login_required
def dashboard(request):
    fanpages = Fanpage.objects.all()
    flows = Flow.objects.all()
    recent_logs = MessageLog.objects.order_by('-timestamp')[:10]
    
    context = {
        'fanpage_count': fanpages.count(),
        'flow_count': flows.count(),
        'fanpages': fanpages,
        'recent_logs': recent_logs,
    }
    return render(request, 'core/dashboard.html', context)

@login_required
def fanpage_list(request):
    fanpages = Fanpage.objects.all()
    return render(request, 'core/fanpage_list.html', {'fanpages': fanpages})

@login_required
def flow_list(request):
    flows = Flow.objects.all()
    return render(request, 'core/flow_list.html', {'flows': flows})
