from typing import TYPE_CHECKING

from aws_lambda_powertools import Logger
from layercake.email_ import Message
from layercake.strutils import first_word, truncate_str

from . import (
    reminder_access_period_before_30_days,
    reminder_cert_expiration_before_30_days,
    reminder_cert_expired,
    reminder_no_access_after_3_days,
    reminder_no_activity_after_7_days,
)

if TYPE_CHECKING:
    from mypy_boto3_sesv2 import SESV2Client
else:
    SESV2Client = object


TEMPLATES = {
    'reminder_access_period_before_30_days': {
        'subject': reminder_access_period_before_30_days.SUBJECT,
        'message': reminder_access_period_before_30_days.MESSAGE,
    },
    'reminder_cert_expiration_before_30_days': {
        'subject': reminder_cert_expiration_before_30_days.SUBJECT,
        'message': reminder_cert_expiration_before_30_days.MESSAGE,
    },
    'reminder_cert_expired': {
        'subject': reminder_cert_expired.SUBJECT,
        'message': reminder_cert_expired.MESSAGE,
    },
    'reminder_no_access_after_3_days': {
        'subject': reminder_no_access_after_3_days.SUBJECT,
        'message': reminder_no_access_after_3_days.MESSAGE,
    },
    'reminder_no_activity_after_7_days': {
        'subject': reminder_no_activity_after_7_days.SUBJECT,
        'message': reminder_no_activity_after_7_days.MESSAGE,
    },
}

logger = Logger(__name__)


def send_email(
    to: tuple[str, str],
    subject: str,
    message: str,
    context: dict = {},
    *,
    sender: tuple[str, str],
    sesv2_client: SESV2Client,
) -> None:
    name, _ = to
    emailmsg = Message(
        from_=sender,
        to=to,
        subject=subject.format(
            course=truncate_str(context['course']),
        ),
    )
    emailmsg.add_alternative(
        message.format(
            first_name=first_word(name),
            course=context['course'],
        )
    )

    sesv2_client.send_email(
        Content={
            'Raw': {
                'Data': emailmsg.as_bytes(),
            },
        }
    )
