import os
from django.shortcuts import render
from django.utils import timezone
from .models import ScriptLog
from .obfuscator import obfuscate

def home(request):
    """Render the landing screen with the obfuscator interface."""
    output_code = ""
    input_code = ""
    
    if request.method == "POST":
        input_code = request.POST.get("code", "")
        if input_code:
            output_code = obfuscate(input_code)
            
            # Save to log
            ScriptLog.objects.create(
                input_code=input_code,
                output_code=output_code,
                input_size=len(input_code),
                output_size=len(output_code)
            )

    host_name = request.get_host().lower()
    agent_brand = "AppWizzy" if host_name == "appwizzy.com" else "Flatlogic"
    now = timezone.now()

    context = {
        "project_name": "VM-Luau Obfuscator",
        "agent_brand": agent_brand,
        "input_code": input_code,
        "output_code": output_code,
        "current_time": now,
        "project_description": os.getenv("PROJECT_DESCRIPTION", "Strong Luau Obfuscation with Custom VM Protection."),
    }
    return render(request, "core/index.html", context)
