# expat_life/email_services.py

from expat_life.email_utils import send_html_email
from django.urls import reverse
from django.contrib.sites.shortcuts import get_current_site

def send_foreigner_password_reset_code_email(user, reset_code, request=None, expiry_minutes=15):
    """
    Sends password reset verification code to Guest / Foreigner user.
    """

    site_url = ""

    if request:
        site_url = request.build_absolute_uri("/")

    return send_html_email(
        subject="Your Live in Sri Lanka Password Reset Code",
        template_name="emails/expat_life/password_reset_code.html",
        context={
            "user": user,
            "reset_code": reset_code,
            "expiry_minutes": expiry_minutes,
            "site_url": site_url,
        },
        to_email=user.email,
    )

def send_guest_signup_welcome_email(
    user,
    full_name,
    email,
    username,
    temporary_password,
    request=None,
):
    site_url = ""
    dashboard_url = ""

    if request:
        site_url = request.build_absolute_uri("/")
        dashboard_url = request.build_absolute_uri(
            reverse("expat_life:foreigner_dashboard")
        )

    return send_html_email(
        subject="Welcome to Live in Sri Lanka - Your Guest Account Details",
        template_name="emails/expat_life/guest_signup_welcome.html",
        context={
            "user": user,
            "full_name": full_name,
            "email": email,
            "username": username,
            "temporary_password": temporary_password,
            "account_role": "Guest",
            "site_url": site_url,
            "dashboard_url": dashboard_url,
        },
        to_email=email,
        from_email="Live in Sri Lanka <dsjayathunge@gmail.com>",
    )

def send_guest_dating_interest_notification_email(
    foreigner,
    dating_partner,
    interest,
    request=None,
):
    """
    Notify the foreign guest when a local dating partner expresses interest
    in their foreigner profile.
    """

    guest_user = foreigner.user
    guest_email = guest_user.email

    if not guest_email:
        return False

    site_url = ""
    dashboard_url = ""

    if request:
        site_url = request.build_absolute_uri("/")
        dashboard_url = request.build_absolute_uri(
            reverse("expat_life:foreigner_dashboard")
        )

    guest_name = (
        getattr(foreigner, "full_name", None)
        or guest_user.get_full_name()
        or guest_user.get_username()
    )

    partner_user = dating_partner.user

    partner_name = (
        getattr(dating_partner, "full_name", None)
        or partner_user.get_full_name()
        or partner_user.get_username()
    )

    return send_html_email(
        subject="New Dating Interest Received - Live in Sri Lanka",
        template_name="emails/expat_life/guest_dating_interest_notification.html",
        context={
            "foreigner": foreigner,
            "guest_user": guest_user,
            "guest_name": guest_name,
            "dating_partner": dating_partner,
            "partner_name": partner_name,
            "interest": interest,
            "site_url": site_url,
            "dashboard_url": dashboard_url,
        },
        to_email=guest_email,
        from_email="Live in Sri Lanka <dsjayathunge@gmail.com>",
    )

def send_foreigner_payment_invoice_email(
    payment_transaction,
    request=None,
):
    """
    Sends payment confirmation / invoice email for Guest payments.
    Prevents duplicate invoice emails using payment_transaction.invoice_email_sent.
    """

    if not payment_transaction:
        return False

    if payment_transaction.invoice_email_sent:
        return False

    user = payment_transaction.user

    if not user or not user.email:
        return False

    guest_name = (
        user.get_full_name()
        or getattr(user, "username", "")
        or user.email
    )

    payment_center_url = ""
    dashboard_url = ""

    if request:
        payment_center_url = request.build_absolute_uri(
            reverse("expat_life:foreigner_payment_center")
        )
        dashboard_url = request.build_absolute_uri(
            reverse("expat_life:foreigner_dashboard")
        )

    items = payment_transaction.items.select_related("payable_item").all()

    email_sent = send_html_email(
        subject=f"Payment Confirmation - Transaction #{payment_transaction.id}",
        template_name="emails/expat_life/foreigner_payment_invoice.html",
        context={
            "payment_transaction": payment_transaction,
            "items": items,
            "user": user,
            "guest_name": guest_name,
            "payment_center_url": payment_center_url,
            "dashboard_url": dashboard_url,
            "company_name": "Live in Sri Lanka",
            "support_email": "dsjayathunge@gmail.com",
        },
        to_email=user.email,
        from_email="Live in Sri Lanka <dsjayathunge@gmail.com>",
    )

    if email_sent:
        payment_transaction.invoice_email_sent = True
        payment_transaction.save(update_fields=[
            "invoice_email_sent",
            "updated_at",
        ])

    return email_sent


