from fastapi import HTTPException
from fastapi_mail import ConnectionConfig, FastMail, MessageSchema
from sqlalchemy.orm import Session
from app.dependency.authantication import JWTPayloadSchema
from app.models.main.landing_page import TblLandingPage, LandingPageBase
from app.utils.schemas_utils import CustomResponse
from app.locale.messages import Messages
from app.api.landing_page import schema


class LandingPageService:
    def __init__(self, db:Session, token:JWTPayloadSchema):
        self.db = db
        self.token = token
        
    async def created(self, request:schema.LandingPageCreate):
        created_data = LandingPageBase.model_validate(request.model_dump())
        conf = ConnectionConfig(
                MAIL_USERNAME="gokulapandib@compunet.work",
                MAIL_PASSWORD="Gokul@2312024732",
                MAIL_FROM="noreply@compunet.work",
                MAIL_SERVER="smtp.stackmail.com",
                MAIL_PORT=587,
                MAIL_STARTTLS=True,
                MAIL_SSL_TLS=False,
                USE_CREDENTIALS=True
            )
        fm = FastMail(conf)
        subject = "Request for Information"
        body = f"""
        Dear Sir/Madam,

        My name is {request.name}, and I am from {request.institution_name}.

        I am writing this email regarding {request.email}.

        If you need any further information, please feel free to contact me.

        Thank you for your time and support.

        Best regards,
        {request.name}
        {request.institution_name}
        {request.email}
        """
        message = MessageSchema(
                subject=subject,
                recipients=["gokulapandib@compunet.work"],
                body=body,
                subtype="plain"
        )
        await fm.send_message(message)
        TblLandingPage.create(created_data, self.db)
        self.db.commit()
        return CustomResponse(status="1", message=Messages.LANDING_PAGE)
    
    async def geted(self, landing_id:int):
        geted_data = TblLandingPage.get(landing_id, self.db)
        if not geted_data:
            raise HTTPException(status_code=404, detail="Get group ID not found")
        return geted_data