from html import escape

import resend

from app.core.config import settings


resend.api_key = settings.RESEND_API_KEY


def send_verification_email(
    to_email: str,
    verification_code: str,
) -> None:
    resend.Emails.send(
        {
            "from": settings.RESEND_FROM_EMAIL,
            "to": [to_email],
            "subject": "Verify your AAD Business App account",
            "html": f"""
                <h2>AAD Business App</h2>

                <p>Your verification code is:</p>

                <h1>{verification_code}</h1>

                <p>
                    This code expires in 10 minutes.
                </p>

                <p>
                    AAD Digital Technology
                </p>
            """,
        }
    )


def send_password_reset_email(
    to_email: str,
    reset_token: str,
) -> None:
    reset_link = (
        f"{settings.FRONTEND_URL}"
        f"/reset-password"
        f"?email={to_email}"
        f"&token={reset_token}"
    )

    resend.Emails.send(
        {
            "from": settings.RESEND_FROM_EMAIL,
            "to": [to_email],
            "subject": "Reset your AAD Business App password",
            "html": f"""
                <h2>AAD Business App</h2>

                <p>
                    We received a request to reset your password.
                </p>

                <p>
                    <a href="{reset_link}">
                        Reset your password
                    </a>
                </p>

                <p>
                    This link expires in 15 minutes.
                </p>

                <p>
                    If you did not request a password reset,
                    you can ignore this email.
                </p>

                <p>
                    AAD Digital Technology
                </p>
            """,
        }
    )


def send_platform_staff_invitation(to_email: str, invitation_token: str) -> None:
    invitation_link = f"{settings.FRONTEND_URL}/signup?platform_invite={invitation_token}"
    resend.Emails.send({
        "from": settings.RESEND_FROM_EMAIL,
        "to": [to_email],
        "subject": "You have been invited to AAD Platform Administration",
        "html": f"<h2>AAD Digital Technology</h2><p>You have been invited to the secure AAD Platform Admin Panel.</p><p><a href=\"{invitation_link}\">Accept your staff invitation</a></p><p>This invitation expires in 72 hours. Do not forward this link.</p>",
    })


def send_debt_reminder_email(
    to_email: str,
    customer_name: str,
    business_name: str,
    amount_outstanding,
    due_date=None,
    note: str | None = None,
):
    amount = f"{float(amount_outstanding):,.2f}"

    safe_customer = escape(customer_name)
    safe_business = escape(business_name)
    safe_note = (
        escape(note).replace("\n", "<br>")
        if note
        else ""
    )

    due_text = (
        f"<p>Due date: <strong>{escape(str(due_date))}</strong></p>"
        if due_date
        else ""
    )

    note_text = (
        f"<p>{safe_note}</p>"
        if safe_note
        else ""
    )

    return resend.Emails.send(
        {
            "from": settings.RESEND_FROM_EMAIL,
            "to": [to_email],
            "subject": f"Payment reminder from {business_name}",
            "html": f"""
                <h2>{safe_business}</h2>

                <p>Hello {safe_customer},</p>

                <p>
                    This is a payment reminder regarding an
                    outstanding balance of
                    <strong>NGN {amount}</strong>.
                </p>

                {due_text}
                {note_text}

                <p>
                    If you have already made payment,
                    please contact the business so the
                    account can be updated.
                </p>

                <p>
                    Sent through AAD Business App
                </p>
            """,
        }
    )
