import logging
import time
from django.shortcuts import render, redirect
from django.contrib import messages
from django.core.mail import send_mail
from django.template.loader import render_to_string
from django.utils.html import strip_tags
from django.conf import settings

from hub.services.forms import LeadForm
from hub.services.odoo import OdooAPI

logger = logging.getLogger(__name__)


def contact(request):
    return render(request, "services/contact_form.html")


def thank_you(request):
    return render(request, "services/thank_you.html")


def contact_form(request):
    if request.method == "POST":
        # Spam protection checks
        honeypot_value = request.POST.get("website", "")
        timestamp_value = request.POST.get("form_timestamp", "0")
        current_time = int(time.time())

        # Check 1: Honeypot field should be empty
        if honeypot_value:
            # Bot detected - silently redirect
            return redirect("services:homepage")

        # Check 2: Form shouldn't be submitted too quickly (< 3 seconds)
        try:
            form_time = int(timestamp_value)
            if current_time - form_time < 3:
                # Too quick submission - likely a bot
                return redirect("services:homepage")
        except ValueError:
            # Invalid timestamp - likely a bot
            return redirect("services:homepage")

        # Continue with normal form processing
        form = LeadForm(request.POST)
        if form.is_valid():
            from hub.services.models import Lead, Service, ServiceOffering, Plan

            # Create a lead with context information
            lead = Lead(
                name=form.cleaned_data["name"],
                email=form.cleaned_data["email"],
                message=form.cleaned_data["message"] or "",
                company=form.cleaned_data["company"],
                phone=form.cleaned_data["phone"],
            )

            # Store the source/context information
            source = request.POST.get("source", "Contact Form")
            details = request.POST.get("details", "")
            next_url = request.POST.get("next", "/")

            # Handle service/offering/plan data
            service_id = request.POST.get("service_id")
            service_name = request.POST.get("service_name", "")
            offering_id = request.POST.get("offering_id")
            offering_name = request.POST.get("offering_name", "")
            plan_id = request.POST.get("plan_id")
            plan_name = request.POST.get("plan_name", "")

            # Link to related objects if they exist
            if service_id:
                try:
                    lead.service = Service.objects.get(id=service_id)
                except Service.DoesNotExist:
                    pass

            if offering_id:
                try:
                    lead.offering = ServiceOffering.objects.get(id=offering_id)
                except ServiceOffering.DoesNotExist:
                    pass

            if plan_id:
                try:
                    lead.plan = Plan.objects.get(id=plan_id)
                except Plan.DoesNotExist:
                    pass

            # Add context to the message and lead_title
            lead_title = ""
            context_info = []

            if source:
                context_info.append(f"Source Page: {source}")
                lead_title += f" - {source}"

            if details:
                context_info.append(f"Details: {details}")
                lead_title += f" - {details}"

            # Add service information to lead title and message
            service_info = []
            if service_name:
                service_info.append(f"Service: {service_name}")
                lead_title += f" - {service_name}"

            if offering_name:
                service_info.append(f"Provider: {offering_name}")

            if plan_name:
                service_info.append(f"Plan: {plan_name}")

            # Handle selected choice if present
            selected_choice = request.POST.get("selected_choice", "")
            if selected_choice:
                try:
                    choice_id, choice_name = selected_choice.split("|", 1)
                    # Add selected choice to message
                    service_info.append(f"Selected Plan: {choice_name}")

                    # Try to set the plan based on the choice_id
                    try:
                        lead.plan = Plan.objects.get(id=choice_id)
                    except Plan.DoesNotExist:
                        pass
                except ValueError:
                    pass

            if service_info:
                context_info.append("Service Information: " + ", ".join(service_info))

            # Format the final message with all context
            formatted_context = "<br>".join(context_info)
            original_message = lead.message
            lead.message = (
                lead.message + "<br><br>" + formatted_context
                if lead.message
                else formatted_context
            )

            try:
                logger.info(
                    f"Contact form submission for {service_name or 'general inquiry'}"
                )
                odoo = OdooAPI()
                odoo_lead_id = odoo.create_lead(lead, lead_title)
                lead.odoo_lead_id = odoo_lead_id
                lead.save()
                logger.info(f"Successfully created lead with Odoo ID: {odoo_lead_id}")

                # Send confirmation email
                try:
                    # Prepare context for email template
                    email_context = {
                        "name": lead.name,
                        "email": lead.email,
                        "company": lead.company or "Not provided",
                        "phone": lead.phone or "Not provided",
                        "message": original_message,
                        "service": service_name or "General inquiry",
                        "provider": offering_name or "Not specified",
                        "plan": plan_name or "Not specified",
                    }

                    # Render email content from template
                    html_message = render_to_string(
                        "email/lead_confirmation.html", email_context
                    )
                    plain_message = render_to_string(
                        "email/lead_confirmation.txt", email_context
                    )
                    # Send the email
                    send_mail(
                        subject=f'Thank you for contacting Servala about {service_name or "our services"}',
                        message=plain_message,
                        html_message=html_message,
                        from_email=settings.DEFAULT_FROM_EMAIL,
                        recipient_list=[lead.email],
                        fail_silently=False,
                    )
                    logger.info(f"Confirmation email sent to {lead.email}")
                except Exception as e:
                    # Log error but don't stop the process
                    logger.error(
                        f"Failed to send confirmation email: {str(e)}", exc_info=True
                    )

                # Redirect to thank you page
                return redirect("services:thank_you")

            except Exception as e:
                logger.error(f"Failed to create lead: {str(e)}", exc_info=True)
                messages.error(
                    request,
                    "Sorry, there was an error processing your request. Please try again later.",
                )
                return redirect(next_url)
        else:
            # Return form with errors
            return render(
                request,
                "services/contact_form.html",
                {
                    "form": form,
                    "source": request.POST.get("source", ""),
                    "details": request.POST.get("details", ""),
                },
            )

    return redirect(
        "services:homepage"
    )  # Redirect if someone tries to access the URL directly
