from django.shortcuts import render
from django.conf import settings
import requests
from .models import Contact
from .forms import FormWithCaptcha

# Create your views here.


def index(request):
    return render(request, "webpage/index.html", {})


def contact(request):
    if request.method == "POST":
        name: str = request.POST.get("name")
        email: str = request.POST.get("email")
        phone_number: str = request.POST.get("phone_number")

        street: str = request.POST.get("street")
        city: str = request.POST.get("city")
        state: str = request.POST.get("state")
        zip_code: str = request.POST.get("zip")
        capchaForm = FormWithCaptcha(request.POST)
        

        url = "https://api.example-reverse-search-service.com/v1/search"
        params = {
            "name": name,
            "email": email,
            "phone": phone_number,

            "street": street,
            "city": city,
            "state": state,
            "zip_code": zip_code,
            "api_key": 'some_key',
        }
        try:
            contact_obj = Contact.objects.get_or_create(
                name=name,
                email=email,
                phone_number=phone_number,
                street=street,
                city=city,
                state=state,
                zip_code=zip_code
            )
            capcha = None if settings.DEBUG else FormWithCaptcha()
            return render(request, "webpage/contact.html", {'capchaForm':capcha})
        except:
            return render(request, "webpage/contact.html", {'capchaForm':capchaForm, 'error':'There was an error. Try again later' })        
        
    capcha = None if settings.DEBUG else FormWithCaptcha()
    return render(request, "webpage/contact.html", {'capchaForm':capcha})


def process(request):
    return render(request, "webpage/process.html", {})


def about_us(request):
    return render(request, "webpage/about_us.html", {})
