from environment import Environment
from flask_mail import Message
import os
from mailersend import emails

mailer = emails.NewEmail(Environment.MAILERSEND_API_KEY)

class EmailService:

    recipients=["jacob@storyit.us"] if Environment.USE_TEST_MODE else ["branson@storyit.us", "wil@storyit.us", "conall@storyit.us"]

    @staticmethod
    def send_manual_verification_alert(mail, username, required_tagged_username, phone_number, reason="Unknown", ig_is_private="Unknown"):
        print("Sending manual verification alert")
        
        subject="New Manual Verification Required"
        sender="storyitinternalalerts"
        
        body=f"""
Hey there! A new offer has been pushed to the manual verification dashboard.

Check it out:
https://manualverification-storyit.web.app/

Details:
    Instagram username: @{username}
    Phone Number: {phone_number}
    Private: {ig_is_private}
    Required tag: @{required_tagged_username}
    Reasoning: {reason}
    
- StoryIt Manual Verification Bot
"""
        msg = Message(subject, sender=sender, recipients=EmailService.recipients)
        msg.body = body
        try:
            mail.send(msg)
            print(f"Sent new manual verification alert to {str(EmailService.recipients)}")
        except Exception as e:
            print(f"Failed to send manual verification alert: {e}")
            raise Exception(f"Failed to send manual verification alert: {e}")

    @staticmethod
    def send_manual_verification_reminder(mail, verification_email, verification_username, username, required_tagged_username):
        subject="Reminder: Manual Verification Required"
        sender="storyitinternalalerts"
        recipients=[verification_email]
        body=f"""
Hey again! It's been 5 hours since you requested to follow @{username} using the verification account @{verification_username}. This is a reminder to complete the verification.

Check it out:
https://manualverification-storyit.web.app/

Details:
    Instagram username: @{username}
    Private: True
    Required tag: @{required_tagged_username}
    

- StoryIt Manual Verification Bot
"""
        msg = Message(subject, sender=sender, recipients=recipients)
        msg.body = body
        mail.send(msg)
        print(f"Sent manual verification reminder message to {str(recipients)}")


    @staticmethod
    def send_offer_approved_alert(mail, phone_number, username, content_type, required_tagged_username, ig_is_private, client_name, specific_product, ugc_url, evaluation):
        score = evaluation["score"]
        pros = evaluation["pros"]
        cons = evaluation["cons"]

        subject=f"New Offer Approved for {client_name}"
        sender="storyitinternalalerts" 

        body=f"""
A new offer has been automatically approved for {client_name}!

See the post here: {ugc_url}

Evaluation:
    Score: {score}
    Pros: {pros}
    Cons: {cons}

Details:
    Phone number: {phone_number}
    Instagram username: @{username}
    Private: {ig_is_private}
    Required tag: @{required_tagged_username}
    Content type: {content_type}
    Specific product (in content): {specific_product}
    
- StoryIt Auto-verification

"""
        msg = Message(subject, sender=sender, recipients=EmailService.recipients)
        msg.body = body
        mail.send(msg)
        print(f"Sent new offer approved alert to {str(EmailService.recipients)}")

    @staticmethod
    def send_influencer_collab_email(client_name, ig_username,  recipient_email):
        subject = f"{client_name} <> {ig_username} Collaboration"

        body = f"""
Hi {ig_username},<br><br>
We’re reaching out because {client_name} wants to collaborate with you 🍽️📸! Click <a href="https://foodfluenceapp-554a6.web.app/login">here</a> 
to sign into foodfluence and view the collaboration details.<br><br>
You have 72 hours ⏰ to accept or decline the collaboration.<br><br>
**This is an automated message, please do not reply to this email as this inbox is not monitored.**<br><br>
Thank you,<br>
FoodFluence Team<br>
The all-in-one platform connecting restaurants to creators.<br><br>
To no longer receive collaboration requests from brands, click here.
"""
        email = {
            "from": {
                "email": "welcome@joinfoodfluence.com",
                "name": "FoodFluence"
            },
            "to": [
                {
                    "email": recipient_email,        
                }
            ],
            "subject": subject,
            "html": body,
        
        }
                    # Assuming mail.send_email is the method to send email using MailerSend
        try:
            response = mailer.send(email)
            print("Email sent successfully:", response)
        except Exception as e:
            print("Error sending email:", e)
