"""
CSS template generation for fontpls.
"""


def create_stylesheet(font_metadata):
    """
    Create a CSS stylesheet with @font-face declarations for all fonts.

    Args:
        font_metadata (dict): Dictionary of font metadata

    Returns:
        str: CSS stylesheet content
    """
    stylesheet = "/* Font definitions generated by fontpls */\n\n"

    for url, metadata in font_metadata.items():
        family = metadata["family"]
        style = metadata["style"]
        filename = metadata["filename"]
        format_name = metadata["format"]

        # Determine font-weight and font-style from style name
        weight = "400"
        font_style = "normal"

        # Extract weight
        style_lower = style.lower()
        if "bold" in style_lower or "black" in style_lower:
            weight = "700"
        elif "thin" in style_lower or "ultra light" in style_lower:
            weight = "100"
        elif "light" in style_lower:
            weight = "300"
        elif "medium" in style_lower:
            weight = "500"
        elif "regular" in style_lower or "normal" in style_lower:
            weight = "400"

        # Extract style
        if "italic" in style.lower() or "oblique" in style.lower():
            font_style = "italic"

        # Store weight and font_style in metadata for the demo page
        metadata["weight"] = weight
        metadata["font_style"] = font_style

        # Create @font-face declaration
        stylesheet += f"@font-face {{\n"
        stylesheet += f"  font-family: '{family}';\n"
        stylesheet += f"  src: url('{filename}') format('{format_name}');\n"
        stylesheet += f"  font-weight: {weight};\n"
        stylesheet += f"  font-style: {font_style};\n"
        stylesheet += "}\n\n"

    return stylesheet
