"""
Unit Tests for Options Analytics Engine and Greeks
"""
import pytest
from python.analytics.options_engine import OptionsEngine

def test_black_scholes_call_greeks():
    # Spot=24500, Strike=24500 (ATM), 7 days to expiry (7/365), Vol=15% (0.15)
    greeks = OptionsEngine.calculate_greeks(
        spot=24500.0,
        strike=24500.0,
        time_to_expiry_years=7.0 / 365.0,
        volatility=0.15,
        risk_free_rate=0.07,
        option_type="CE"
    )
    # ATM Call Delta should be approximately 0.50 to 0.55
    assert 0.45 <= greeks["delta"] <= 0.60
    assert greeks["gamma"] > 0
    assert greeks["theta"] < 0  # Time decay is negative for long call
    assert greeks["vega"] > 0

def test_black_scholes_put_greeks():
    greeks = OptionsEngine.calculate_greeks(
        spot=24500.0,
        strike=24500.0,
        time_to_expiry_years=7.0 / 365.0,
        volatility=0.15,
        risk_free_rate=0.07,
        option_type="PE"
    )
    # ATM Put Delta should be approximately -0.45 to -0.55
    assert -0.60 <= greeks["delta"] <= -0.40
    assert greeks["gamma"] > 0
    assert greeks["theta"] < 0

def test_pcr_calculation():
    chain = [
        {"strike_price": 24400, "ce": {"oi": 50000, "volume": 10000}, "pe": {"oi": 80000, "volume": 15000}},
        {"strike_price": 24500, "ce": {"oi": 100000, "volume": 20000}, "pe": {"oi": 120000, "volume": 25000}},
        {"strike_price": 24600, "ce": {"oi": 80000, "volume": 15000}, "pe": {"oi": 40000, "volume": 8000}},
    ]
    pcr = OptionsEngine.calculate_pcr(chain)
    # Total PE OI = 80k + 120k + 40k = 240k
    # Total CE OI = 50k + 100k + 80k = 230k
    # PCR OI = 240 / 230 ~= 1.043
    assert 1.0 <= pcr["pcr_oi"] <= 1.10
