from lseg_analytics.pricing.instruments import structured_notes as sn

import datetime as dt
import pandas as pd
from IPython.display import display

# 1. Provide ISIN/cusip for the request

ISIN = "FR00140157O0"

SN_definition = sn.StructuredNoteDefinitionInstrument(
    code=ISIN
    )
print(f"Security requested: {ISIN}")

# 2. Create Structured Note pricing parameters object - optional

SN_pricing_params = sn.StructuredNotePricingParameters(
            valuation_date = dt.date(2026, 2, 10) # Set your desired valuation date
        )

print("Pricing parameters configured")

# Execute the calculation using the value() function
# The 'definitions' parameter accepts a list of instruments definitions for batch processing
try:
    response = sn.value(
        definitions = [SN_definition],
        pricing_preferences = SN_pricing_params
    )

    errors = [a.error for a in response.analytics if a.error]
    if errors:
        raise Exception(errors[0].message)
    print("Pricing execution completed")
    
except Exception as e:
    print(f"Price calculation failed: {str(e)}")
    raise

# Extract valuation from the response
valuation = response.analytics[0].valuation

# Convert the dictionary to a DataFrame
df_valuation = pd.DataFrame([(key, val['value']) for key, val in valuation.items()],
    columns=['Field', 'Value'])

display(df_valuation)

# Extract Greeks from the response
greeks = response.analytics[0].risk

# Convert the dictionary to a DataFrame
df_greeks = pd.DataFrame([(key, val['value']) for key, val in greeks.items()],
    columns=['Greeks', 'Value'])

display(df_greeks)