"""
MakLinkApp – backend/config/settings.py
Flask app configuration + email helper
"""
import os
import smtplib
from email.mime.text import MIMEText
from dotenv import load_dotenv

load_dotenv()

# Database
DB_URI     = os.getenv('DB_URI', f"mysql+pymysql://{os.getenv('DB_USER','maklinkapp_user')}:{os.getenv('DB_PASS','')}@{os.getenv('DB_HOST','db')}:{os.getenv('DB_PORT','3306')}/{os.getenv('DB_NAME','maklinkapp')}")
JWT_SECRET = os.getenv('JWT_SECRET', 'change_in_production')
REDIS_URL  = os.getenv('REDIS_URL', 'redis://localhost:6379/0')

# Mail
MAIL_HOST = os.getenv('MAIL_HOST', 'mail.tfsbd.com')
MAIL_PORT = int(os.getenv('MAIL_PORT', 587))
MAIL_USER = os.getenv('MAIL_USER', 'noreply@tfsbd.com')
MAIL_PASS = os.getenv('MAIL_PASS', '')
MAIL_FROM = f"MakLinkApp – TFSBD <{MAIL_USER}>"


def sendOTPEmail(to_email: str, otp: int) -> bool:
    """Send OTP verification email."""
    subject = f"Your MakLinkApp verification code: {otp}"
    body    = f"""
Dear TFSBD Platform User,

Your one-time verification code is:

    {otp}

This code expires in 5 minutes. Do not share it with anyone.

— MakLinkApp Security Team
  Trade Finance Solution Bangladesh (TFSBD)
  maklinkapp.tfsbd.com
"""
    try:
        msg = MIMEText(body)
        msg['Subject'] = subject
        msg['From']    = MAIL_FROM
        msg['To']      = to_email
        with smtplib.SMTP(MAIL_HOST, MAIL_PORT) as server:
            server.starttls()
            server.login(MAIL_USER, MAIL_PASS)
            server.sendmail(MAIL_USER, [to_email], msg.as_string())
        return True
    except Exception as e:
        print(f"[EMAIL ERROR] {e}")
        return False
