from .database import db


def eligible_symbols(required_leverage: int) -> list[dict]:
    return db.fetch_all(
        """
        SELECT * FROM symbols
        WHERE max_leverage >= %s
          AND (state IS NULL OR LOWER(state) IN ('live','active','trading'))
        ORDER BY symbol
        """,
        (required_leverage,),
    )


def all_symbols_for_slots(slots: list[dict]) -> list[str]:
    if not slots:
        return []
    max_required = min(int(slot["leverage"]) for slot in slots)
    rows = eligible_symbols(max_required)
    return [row["symbol"] for row in rows]

