import os
import platform

from django import get_version as django_version
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.views import LoginView, LogoutView
from django.shortcuts import render, redirect
from django.urls import reverse_lazy
from django.views.generic import CreateView
from .forms import ScamReportForm, SignUpForm
from .models import ScamReport


def index(request):
    """Render the homepage with a scam submission form."""
    form = ScamReportForm()
    return render(request, 'core/index.html', {'form': form})


def analyze_report(request):
    """Process the scam report submission and display an analysis page."""
    if request.method == 'POST':
        form = ScamReportForm(request.POST)
        if form.is_valid():
            report = form.save()
            return render(request, 'core/analysis_result.html', {'report': report})
    # Redirect to home if not a POST request or form is invalid
    return redirect('index')



class SignupView(CreateView):
    form_class = SignUpForm
    success_url = reverse_lazy('login')
    template_name = 'registration/signup.html'


class CustomLoginView(LoginView):
    template_name = 'registration/login.html'
    redirect_authenticated_user = True

    def get_success_url(self):
        return reverse_lazy('index')


class CustomLogoutView(LogoutView):
    next_page = reverse_lazy('index')


from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
import json

@csrf_exempt
def api_analyze(request):
    """API endpoint to analyze content for scams."""
    if request.method == 'POST':
        try:
            data = json.loads(request.body)
            content_to_analyze = data.get('content')

            if content_to_analyze:
                # In the future, we will add real analysis logic here.
                # For now, we'll return a placeholder response.
                return JsonResponse({'status': 'ok', 'message': 'Analysis complete.'})
            else:
                return JsonResponse({'status': 'error', 'message': 'No content provided.'}, status=400)
        except json.JSONDecodeError:
            return JsonResponse({'status': 'error', 'message': 'Invalid JSON.'}, status=400)

    return JsonResponse({'status': 'error', 'message': 'Invalid request method.'}, status=405)
