from vulnsearch.models.vulnerability import Package


def to_toml_dict(pkgs: Package | list[Package]) -> dict | list[dict]:
    """Build a dictionary structure ready for TOML serialization."""
    result: list[dict] = []

    if isinstance(pkgs, Package):
        pkgs = [pkgs]

    for pkg in pkgs:
        data = pkg.model_dump(exclude_none=True)

        data["vulnerabilities"] = [
            v.model_dump(exclude_none=True) for v in pkg.report_vulnerabilities()
        ]

        result.append(data)

    return result
