from django.shortcuts import render, redirect
from . import forms
from django.core.mail import send_mail
from django.contrib import messages
from django.urls import reverse
from .models import *

# Create your views here.
def index(request):
    context = {
        'title': 'Home',
        'description': 'Paragon Dynamics delivers expert project management, program delivery, and business consultancy with a results-driven, global approach. Paragon Pixels offers custom web design and development built for performance, flexibility, and seamless user experiences.',
        'content': LandingContent.objects.all(),
    }

    return render(request, 'info/index.html', context)

def about(request):
    context = {
        'title': 'About',
        'description': 'Paragon Services brings together Paragon Dynamics and Paragon Pixels—two expert-led divisions delivering business consultancy and tailored digital solutions. With strengths in project and program management, strategic execution, and end-to-end web development, we offer integrated services built to adapt, scale, and drive lasting results.',
        'content': AboutContent.objects.all(),
        'clients': Clients.objects.all().order_by('-id'),
        'why': LandingContent.objects.values('why_wait'),
    }

    return render(request, 'info/about.html', context)

def paragon_dynamics(request):
    context = {
        'title': 'Dynamics',
        'description': 'Paragon Dynamics delivers tailored project management, program delivery, and strategic consultancy services built for tech-driven and fast-paced environments. From startup MVPs to complex SaaS ecosystems, we provide custom-fit solutions that blend Agile, Waterfall, and Hybrid methodologies to drive outcomes, not just output. Our hands-on, embedded approach ensures clear alignment with your business goals—whether you’re managing a single project, scaling a multi-stream program, or navigating strategic change. With deep industry expertise, flexible frameworks, and a focus on practical results, Paragon Dynamics helps organisations plan smarter, deliver faster, and grow with confidence.',
        'content': DynamicsContent.objects.all(),
        'services': DynamicsSections.objects.all(),
        'choose': WhyChoose.objects.all().filter(dynamics=True),
        'dyn_form': forms.DirectForm(),
        'd_form': forms.DynamicsForm(),
        'i_form': forms.InfoForm(),
    }

    if request.method == 'POST':
        dyn_form = forms.DirectForm(request.POST)
        d_form = forms.DynamicsForm(request.POST)
        i_form = forms.InfoForm(request.POST)

        if dyn_form.is_valid() and d_form.is_valid() and i_form.is_valid():
            name = dyn_form.cleaned_data['name']
            surname = dyn_form.cleaned_data['surname']
            email = dyn_form.cleaned_data['email']
            d_selected = d_form.cleaned_data['choices']
            i_message = i_form.cleaned_data['message']
            message = f'Name: {name}\nSurname: {surname}\nEmail: {email}\nSelected Services: {d_selected}\nMessage:\n{i_message}'
            
            if not Newsletter.objects.filter(email=email).exists():
                Newsletter.objects.create(
                    name=name,
                    surname=surname,
                    email=email
                )

        else:
            messages.warning(request, 'Something went wrong, please try again!')
            return render(request, 'info/p_dynamics.html', context)
        
        send_mail(
            subject = f'Contact request: Paragon Dynamics',
            message = message,
            from_email = 'noreply@paragonmail.online',
            recipient_list = ['info@paragonmail.online'],
        )
        
        messages.success(request, 'Request sent succesfully!')
        return redirect(reverse('info:p_dynamics'))
    
    else:
        return render(request, 'info/p_dynamics.html', context)

def paragon_pixels(request):
    context = {
        'title': 'Pixels',
        'description': 'Paragon Pixels provides custom design, development, and deployment services for websites and web applications. We deliver fully tailored digital solutions—crafted from the ground up to meet your specific business goals, user needs, and technical requirements. From responsive, user-first UI/UX design to scalable Django-based development and secure, performance-optimized deployment, every project is built with flexibility, functionality, and future growth in mind. Whether launching a new platform or evolving an existing one, Paragon Pixels ensures your digital presence is intuitive, robust, and ready for what’s next.',
        'content': PixelsContent.objects.all(),
        'choose': WhyChoose.objects.all().filter(pixels=True),
        'pix_form': forms.DirectForm(),
        'p_form': forms.PixelsForm(),
        'i_form': forms.InfoForm(),
    }

    if request.method == 'POST':
        pix_form = forms.DirectForm(request.POST)
        p_form = forms.PixelsForm(request.POST)
        i_form = forms.InfoForm(request.POST)

        if pix_form.is_valid() and p_form.is_valid() and i_form.is_valid():
            name = pix_form.cleaned_data['name']
            surname = pix_form.cleaned_data['surname']
            email = pix_form.cleaned_data['email']
            d_selected = p_form.cleaned_data['choices']
            i_message = i_form.cleaned_data['message']
            message = f'Name: {name}\nSurname: {surname}\nEmail: {email}\nSelected Services: {d_selected}\nMessage:\n{i_message}'
            
            if not Newsletter.objects.filter(email=email).exists():
                Newsletter.objects.create(
                    name=name,
                    surname=surname,
                    email=email
                )
        else:
            messages.warning(request, 'Something went wrong, please try again!')
            return render(request, 'info/p_pixels.html', context)
        
        send_mail(
            subject = f'Contact request: Paragon Pixels',
            message = message,
            from_email = 'noreply@paragonmail.online',
            recipient_list = ['info@paragonmail.online'],
        )
        
        messages.success(request, 'Request sent succesfully!')
        return redirect(reverse('info:p_pixels'))
    
    else:
        return render(request, 'info/p_pixels.html', context)

def contact(request):
    context = {
        'title': 'Contact',
        'description': 'Get in touch with Paragon Services for expert business consultancy, project execution, or custom digital solutions. Our responsive team is ready to help you achieve your goals with strategic insight and tailored, results-driven execution.',
        'content': ContactContent.objects.all(),
        'form': forms.ContactForm(),
        'i_form': forms.InfoForm(),
        'd_form': forms.DynamicsForm(),
        'p_form': forms.PixelsForm(),
    }

    if request.method == 'POST':
        form = forms.ContactForm(request.POST)
        i_form = forms.InfoForm(request.POST)
        d_form = forms.DynamicsForm(request.POST)
        p_form = forms.PixelsForm(request.POST)
    
        print(request.POST)
        if form.is_valid():
            name = form.cleaned_data['name']
            surname = form.cleaned_data['surname']
            email = form.cleaned_data['email']
            selected = form.cleaned_data['service']
            message = f'Name: {name}\nSurname: {surname}\nEmail: {email}\n'
            
            if not Newsletter.objects.filter(email=email).exists():
                Newsletter.objects.create(
                    name=name,
                    surname=surname,
                    email=email
                )

            if i_form.is_valid():
                i_message = i_form.cleaned_data['message']
                message += f'Request Message:\n{i_message}\n'

            if d_form.is_valid():
                d_selected = d_form.cleaned_data['choices']
                message += f'\nSelected Catagories: {d_selected}\n'

            if p_form.is_valid():
                p_selected = p_form.cleaned_data['choices']
                message += f'\nSelected Catagories: {p_selected}\n'
        else:
            messages.warning(request, 'Something went wrong, please try again!')
            return render(request, 'info/contact.html', context)
        
        # Send cleaned data via email
        send_mail(
            subject = f'Contact request: {selected}',
            message = message,
            from_email = 'noreply@paragonmail.online',
            recipient_list = ['info@paragonmail.online'],
        )
        
        messages.success(request, 'Request sent succesfully!')
        return redirect(reverse('info:contact'))
    
    else:
        return render(request, 'info/contact.html', context)
