import os
import logging
from utils.email import send_gmail_with_attachment


class WelcomeMessage:
    """
    Sends a welcome email with an optional attached user manual (PDF).

    - Looks for the manual in SERVER_ASSETS_DIR (env) or defaults to ../assets next to this file.
    - Manual filename can be customized with WELCOME_MANUAL_FILENAME (env), default: manual.pdf.
    """

    def __init__(self, name: str, email: str):
        self.name = name
        self.email = email
        self.subject = "Welcome to Order Go - TMS - Your Account is Ready"

        # Allow overriding assets folder and manual filename via env for containerized deployments.
        default_assets = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "assets"))
        self.assets_folder = os.getenv("SERVER_ASSETS_DIR", default_assets)
        self.manual_filename = os.getenv("WELCOME_MANUAL_FILENAME", "Manual.pdf")
        self.manual = os.path.join(self.assets_folder, self.manual_filename)

        logging.info("WelcomeMessage assets dir: %s", self.assets_folder)
        logging.info("WelcomeMessage manual path: %s", self.manual)

        self.body = f"""

    Dear {self.name},

    We are pleased to welcome you to Order Go - TMS. Thank you for choosing our platform to support your business needs.

    To assist you in getting started, we have attached the User Manual to this email. It provides step-by-step instructions on account setup, feature overview, and best practices for using Order Go - TMS efficiently.

    We recommend reviewing the manual at your convenience to become familiar with the system's capabilities. Should you require any further assistance, our support team is available at support@ordergotms.com.

    We look forward to supporting your success and building a long-term partnership.

    Sincerely,
    The Order Go - TMS Team

    """

    def send_email(self):
        # If the manual exists, send with attachment; otherwise, log and send nothing (or integrate a no-attachment sender later).
        if os.path.isfile(self.manual):
            send_gmail_with_attachment(
                to_email=self.email,
                subject=self.subject,
                body=self.body,
                attachment_path=self.manual,
            )
        else:
            logging.warning(
                f"Welcome manual missing, skipping attachment. Looked at: {self.manual} "
                f"(set SERVER_ASSETS_DIR or WELCOME_MANUAL_FILENAME to adjust). "
                f"Current working directory: {os.getcwd()}"
            )
            # If you later implement send_gmail() without attachment, call it here.
            # from utils.email import send_gmail
            # send_gmail(to_email=self.email, subject=self.subject, body=self.body)
