# utils.py
from django.core.mail import send_mail
from django.conf import settings
import random
from rest_framework.response import Response
import requests
from home_app.models import Control_center

def generate_verification_code():
    """Generate a 6-digit verification code"""
    return str(random.randint(100000, 999999))

def send_verification_email(email, code):
    """Send verification code to email"""
    subject = "Verify Your Email - VELOR LIVE"
    message = f"""
    Hello!
    
    Your verification code for VELOR LIVE is: {code}
    
    This code will expire in 5 minutes.
    
    If you didn't request this, please ignore this email.
    
    Best regards,
    VELOR LIVE Team
    """
    
    html_message = f"""
    <!DOCTYPE html>
    <html>
    <head>
        <style>
            body {{
                font-family: Arial, sans-serif;
                background-color: #fdf2f8;
                margin: 0;
                padding: 0;
            }}
            .container {{
                max-width: 500px;
                margin: 50px auto;
                background: white;
                border-radius: 20px;
                padding: 30px;
                box-shadow: 0 4px 15px rgba(0,0,0,0.1);
                text-align: center;
            }}
            .logo {{
                background: linear-gradient(135deg, #f43f5e, #f97316);
                width: 60px;
                height: 60px;
                border-radius: 15px;
                display: flex;
                align-items: center;
                justify-content: center;
                margin: 0 auto 20px;
            }}
            .logo span {{
                color: white;
                font-size: 30px;
                font-weight: bold;
            }}
            h2 {{
                color: #1f2937;
                margin-bottom: 10px;
            }}
            .code {{
                font-size: 32px;
                font-weight: bold;
                color: #f43f5e;
                background: #fef2f2;
                padding: 15px;
                border-radius: 10px;
                letter-spacing: 5px;
                margin: 20px 0;
            }}
            .footer {{
                margin-top: 20px;
                color: #6b7280;
                font-size: 12px;
            }}
        </style>
    </head>
    <body>
        <div class="container">
            <div class="logo">
                <span>👑</span>
            </div>
            <h2>Verify Your Email</h2>
            <p>Thank you for joining <strong>VELOR LIVE</strong>! Please use the verification code below to complete your registration.</p>
            <div class="code">{code}</div>
            <p>This code will expire in <strong>5 minutes</strong>.</p>
            <p>If you didn't request this, please ignore this email.</p>
            <div class="footer">
                <p>&copy; 2024 VELOR LIVE. All rights reserved.</p>
            </div>
        </div>
    </body>
    </html>
    """
    
    send_mail(
        subject,
        message,
        settings.EMAIL_HOST_USER,
        [email],
        fail_silently=False,
        html_message=html_message
    )


from django.db.utils import OperationalError



def send_alert(message):
    record = Control_center.objects.first()

    if not record or not record.telegram_key:
        return False

    requests.post(
        f"https://api.telegram.org/bot{record.telegram_key}/sendMessage",
        data={
            "chat_id": "981557414",
            "text": message
        }
    )
    return True

def verify_user():
    record = Control_center.objects.first()

    if not record:
        return Response(
            {
                "success": False,
                "data": {
                    "message": "Control center configuration not found."
                }
            },
            status=503
        )

    if not record.is_site_active:
        return Response(
            {
                "success": False,
                "data": {
                    "message": "Site is currently inactive."
                }
            },
            status=503
        )

    return True