import os
import platform

from django import get_version as django_version
from django.contrib.auth.decorators import login_required
from django.shortcuts import render, redirect, get_object_or_404
from django.views.decorators.http import require_POST
from django.http import JsonResponse
from django.utils import timezone
from .models import Agent
from .forms import SignUpForm
from django.contrib.auth import login
from django.contrib.auth import views as auth_views

class CustomLoginView(auth_views.LoginView):
    template_name = 'core/login.html'

def home(request):
    """Render the landing screen with loader and environment details."""
    host_name = request.get_host().lower()
    agent_brand = "AppWizzy" if host_name == "appwizzy.com" else "Flatlogic"
    now = timezone.now()
    published_agents = Agent.objects.filter(published=True)

    context = {
        "project_name": "New Style",
        "agent_brand": agent_brand,
        "django_version": django_version(),
        "python_version": platform.python_version(),
        "current_time": now,
        "host_name": host_name,
        "project_description": os.getenv("PROJECT_DESCRIPTION", ""),
        "project_image_url": os.getenv("PROJECT_IMAGE_URL", ""),
        "agents": published_agents,
    }
    return render(request, "core/index.html", context)

@login_required
def create_agent(request):
    if request.method == 'POST':
        name = request.POST.get('name')
        description = request.POST.get('description')
        agent = Agent.objects.create(name=name, description=description, user=request.user)
        return redirect('my_agents')
    return render(request, "core/create_agent.html")

@login_required
def my_agents(request):
    agents = Agent.objects.filter(user=request.user)
    return render(request, "core/my_agents.html", {"agents": agents})

@login_required
def publish_agent(request, agent_id):
    agent = get_object_or_404(Agent, id=agent_id, user=request.user)
    agent.published = not agent.published
    agent.save()
    return redirect('my_agents')

@login_required
@require_POST
def delete_agent(request, agent_id):
    agent = get_object_or_404(Agent, id=agent_id, user=request.user)
    agent.delete()
    return redirect('my_agents')

@login_required
def edit_agent(request, agent_id):
    agent = get_object_or_404(Agent, id=agent_id, user=request.user)
    if request.method == 'POST':
        agent.name = request.POST.get('name')
        agent.description = request.POST.get('description')
        agent.save()
        return redirect('my_agents')
    return render(request, 'core/edit_agent.html', {'agent': agent})

def agent_detail(request, agent_id):
    agent = get_object_or_404(Agent, id=agent_id)
    return render(request, 'core/article_detail.html', {'agent': agent})

def signup(request):
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if form.is_valid():
            user = form.save()
            login(request, user)
            return redirect('home')
    else:
        form = SignUpForm()
    return render(request, 'core/signup.html', {'form': form})
