"""
AkDevz AI Market Intelligence — Quantitative Risk Engine
Calculates net risk/reward ratios after realistic fees, STT, and execution slippage.
"""
from typing import Dict, Any

class RiskEngine:
    @staticmethod
    def calculate_net_risk_reward(
        entry: float,
        stop_loss: float,
        target_1: float,
        slippage_pts: float = 0.50,
        brokerage_per_order: float = 20.0,
        lot_size: int = 25
    ) -> Dict[str, Any]:
        """
        Computes gross and net reward-to-risk after accounting for slippage and brokerage.
        """
        gross_risk_pts = abs(entry - stop_loss)
        gross_reward_pts = abs(target_1 - entry)

        if gross_risk_pts <= 0.0001:
            return {
                "is_acceptable": False,
                "gross_rr": 0.0,
                "net_rr": 0.0,
                "reason": "Invalid or zero risk distance"
            }

        gross_rr = round(gross_reward_pts / gross_risk_pts, 2)

        # Apply slippage on both entry and exit
        net_risk_pts = gross_risk_pts + (2.0 * slippage_pts)
        net_reward_pts = max(0.0, gross_reward_pts - (2.0 * slippage_pts))

        # Net cash values
        gross_risk_cash = gross_risk_pts * lot_size
        gross_reward_cash = gross_reward_pts * lot_size
        total_brokerage = brokerage_per_order * 2.0  # Buy + Sell

        net_risk_cash = (net_risk_pts * lot_size) + total_brokerage
        net_reward_cash = max(0.0, (net_reward_pts * lot_size) - total_brokerage)

        net_rr = round(net_reward_cash / net_risk_cash, 2) if net_risk_cash > 0 else 0.0
        is_acceptable = net_rr >= 1.50

        return {
            "is_acceptable": is_acceptable,
            "gross_rr": gross_rr,
            "net_rr": net_rr,
            "gross_risk_points": round(gross_risk_pts, 2),
            "gross_reward_points": round(gross_reward_pts, 2),
            "net_risk_cash": round(net_risk_cash, 2),
            "net_reward_cash": round(net_reward_cash, 2),
            "slippage_points": slippage_pts,
            "lot_size": lot_size,
        }

    @staticmethod
    def calculate_rr(
        entry_price: float,
        stop_loss: float,
        target_price: float,
        lot_size: int = 25,
        slippage_pts: float = 1.0,
        brokerage_per_order: float = 20.0
    ) -> Dict[str, Any]:
        """
        Comprehensive calculation of Reward/Risk ratio after accounting for
        Indian exchange regulatory charges: Brokerage, STT, Turnover fee, GST, SEBI fee, Stamp duty.
        """
        risk_pts = abs(entry_price - stop_loss)
        reward_pts = abs(target_price - entry_price)

        # Turnover
        turnover_buy = entry_price * lot_size
        turnover_sell = target_price * lot_size

        # Brokerage (Flat Rs 20 per trade)
        brokerage = brokerage_per_order * 2.0

        # STT (Securities Transaction Tax on Option Sell: 0.0625% on premium)
        stt = turnover_sell * 0.000625

        # Exchange Transaction Charges (NSE Options: 0.05% on premium turnover)
        exchange_charges = (turnover_buy + turnover_sell) * 0.0005

        # GST (18% on Brokerage + Exchange Charges)
        gst = (brokerage + exchange_charges) * 0.18

        # SEBI Turnover Fee (Rs 10 per crore)
        sebi_charges = ((turnover_buy + turnover_sell) / 10000000.0) * 10.0

        # Stamp Duty (0.003% on buy)
        stamp_duty = turnover_buy * 0.00003

        total_costs = brokerage + stt + exchange_charges + gst + sebi_charges + stamp_duty
        slippage_cost = (slippage_pts * 2.0) * lot_size
        total_friction = total_costs + slippage_cost

        gross_profit = reward_pts * lot_size
        gross_loss = risk_pts * lot_size

        net_profit = max(0.0, gross_profit - total_friction)
        net_loss = gross_loss + total_friction

        gross_rr = (reward_pts / risk_pts) if risk_pts > 0 else 0.0
        net_rr = (net_profit / net_loss) if net_loss > 0 else 0.0

        return {
            "reward_to_risk_ratio": round(gross_rr, 2),
            "net_rr": round(net_rr, 2),
            "net_profit_target": round(net_profit, 2),
            "net_loss_stop": round(net_loss, 2),
            "total_estimated_costs": round(total_friction, 2),
            "regulatory_taxes": round(total_costs, 2),
            "slippage_cost": round(slippage_cost, 2),
            "is_acceptable": net_rr >= 1.5
        }

