from django.http import HttpResponse
from django.shortcuts import render, redirect
import subprocess
import sys
# Make sure the upload_and_review function is in this file.
import json
import os
from django.contrib.auth.decorators import login_required
from django.contrib.auth import authenticate, login, logout
from django.shortcuts import render, redirect
from django.core.mail import send_mail
from .forms import ContactForm

from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from django.conf import settings





import logging
logger = logging.getLogger(__name__)

# Configure your API key
#Sgenai.configure(api_key=os.getenv("GOOGLE_API_KEY"))

def index(request):
    return render(request, 'index.html')

def software(request):
    return render(request, 'software.html')

def run_script(request):
        script_path = 'scripts/script1.py' # Replace with actual path
        try:
            process = subprocess.run([sys.executable, script_path], capture_output=True, text=True)
            #process = subprocess.run(['/path/to/your/venv/bin/python', script_path], capture_output=True, text=True)
            output = process.stdout if process.returncode == 0 else f"Error: {process.stderr}"
        except Exception as e:
            output = f"Exception: {str(e)}"
        return render(request, 'Result.html', {'output': output})




def signup_view(request):
    return render(request, 'signup.html')

def login_view(request):
    return render(request, 'login.html')



def contact_view(request):
    if request.method == 'POST':
            print("POST request received.")
            form = ContactForm(request.POST)
            print("Form data:", request.POST)
            if form.is_valid():
                print("Form is valid.")
                name = form.cleaned_data['name']
                email = form.cleaned_data['email']
                message = form.cleaned_data['message']
                send_mail(
                        subject=f"New Contact Form Submission from {name}",
                        message=message,
                        from_email='support@sawgrassai.com',
                        recipient_list=['support@sawgrassai.com'],
                        fail_silently=False,
                    )
                return render(request, 'success.html')
    return render(request, 'contact.html')