# server/mail_file.py

from datetime import datetime, timezone
from flask_mail import Message
from constants_file import BASE_URL_FRONTEND
import requests

VERCEL_MAIL_URL = "https://ksmart-email.vercel.app/send-mail"



def send_mail_vercel(msg):
    payload = {
        "subject": msg.subject,
        "recipients": msg.recipients,
        "html": msg.html,
        "sender_name": "Kamfa Modern College & English Classes",
    }

    response = requests.post(
        VERCEL_MAIL_URL,
        json=payload,
    )

    return response.json()

def send_reset_email(email, link):
    msg = Message(
        'Kamfa Modern College & English Classes - Reset Your Password',
        recipients=[email]
    )
    
    current_year = datetime.now(timezone.utc).year

    msg.html = f"""
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
        <title>Password Reset</title>
        <style>
            body {{
                margin: 0;
                padding: 0;
                background-color: #f3f4f6;
                font-family: Arial, Helvetica, sans-serif;
            }}
            .container {{
                max-width: 500px;
                margin: 50px auto;
                background: #ffffff;
                padding: 30px;
                border-radius: 8px;
                box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            }}
            .header {{
                text-align: center;
                padding-bottom: 20px;
                border-bottom: 2px solid #e5e7eb;
            }}
            .header h1 {{
                color: #1e40af;
                margin: 0;
                font-size: 24px;
            }}
            .content {{
                padding: 20px 0;
            }}
            .link-box {{
                background-color: #f3f4f6;
                padding: 15px;
                border-radius: 6px;
                margin: 20px 0;
                text-align: center;
            }}
            .button {{
                display: inline-block;
                background-color: #2563eb;
                color: #ffffff;
                padding: 10px 20px;
                font-size: 14px;
                border-radius: 6px;
                text-decoration: none;
                font-weight: bold;
            }}
            .button:hover {{
                background-color: #1d4ed8;
            }}
            .footer {{
                text-align: center;
                font-size: 12px;
                color: #6b7280;
                padding-top: 20px;
                border-top: 1px solid #e5e7eb;
            }}
        </style>
    </head>
    <body>
        <div class="container">
            <div class="header">
                <h1>Kamfa Modern College & English Classes</h1>
            </div>
            
            <div class="content">
                <p>Hello,</p>
                
                <p>We received a request to reset your password for the Kamfa Modern College & English Classes.</p>
                
                <div class="link-box">
                    <a href="{link}" class="button">Reset Password</a>
                    
                    <p style="margin-top: 15px; font-size: 12px; word-break: break-all;">
                        Or copy this link: <br>
                        <a href="{link}" style="color: #2563eb;">{link}</a>
                    </p>
                </div>
                
                <p>This link will expire in 15 minutes.</p>
                
                <p>If you didn't request this, please ignore this email.</p>
                
                <p>Thanks,<br>Kamfa Modern College & English Classes Team</p>
            </div>
            
            <div class="footer">
                &copy; {current_year} Kamfa Modern College & English Classes
            </div>
        </div>
    </body>
    </html>
    """

    return msg



def send_new_user_message(email, name):
    msg = Message(
        'Welcome to Kamfa Modern College & English Classes - Your Account Has Been Created',
        recipients=[email]
    )
    
    current_year = datetime.now(timezone.utc).year
    login_link = f"{BASE_URL_FRONTEND}/login"

    msg.html = f"""
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
        <title>Welcome to Kamfa Modern College & English Classes</title>
        <style>
            body {{
                margin: 0;
                padding: 0;
                background-color: #f3f4f6;
                font-family: Arial, Helvetica, sans-serif;
            }}
            .container {{
                max-width: 500px;
                margin: 50px auto;
                background: #ffffff;
                padding: 30px;
                border-radius: 8px;
                box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            }}
            .header {{
                text-align: center;
                padding-bottom: 20px;
                border-bottom: 2px solid #e5e7eb;
            }}
            .header h1 {{
                color: #1e40af;
                margin: 0;
                font-size: 24px;
            }}
            .content {{
                padding: 20px 0;
            }}
            .credentials-box {{
                background-color: #f3f4f6;
                padding: 15px;
                border-radius: 6px;
                margin: 20px 0;
                text-align: center;
            }}
            .credentials-box p {{
                margin: 5px 0;
            }}
            .credentials-box .label {{
                font-weight: bold;
                color: #374151;
            }}
            .credentials-box .value {{
                font-family: monospace;
                font-size: 16px;
                color: #1e40af;
                font-weight: bold;
            }}
            .button {{
                display: inline-block;
                background-color: #2563eb;
                color: #ffffff;
                padding: 10px 20px;
                font-size: 14px;
                border-radius: 6px;
                text-decoration: none;
                font-weight: bold;
                margin-top: 10px;
            }}
            .button:hover {{
                background-color: #1d4ed8;
            }}
            .warning {{
                background-color: #fef3c7;
                border-left: 4px solid #f59e0b;
                padding: 12px;
                margin: 20px 0;
                border-radius: 4px;
            }}
            .warning p {{
                margin: 0;
                color: #92400e;
                font-size: 14px;
            }}
            .footer {{
                text-align: center;
                font-size: 12px;
                color: #6b7280;
                padding-top: 20px;
                border-top: 1px solid #e5e7eb;
            }}
        </style>
    </head>
    <body>
        <div class="container">
            <div class="header">
                <h1>Kamfa Modern College & English Classes</h1>
            </div>
            
            <div class="content">
                <p>Hello <strong>{name}</strong>,</p>
                
                <p>An account has been created for you in the Kamfa Modern College & English Classes.</p>
                
                <div class="credentials-box">
                    <p><span class="label">Email:</span> <span class="value">{email}</span></p>
                    <p><span class="label">Temporary Password:</span> <span class="value">123456</span></p>
                </div>
                
                <div class="warning">
                    <p><strong>⚠️ Important:</strong> You will be required to change your password after your first login.</p>
                </div>
                
                <div style="text-align: center;">
                    <a href="{login_link}" class="button">Login to Your Account</a>
                </div>
                
                <p style="margin-top: 20px;">For security reasons, please:</p>
                <ul style="color: #4b5563;">
                    <li>Change your password immediately after logging in</li>
                    <li>Do not share your password with anyone</li>
                    <li>Contact the system administrator if you have any questions</li>
                </ul>
                
                <p>Thanks,<br>Kamfa Modern College & English Classes Team</p>
            </div>
            
            <div class="footer">
                &copy; {current_year} Kamfa Modern College & English Classes
            </div>
        </div>
    </body>
    </html>
    """

    return msg