"""
=========================================
CV6 AI Trading OS
Portfolio Engine
=========================================
"""


class PortfolioEngine:

    def calculate(
        self,
        capital: float,
        used_margin: float,
        pnl: float,
        open_positions: int
    ):

        available_capital = capital - used_margin

        margin_percent = 0

        if capital > 0:
            margin_percent = (used_margin / capital) * 100

        if margin_percent < 40:
            health = "Excellent"

        elif margin_percent < 70:
            health = "Good"

        elif margin_percent < 90:
            health = "Warning"

        else:
            health = "Danger"

        return {
            "available_capital": round(available_capital, 2),
            "margin_used_percent": round(margin_percent, 2),
            "portfolio_health": health
        }