"""
Generate CSS for self-hosted fonts.
"""

from bengal.fonts.downloader import FontVariant


class FontCSSGenerator:
    """
    Generates @font-face CSS for downloaded fonts.
    """

    def generate(
        self,
        font_mapping: dict[str, list[FontVariant]],
        font_path_prefix: str = "/fonts",
    ) -> str:
        """
        Generate fonts.css content.

        Args:
            font_mapping: Dict of font name -> list of variants
            font_path_prefix: URL prefix for font files

        Returns:
            Complete CSS content as string
        """
        if not font_mapping:
            return ""

        css_parts = [
            "/* Auto-generated by Bengal Font Helper */",
            "/* See bengal.toml [fonts] section */",
            "",
        ]

        # Generate @font-face rules for each variant
        for _font_name, variants in font_mapping.items():
            if not variants:
                continue

            family = variants[0].family  # All variants share same family

            css_parts.append(f"/* {family} */")

            for variant in variants:
                url = f"{font_path_prefix}/{variant.filename}"

                # Determine format from file extension
                font_format = "woff2" if ".woff2" in url else "truetype"

                css_parts.append("@font-face {")
                css_parts.append(f"  font-family: '{variant.family}';")
                css_parts.append(f"  font-weight: {variant.weight};")
                css_parts.append(f"  font-style: {variant.style};")
                css_parts.append("  font-display: swap;")
                css_parts.append(f"  src: url('{url}') format('{font_format}');")
                css_parts.append("}")
                css_parts.append("")

        # Generate CSS custom properties
        css_parts.append("/* CSS Custom Properties */")
        css_parts.append(":root {")

        for font_name, variants in font_mapping.items():
            if variants:
                family = variants[0].family
                css_parts.append(f"  --font-{font_name}: '{family}';")

        css_parts.append("}")
        css_parts.append("")

        return "\n".join(css_parts)