def send_rental_owner_signup_welcome_email(
    user,
    full_name,
    email,
    username,
    temporary_password,
    request=None,
):
    site_url = ""
    dashboard_url = ""

    if request:
        site_url = request.build_absolute_uri("/")
        dashboard_url = request.build_absolute_uri(
            reverse("rental_app:rental_owner_dashboard")
        )

    return send_html_email(
        subject="Welcome to Rent in Sri Lanka - Your Rental Owner Account Details",
        template_name="emails/rental_app/rental_owner_signup_welcome.html",
        context={
            "user": user,
            "full_name": full_name,
            "email": email,
            "username": username,
            "temporary_password": temporary_password,
            "account_role": "Rental Owner",
            "site_url": site_url,
            "dashboard_url": dashboard_url,
        },
        to_email=email,
        from_email="Rent in Sri Lanka <dsjayathunge@gmail.com>",
    )
#========================================
#Dating app

def send_dating_partner_signup_welcome_email(
    user,
    full_name,
    email,
    username,
    temporary_password,
    request=None,
):
    site_url = ""
    dashboard_url = ""

    if request:
        site_url = request.build_absolute_uri("/")
        dashboard_url = request.build_absolute_uri(
            reverse("dating_app:dating_partner_dashboard")
        )

    return send_html_email(
        subject="Welcome to Date in Sri Lanka - Your Dating Partner Account Details",
        template_name="emails/dating_app/dating_partner_signup_welcome.html",
        context={
            "user": user,
            "full_name": full_name,
            "email": email,
            "username": username,
            "temporary_password": temporary_password,
            "account_role": "Dating Partner",
            "site_url": site_url,
            "dashboard_url": dashboard_url,
        },
        to_email=email,
        from_email="Date in Sri Lanka <dsjayathunge@gmail.com>",
    )

def send_dating_partner_interest_notification_email(
    partner,
    foreigner_profile,
    interest,
    request=None,
):
    """
    Notify the local dating partner when a foreigner expresses interest
    in their dating profile.
    """

    partner_user = partner.user
    partner_email = partner_user.email

    if not partner_email:
        return False

    site_url = ""
    dashboard_url = ""

    if request:
        site_url = request.build_absolute_uri("/")
        dashboard_url = request.build_absolute_uri(
            reverse("dating_app:dating_partner_dashboard")
        )

    partner_name = getattr(partner, "full_name", None) or partner_user.get_username()

    foreigner_user = foreigner_profile.user
    foreigner_name = (
        getattr(foreigner_profile, "full_name", None)
        or foreigner_user.get_full_name()
        or foreigner_user.get_username()
    )

    return send_html_email(
        subject="New Interest Received - Date in Sri Lanka",
        template_name="emails/dating_app/dating_partner_interest_notification.html",
        context={
            "partner": partner,
            "partner_user": partner_user,
            "partner_name": partner_name,
            "foreigner_profile": foreigner_profile,
            "foreigner_name": foreigner_name,
            "interest": interest,
            "site_url": site_url,
            "dashboard_url": dashboard_url,
        },
        to_email=partner_email,
        from_email="Date in Sri Lanka <dsjayathunge@gmail.com>",
    )

def send_service_partner_signup_welcome_email(
    user,
    full_name,
    email,
    username,
    temporary_password,
    request=None,
):
    site_url = ""
    dashboard_url = ""

    if request:
        site_url = request.build_absolute_uri("/")
        dashboard_url = request.build_absolute_uri(
            reverse("service_partner_app:service_partner_dashboard")
        )

    return send_html_email(
        subject="Welcome to Hired in Sri Lanka - Your Service Partner Account Details",
        template_name="emails/service_partner_app/service_partner_signup_welcome.html",
        context={
            "user": user,
            "full_name": full_name,
            "email": email,
            "username": username,
            "temporary_password": temporary_password,
            "account_role": "Service Partner",
            "site_url": site_url,
            "dashboard_url": dashboard_url,
        },
        to_email=email,
        from_email="Hired in Sri Lanka <dsjayathunge@gmail.com>",
    )

