import shutil
import smtplib

from fastapi.responses import JSONResponse

from khairo.settings import EMAIL, PASSWORD


class KhairoFullMixin:
    @staticmethod
    def Response(userMessage: object, status_code: int) -> object:
        return JSONResponse(content=userMessage, status_code=status_code)

    @staticmethod
    async def upload(fileObject, file_path):
        try:
            with open(file_path, "wb+") as file_object:
                shutil.copyfileobj(fileObject.file, file_object)
                return True
        except Exception as e:
            return False

    @staticmethod
    def mailUser(userEmail: str, emailMessage: str, emailTitle):
        ######################### setting mail server #######################
        with smtplib.SMTP_SSL('smtp.gmail.com', 465) as access360Mail:
            try:
                ######################### Authenticate account #######################
                access360Mail.login(EMAIL, PASSWORD)
                ######################### setting up mail body #######################
                # message["subject"] = emailTitle
                # message['from'] = EMAIL
                # message['to'] = userEmail
                # message.set_content(emailMessage)
                # access360Mail.send_message(message)
                message = f"subject:{emailTitle}\n\n {emailMessage}"
                access360Mail.sendmail(EMAIL, userEmail, message)
                ######################### return success message #######################
                return "message sent successfully"

            except Exception:
                ######################### return error if mail failed to send #######################
                return "Error sending message"

import shutil
import smtplib

from fastapi.responses import JSONResponse

from khairo.settings import EMAIL, PASSWORD


class KhairoFullMixin:
    @staticmethod
    def Response(userMessage: object, status_code: int) -> object:
        return JSONResponse(content=userMessage, status_code=status_code)

    @staticmethod
    async def upload(fileObject, file_path):
        try:
            with open(file_path, "wb+") as file_object:
                shutil.copyfileobj(fileObject.file, file_object)
                return True
        except Exception as e:
            return False

    @staticmethod
    def mailUser(userEmail: str, emailMessage: str, emailTitle):
        ######################### setting mail server #######################
        with smtplib.SMTP_SSL('smtp.gmail.com', 465) as access360Mail:
            try:
                ######################### Authenticate account #######################
                access360Mail.login(EMAIL, PASSWORD)
                ######################### setting up mail body #######################
                # message["subject"] = emailTitle
                # message['from'] = EMAIL
                # message['to'] = userEmail
                # message.set_content(emailMessage)
                # access360Mail.send_message(message)
                message = f"subject:{emailTitle}\n\n {emailMessage}"
                access360Mail.sendmail(EMAIL, userEmail, message)
                ######################### return success message #######################
                return "message sent successfully"

            except Exception:
                ######################### return error if mail failed to send #######################
                return "Error sending message"

######################### setting mail server ####################### ######################### Authenticate account ####################### ######################### setting up mail body ####################### # message["subject"] = emailTitle # message['from'] = EMAIL # message['to'] = userEmail # message.set_content(emailMessage) # access360Mail.send_message(message) ######################### return success message ####################### ######################### return error if mail failed to send #######################