from django.shortcuts import render
from Web.forms import FeedbackForm
from django.core.mail import send_mail
from django.http import HttpResponse,HttpResponseRedirect
from django.urls import reverse
from django.conf import settings
from Web.models import Feedback
from django.utils import timezone
from django.core.mail import send_mail
import os
import smtplib, ssl
from Comptek import settings

# Create your views here.
def base(request):
    return render(request,'Web/Home/base.html')

def privacystatement(request):
    return render(request,'Web/Home/privacystatement.html')

def termsofuse(request):
    return render(request,'Web/Home/termsofuse.html')

def aboutus(request):
    return render(request,'Web/Aboutus/aboutus.html')

def vision(request):
    return render(request,'Web/Aboutus/vision.html')

def ourcustomers(request):
    args = {}
    text = os.listdir(os.path.join(settings.BASE_DIR,'static/images/Our_Client/'))
    text1=[]
    for i in text:
        text1.append('/static/images/Our_Client/'+i)
    args['mytext'] = text1
    return render(request,'Web/Aboutus/ourcustomers.html',args)

def careers(request):
    return render(request,'Web/Aboutus/careers.html')

def ourpartnerships(request):
    args = {}
    text = os.listdir(os.path.join(settings.BASE_DIR,'static/images/logo/'))
    text1=[]
    for i in text:
        text1.append('/static/images/logo/'+i)
    args['mytext'] = text1
    return render(request,'Web/Aboutus/ourpartnerships.html',args)

def products(request):
    return render(request,'Web/Products/products.html')

def hexagonproducts(request):
    return render(request,'Web/Products/hexagonproducts.html')

def connectedservicesplatform(request):
    return render(request,'Web/Products/connectedservicesplatform.html')

def pointsconnect(request):
    return render(request,'Web/Products/pointsconnect.html')

def appsconnect(request):
    return render(request,'Web/Products/appsconnect.html')

def bentleyproducts(request):
    return render(request,'Web/Products/bentleyproducts.html')

def schooladminsystem(request):
    return render(request,'Web/Products/schooladminsystem.html')

def remoteofficemanagement(request):
    return render(request,'Web/Products/remoteofficemanagement.html')

def retailenterprisemanagement(request):
    return render(request,'Web/Products/retailenterprisemanagement.html')

def midasproducts(request):
    return render(request,'Web/Products/midasproducts.html')

def limn(request):
    return render(request,'Web/Products/limn.html')

def otherpopularsoftware(request):
    return render(request,'Web/Products/otherpopularsoftware.html')

def solutions(request):
    return render(request,'Web/Solutions/solutions.html')

def services(request):
    return render(request,'Web/Services/services.html')

def strategyconsulting(request):
    return render(request,'Web/Services/strategyconsulting.html')

def systemsintegration(request):
    return render(request,'Web/Services/systemsintegration.html')

def enterpriseitinfrastructure(request):
    return render(request,'Web/Services/enterpriseitinfrastructure.html')

def newsandevents(request):
    return render(request,'Web/News/newsandevents.html')

def support(request):
    return render(request,'Web/Support/support.html')

def partners(request):
    return render(request,'Web/Partners/partners.html')

def pcp(request):
    return render(request,'Web/Partners/pcp.html')

def ptp(request):
    return render(request,'Web/Partners/ptp.html')

def pgp(request):
    return render(request,'Web/Partners/pgp.html')

def howtobecomeapartner(request):
    return render(request,'Web/Partners/howtobecomeapartner.html')

def partnertraining(request):
    return render(request,'Web/Partners/partnertraining.html')

import os
import ssl
import smtplib
from email.message import EmailMessage

from django.shortcuts import render


def contacts(request):
    if request.method == "POST":
        try:
            name = request.POST.get("name", "").strip()
            email = request.POST.get("email", "").strip()
            subject = request.POST.get("subject", "").strip()
            msg = request.POST.get("message", "").strip()

            smtp_server = "mail.comptek.in"
            smtp_port = 465

            sender_email = "umpc@comptek.in"
            receiver_email = "umpc@comptek.in"

            # Store this in an environment variable
            password = os.environ.get("Comptek$$$29")

            if not password:
                raise Exception("SMTP_PASSWORD environment variable is not set")

            message = EmailMessage()

            message["Subject"] = subject or "New Contact Form Message"
            message["From"] = sender_email
            message["To"] = receiver_email

            # When you click Reply, it will reply to the visitor
            if email:
                message["Reply-To"] = email

            message.set_content(
                f"""New contact form submission

Name: {name}
Email: {email}
Subject: {subject}

Message:
{msg}
"""
            )

            context = ssl.create_default_context()

            with smtplib.SMTP_SSL(
                smtp_server,
                smtp_port,
                context=context
            ) as server:
                server.login(sender_email, password)
                server.send_message(message)

            return render(
                request,
                "Web/Contacts/contacts.html",
                {"Status": True}
            )

        except Exception as e:
            print(f"SMTP Error: {e}")

            return render(
                request,
                "Web/Contacts/contacts.html",
                {"Status": False}
            )

    return render(request, "Web/Contacts/contacts.html")