"""Crafting recipe system."""

import logging
import tomllib
from dataclasses import dataclass
from pathlib import Path

logger = logging.getLogger(__name__)


@dataclass
class Recipe:
    """A crafting recipe definition."""

    name: str
    description: str
    ingredients: list[str]
    result: str


# Module-level registry
recipes: dict[str, Recipe] = {}


def load_recipe(path: Path) -> Recipe:
    """Load a recipe from TOML.

    Args:
        path: Path to the recipe TOML file

    Returns:
        Recipe instance
    """
    with open(path, "rb") as f:
        data = tomllib.load(f)
    return Recipe(
        name=data["name"],
        description=data["description"],
        ingredients=data["ingredients"],
        result=data["result"],
    )


def load_recipes(directory: Path) -> dict[str, Recipe]:
    """Load all recipes from a directory.

    Args:
        directory: Path to directory containing recipe TOML files

    Returns:
        Dict of recipes keyed by name
    """
    loaded: dict[str, Recipe] = {}
    for path in sorted(directory.glob("*.toml")):
        recipe = load_recipe(path)
        loaded[recipe.name] = recipe
    return loaded
