from __future__ import annotations

from pathlib import Path
from typing import Any
from jinja2 import Environment, FileSystemLoader, select_autoescape

from config import BASE_DIR, BRAND, SITE
from utils import save_text

TEMPLATES_DIR = BASE_DIR / 'templates'
OUTPUT_DIR = BASE_DIR / 'output'

env = Environment(
    loader=FileSystemLoader(TEMPLATES_DIR),
    autoescape=select_autoescape(['html', 'xml'])
)


def render(template_name: str, context: dict[str, Any]) -> str:
    template = env.get_template(template_name)
    base = {
        'site': SITE,
        'brand': BRAND,
    }
    base.update(context)
    return template.render(**base)


def save_page(output_path: str, html: str) -> Path:
    path = OUTPUT_DIR / output_path
    save_text(path, html)
    return path
