import sib_api_v3_sdk
from sib_api_v3_sdk.rest import ApiException
from src.core.config import settings
from jinja2 import Template
import logging
# Configure API key authorization: api-key
configuration = sib_api_v3_sdk.Configuration()
configuration.api_key['api-key'] = settings.sendinblue_api_key

def get_template(template_name: str, **kwargs) -> str:
    try:
        with open(f"templates/{template_name}.html") as file_:
            template = Template(file_.read())
        return template.render(**kwargs)
    except FileNotFoundError as e:
        logging.error(f"Template file not found: {e}")
        raise
    except Exception as e:
        logging.error(f"Error rendering template: {e}")
        raise

async def send_welcome_email(email: str):
    try:
        template = get_template('welcome', name=email.split('@')[0])
        send_smtp_email = sib_api_v3_sdk.SendSmtpEmail(
            to=[{"email": email}],
            subject="Welcome to Our Service",
            html_content=template,
            sender={"email": settings.mail_from}
        )
        api_instance = sib_api_v3_sdk.TransactionalEmailsApi(sib_api_v3_sdk.ApiClient(configuration))
        api_instance.send_transac_email(send_smtp_email)
    except ApiException as e:
        logging.error(f"Exception when sending welcome email: {e}")

async def send_reset_password_email(email: str, reset_token: str):
    try:
        template = get_template('reset_password', reset_link=f"{settings.BASE_URL}/reset-password?token={reset_token}")
        send_smtp_email = sib_api_v3_sdk.SendSmtpEmail(
            to=[{"email": email}],
            subject="Password Reset Request",
            html_content=template,
            sender={"email": settings.mail_from}
        )
        api_instance = sib_api_v3_sdk.TransactionalEmailsApi(sib_api_v3_sdk.ApiClient(configuration))
        api_instance.send_transac_email(send_smtp_email)
    except ApiException as e:
        logging.error(f"Exception when sending reset password email: {e}")



async def send_verification_email(email: str, otp: str):
    try:
        template = get_template('verify_email', otp=otp)
        send_smtp_email = sib_api_v3_sdk.SendSmtpEmail(
            to=[{"email": email}],
            subject="Verify your email",
            html_content=template,
            sender={"email": settings.mail_from}
        )
        api_instance = sib_api_v3_sdk.TransactionalEmailsApi(sib_api_v3_sdk.ApiClient(configuration))
        api_instance.send_transac_email(send_smtp_email)
    except ApiException as e:
        logging.error(f"Exception when sending verification email: {e}")


async def send_otp_email(email: str, otp: str):
    try:
        template = get_template('otp', otp=otp)
        send_smtp_email = sib_api_v3_sdk.SendSmtpEmail(
            to=[{"email": email}],
            subject="Your OTP Code",
            html_content=template,
            sender={"email": settings.mail_from}
        )
        api_instance = sib_api_v3_sdk.TransactionalEmailsApi(sib_api_v3_sdk.ApiClient(configuration))
        api_instance.send_transac_email(send_smtp_email)
    except ApiException as e:
        logging.error(f"Exception when sending OTP email: {e}")



async def send_contact_us_email(full_name: str, phone: str, email: str, message: str):
    try:
        # Render the email template with the contact us form data
        template = get_template(
            'contactus',
            full_name=full_name,
            phone=phone,
            email=email,
            message=message
        )
        send_smtp_email = sib_api_v3_sdk.SendSmtpEmail(
            to=[{"email": settings.mail_to}],  
            subject="New Contact Us Message",
            html_content=template,
            sender={"email": settings.mail_from}
        )
        api_instance = sib_api_v3_sdk.TransactionalEmailsApi(sib_api_v3_sdk.ApiClient(configuration))
        api_instance.send_transac_email(send_smtp_email)
    except ApiException as e:
        logging.error(f"Exception when sending contact us email: {e}")