from django.shortcuts import render,redirect,HttpResponse
from django.core.mail import send_mail
from django.conf import settings
from django.template.loader import render_to_string
from django.utils.html import strip_tags
from django.conf import settings
from django.urls import reverse
from django.contrib.sites.shortcuts import get_current_site
from . forms import BaseUserForm
from django.contrib import messages
from django.contrib.auth.tokens import default_token_generator
# Create your views here.

from .models import BaseUser

def homeView(request):
    return HttpResponse("home")

def createUserView(request):
    if request.method == 'POST':
        form = BaseUserForm(request.POST, request.FILES)
        print(form)
        if form.is_valid():
            form.save()
            form.is_a
            #return redirect('home')
            return HttpResponse("<h1>User Created</h1>")
    else:
        form = BaseUserForm()
    return render(request, 'main/create_user.html', {
        'form': form
    })


def send_mail_page():
        address = "sajeshmaan@gmail.com"
        subject = "Congrts Team Flash"
        message = "Congrts Team Flash hackx committe has announced your team as a winner"
        try:
            send_mail(subject, message, settings.EMAIL_HOST_USER, [address,'rajakiaryal0@gmail.com'])
            result= 'Email sent successfully'
        except Exception as e:
            result = f'Error sending email: {e}'


def send_welcome_email(user, request):
    # Generate activation link
    user = BaseUser.objects.get(email="user@example.com")  # Replace with your user object
    token = default_token_generator.make_token(user)
    token = 'generated_token_here'  # This token will be generated based on your activation logic
    activation_link = f"{get_current_site(request).domain}{reverse('account:activate', args=[token])}"
    
    # Prepare email subject and message
    subject = "Welcome to Sahara - Please Activate Your Account"
    
    # Render the HTML email
    html_message = render_to_string('welcome_email.html', {
        'user': user,
        'activation_link': activation_link,
    })
    
    # Render the plain-text version of the email
    plain_message = strip_tags(html_message)
    
    # Send the email
    send_mail(
        subject,
        plain_message,
        settings.DEFAULT_FROM_EMAIL,
        [user.email],
        html_message=html_message,
    )




def activate_account(request, token):
    try:
        # Decode token and retrieve user
        user = User.objects.get(email__iexact=user_email_from_token)
        if user:
            # Activate user account
            user.is_active = True
            user.save()
            messages.success(request, "Account activated successfully!")
            return redirect('login')  # Redirect to the login page
        else:
            messages.error(request, "Invalid activation link.")
    except Exception as e:
        messages.error(request, str(e))
    return redirect('home')  # Redirect to homepage in case of error
