import jinja2
import os
from typing import Dict


def render_template(template_name: str, context: Dict) -> str:
    """Renders an email template with the given context.

    Args:
        template_name: The name of the template file (e.g., 'scale_up.txt').
        context: A dictionary containing the variables to be used in the template.

    Returns:
        The rendered email content as a string.
    """
    template_loader = jinja2.FileSystemLoader(searchpath="templates/email")
    template_env = jinja2.Environment(loader=template_loader)
    template = template_env.get_template(template_name)
    return template.render(context)


def generate_scale_up_email(event_time: str, resource: str, new_allocation: str) -> str:
    """Generates the email content for a scale-up event.

    Args:
        event_time: The time of the scale-up event.
        resource: The resource that was scaled up.
        new_allocation: The new allocation of the resource.

    Returns:
        The email content as a string.
    """
    context = {
        "event_time": event_time,
        "resource": resource,
        "new_allocation": new_allocation,
    }
    return render_template("scale_up.txt", context)


def generate_scale_down_email(event_time: str, resource: str, new_allocation: str) -> str:
    """Generates the email content for a scale-down event.

    Args:
        event_time: The time of the scale-down event.
        resource: The resource that was scaled down.
        new_allocation: The new allocation of the resource.

    Returns:
        The email content as a string.
    """
    context = {
        "event_time": event_time,
        "resource": resource,
        "new_allocation": new_allocation,
    }
    return render_template("scale_down.txt", context)


if __name__ == '__main__':
    # Example usage
    scale_up_email = generate_scale_up_email(
        event_time="2024-10-27 10:00:00", resource="CPU", new_allocation="20 cores"
    )
    print("Scale-Up Email:\n", scale_up_email)

    scale_down_email = generate_scale_down_email(
        event_time="2024-10-27 11:00:00", resource="Memory", new_allocation="16 GB"
    )
    print("\nScale-Down Email:\n", scale_down_email)