def send_affiliate_signup_welcome_email(
    user,
    full_name,
    email,
    username,
    temporary_password,
    request=None,
):
    """
    Send welcome email to newly registered affiliate partner.
    """
    site_url = ""
    dashboard_url = ""

    if request:
        # Get current site domain from request or Django sites framework
        try:
            current_site = get_current_site(request)
            site_url = f"https://{current_site.domain}/" if request.is_secure() else f"http://{current_site.domain}/"
        except:
            site_url = request.build_absolute_uri("/")

        dashboard_url = request.build_absolute_uri(
            reverse("affiliates_app:affiliate_dashboard")
        )

    return send_html_email(
        subject="Welcome to Partner with Ensolanka – Your Affiliate Account Details",
        template_name="emails/affiliates_app/affiliate_signup_welcome.html",
        context={
            "user": user,
            "full_name": full_name,
            "email": email,
            "username": username,
            "temporary_password": temporary_password,
            "account_role": "Affiliate Partner",
            "site_url": site_url,
            "dashboard_url": dashboard_url,
            "tagline": "Partner with Ensolanka. Grow Together. Earn Together.",
        },
        to_email=email,
        from_email="Partner with Ensolanka <dsjayathunge@gmail.com>",
    )

def send_property_manager_signup_welcome_email(
    user,
    full_name,
    email,
    username,
    temporary_password,
    request=None,
):
    site_url = ""
    dashboard_url = ""

    if request:
        site_url = request.build_absolute_uri("/")
        dashboard_url = request.build_absolute_uri(
            reverse("property_manager_app:property_manager_dashboard")
        )

    return send_html_email(
        subject="Welcome to Home in Sri Lanka - Your Property Care Account Details",
        template_name="emails/property_manager_app/property_manager_signup_welcome.html",
        context={
            "user": user,
            "full_name": full_name,
            "email": email,
            "username": username,
            "temporary_password": temporary_password,
            "account_role": "Property Care Owner",
            "site_url": site_url,
            "dashboard_url": dashboard_url,
            "tagline": "Remote Owners, Local Maintenance.",
        },
        to_email=email,
        from_email="Home in Sri Lanka <dsjayathunge@gmail.com>",
    )

def send_host_signup_welcome_email(
    user,
    contact_person_name,
    email,
    username,
    temporary_password,
    request=None,
):
    site_url = ""
    dashboard_url = ""

    if request:
        site_url = request.build_absolute_uri("/")
        dashboard_url = request.build_absolute_uri(
            reverse("hosting_app:host_dashboard")
        )

    return send_html_email(
        subject="Welcome to Host in Sri Lanka - Your Hosting Partner Account Details",
        template_name="emails/hosting_app/host_signup_welcome.html",
        context={
            "user": user,
            "full_name": contact_person_name,
            "contact_person_name": contact_person_name,
            "email": email,
            "username": username,
            "temporary_password": temporary_password,
            "account_role": "Hosting Partner",
            "site_url": site_url,
            "dashboard_url": dashboard_url,
            "tagline": "Be the place couples choose.",
        },
        to_email=email,
        from_email="Host in Sri Lanka <dsjayathunge@gmail.com>",
    )

def send_hosting_client_signup_welcome_email(
    user,
    full_name,
    email,
    username,
    temporary_password,
    venue=None,
    request=None,
):
    site_url = ""
    dashboard_url = ""
    login_url = ""
    payment_center_url = ""
    venue_url = ""

    if request:
        site_url = request.build_absolute_uri("/")
        dashboard_url = request.build_absolute_uri(
            reverse("hosting_app:hosting_client_dashboard")
        )
        login_url = request.build_absolute_uri(
            reverse("hosting_app:hosting_client_login")
        )
        payment_center_url = request.build_absolute_uri(
            reverse("hosting_app:hosting_client_payment_center")
        )

        if venue:
            venue_url = request.build_absolute_uri(
                reverse(
                    "hosting_app:local_client_booking_start",
                    kwargs={"venue_slug": venue.slug}
                )
            )

    return send_html_email(
        subject="Welcome to Host in Sri Lanka - Your Hosting Client Account Details",
        template_name="emails/hosting_app/hosting_client_signup_welcome.html",
        context={
            "user": user,
            "full_name": full_name,
            "email": email,
            "username": username,
            "temporary_password": temporary_password,
            "account_role": "Hosting Client",
            "venue": venue,
            "site_url": site_url,
            "dashboard_url": dashboard_url,
            "login_url": login_url,
            "payment_center_url": payment_center_url,
            "venue_url": venue_url,
            "tagline": "Plan beautiful moments with trusted local hosts.",
        },
        to_email=email,
        from_email="Host in Sri Lanka <dsjayathunge@gmail.com>",
    )