import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import os
import markdown

def send_premium_test_email():
    # SMTP Settings
    smtp_host = "smtp.qrmaster.net"
    smtp_port = 465
    smtp_user = "timo@qrmaster.net"
    smtp_pass = "fiesta"
    
    # Recipient
    to_email = "knuth.timo@gmail.com"
    
    # Article File Path
    article_path = r"c:\Users\a931627\Documents\QRMASTER\articles\seosandwitch-qr-codes-offline-attribution.md"
    
    with open(article_path, "r", encoding="utf-8") as f:
        article_content = f.read()

    # Professional Markdown to HTML conversion
    # Uses 'tables' and 'fenced_code' for perfect formatting
    article_html = markdown.markdown(article_content, extensions=['tables', 'fenced_code', 'nl2br'])
    
    subject = "Solving the 'print gap' for small businesses (Draft Included)"
    
    # HTML Template - "Premium Digital Document"
    html_template = f"""
    <html>
    <head>
        <style>
            @import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&family=Lora:ital,wght@0,400;0,700;1,400&display=swap');
            
            body {{
                font-family: 'Inter', system-ui, -apple-system, sans-serif;
                line-height: 1.6;
                color: #2D3748;
                background-color: #F7FAFC;
                padding: 40px 20px;
                margin: 0;
            }}
            .container {{
                background-color: #ffffff;
                max-width: 760px;
                margin: 0 auto;
                padding: 60px;
                border: 1px solid #E2E8F0;
                border-radius: 4px;
                box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.05);
            }}
            .pitch-section {{
                margin-bottom: 50px;
                font-size: 16px;
                border-bottom: 2px solid #EDF2F7;
                padding-bottom: 40px;
            }}
            .draft-metadata {{
                display: flex;
                flex-wrap: wrap;
                gap: 20px;
                margin-bottom: 30px;
                padding: 15px;
                background: #F8FAFC;
                border: 1px solid #E2E8F0;
                font-size: 13px;
                color: #64748B;
                border-radius: 4px;
            }}
            .article-content {{
                font-family: 'Lora', Georgia, 'Times New Roman', serif;
                font-size: 18px;
                color: #1A202C;
                line-height: 1.7;
            }}
            
            /* Professional Markdown Overrides */
            .article-content h1 {{ font-family: 'Inter', sans-serif; font-size: 32px; font-weight: 600; margin-top: 0; }}
            .article-content h2 {{ font-family: 'Inter', sans-serif; font-size: 24px; font-weight: 600; margin-top: 40px; border-bottom: 1px solid #E2E8F0; padding-bottom: 8px; }}
            .article-content h3 {{ font-family: 'Inter', sans-serif; font-size: 20px; font-weight: 600; margin-top: 30px; }}
            
            .article-content table {{
                width: 100%;
                border-collapse: collapse;
                margin: 30px 0;
                font-size: 15px;
                font-family: 'Inter', sans-serif;
            }}
            .article-content th {{
                background-color: #F1F5F9;
                text-align: left;
                padding: 12px;
                border: 1px solid #CBD5E1;
                font-weight: 600;
            }}
            .article-content td {{
                padding: 12px;
                border: 1px solid #E2E8F0;
            }}
            .article-content tr:nth-child(even) {{ background-color: #F8FAFC; }}
            
            .article-content blockquote {{
                border-left: 4px solid #3182CE;
                margin: 30px 0;
                padding: 10px 20px;
                background: #EBF8FF;
                font-style: italic;
            }}
            
            .article-content pre {{
                background: #1A202C;
                color: #E2E8F0;
                padding: 20px;
                border-radius: 6px;
                overflow-x: auto;
                font-size: 14px;
                line-height: 1.5;
            }}
            
            .footer-note {{
                margin-top: 60px;
                padding-top: 20px;
                border-top: 1px solid #E2E8F0;
                font-size: 13px;
                color: #94A3B8;
                text-align: center;
            }}
        </style>
    </head>
    <body>
        <div class="container">
            <div class="pitch-section">
                Hi Timo,<br><br>
                I noticed your list of free resources for small businesses is one of the more practical ones out there. One category that's often missing: <b>QR code generators</b>.<br><br>
                Instead of just sending a link, I've drafted a comprehensive guide on bridging the 'print gap' using dynamic indicators. I'd love to see this featured on <i>SEO Sandwitch</i> if it aligns with your upcoming content calendar.<br><br>
                The full draft preview is below.<br><br>
                Best,<br>
                <b>Timo</b><br>
                (Writer & Strategist)
            </div>
            
            <div class="draft-metadata">
                <div><b>STATUS:</b> Finished Draft</div>
                <div><b>FORMAT:</b> Case Study / Guide</div>
                <div><b>LENGTH:</b> ~2,400 Words</div>
            </div>
            
            <div class="article-content">
                {article_html}
            </div>
            
            <div class="footer-note">
                This is a private preview intended for the editorial team at SEO Sandwitch.
            </div>
        </div>
    </body>
    </html>
    """

    message = MIMEMultipart()
    message["From"] = smtp_user
    message["To"] = to_email
    message["Subject"] = subject
    message.attach(MIMEText(html_template, "html"))

    try:
        print(f"Connecting to {smtp_host}:{smtp_port} (PREMIUM MODE)...")
        with smtplib.SMTP_SSL(smtp_host, smtp_port) as server:
            server.login(smtp_user, smtp_pass)
            print("Login successful. Sending premium document email...")
            server.sendmail(smtp_user, to_email, message.as_string())
            print(f"Premium email sent successfully to {to_email}!")
    except Exception as e:
        print(f"Failed to send email: {e}")

if __name__ == "__main__":
    send_premium_test_email()
