from lseg_analytics.pricing.yield_book_rest import (
    bulk_yb_port_udi_request,
    create_job,
    get_result
)

import json

payload = """CUSIP userbond1
DESCRIPT 'AMCAR_2020-1_B_@_1.5_ABS'
TCKR AMCAR
PARENTTCKR AMCARPAR
COUP 6.584000
SECTYPE BOND
SECSUBTYPE BOND
SECSUBTYP2 CBOND
DTDATE 2024-10-18
FCPDATE 2025-02-20
DELAYFIXPY 0
PAYFREQ 4
CBRSECTOR NA
ASSETCLSUB ABS
MKTTYPE NA
DAYTYPE 30/360
MOODY AA1
COUNTRY US
CURRENCY USD
ISIN US03067DAE58
PYVALUE 100
MATDATE 2029-11-20
FNLDATE 2029-11-20
REDMP 100
ISSAMT 0
OUTAMT 0
INDUSTRY I.NA
DEBTCLASS DEB-UNS
CALLSCH 20271120 100"""

job = create_job(   
                    name="udi_exmple_job",
                    priority=0, 
                    order="FAST",
                    desc="New job for UDI creation"
                )

udi_request = bulk_yb_port_udi_request(data=payload, job="udi_exmple_job")

udi_response = get_result(request_id_parameter=udi_request.results[0].id)

print(json.dumps(udi_response, indent=4))

bulk_payload = """CUSIP userbond2
DESCRIPT 'AMCAR_2020-1_B_@_1.5_ABS'
TCKR AMCAR
PARENTTCKR AMCARPAR
COUP 6.584000
SECTYPE BOND
SECSUBTYPE BOND
SECSUBTYP2 CBOND
DTDATE 2024-10-18
FCPDATE 2025-02-20
DELAYFIXPY 0
PAYFREQ 4
CBRSECTOR NA
ASSETCLSUB ABS
MKTTYPE NA
DAYTYPE 30/360
MOODY AA1
COUNTRY US
CURRENCY USD
ISIN US03067DAE58
PYVALUE 100
MATDATE 2029-11-20
FNLDATE 2029-11-20
REDMP 100
ISSAMT 0
OUTAMT 0
INDUSTRY I.NA
DEBTCLASS DEB-UNS
CALLSCH 20271120 100

CUSIP usrbnd3
COUP 2
SECTYPE BOND
MATDATE 2029-11-20
SECSUBTYPE BOND
SECSUBTYP2 CBOND
PARENTTCKR BOJA
FNLDATE 2029-11-20
REDMP 100
DTDATE 2024-10-18
ISSAMT 0
OUTAMT 0
INDUSTRY I.NA
COUNTRY US
MKTTYPE NA
CBRSECTOR NA
DEBTCLASS DEB-UNS
PAYFREQ 4
DAYTYPE 30/360
FCPDATE 2025-02-20
CALLSCH 20271120 100

CUSIP usrbnd4
COUP 3
SECTYPE BOND
MATDATE 2030-05-15
SECSUBTYPE BOND
SECSUBTYP2 CBOND
PARENTTCKR BOJA
FNLDATE 2030-05-15
REDMP 100
DTDATE 2024-10-18
ISSAMT 0
OUTAMT 0
INDUSTRY I.NA
COUNTRY US
MKTTYPE NA
CBRSECTOR NA
DEBTCLASS DEB-UNS
PAYFREQ 4
DAYTYPE 30/360
FCPDATE 2025-02-20
CALLSCH 20271120 100

CUSIP usrbnd5
COUP 4
SECTYPE BOND
MATDATE 2030-05-15
SECSUBTYPE BOND
SECSUBTYP2 CBOND
PARENTTCKR BOJA
FNLDATE 2030-05-15
REDMP 100
DTDATE 2024-10-18
ISSAMT 0
OUTAMT 0
INDUSTRY I.NA
COUNTRY US
MKTTYPE NA
CBRSECTOR NA
DEBTCLASS DEB-UNS
PAYFREQ 4
DAYTYPE 30/360
FCPDATE 2025-02-20
CALLSCH 20271120 100
"""

# Uncomment for usage - below is an examplary code for file reading
# with open("// enter your file path here //", "r", encoding="utf-8") as f:
#    bulk_payload = f.read()

job = create_job(   
                    name="udi_bulk_exmple_job",
                    priority=0, 
                    order="FAST",
                    desc="New job for Bulk UDI creation"
                )

udi_bulk_request = bulk_yb_port_udi_request(data=bulk_payload, job="udi_bulk_exmple_job")

for result in udi_bulk_request.results:
    response = get_result(request_id_parameter=result.id)
    
    result = {}
    
    # Split into lines
    lines = response["data"].strip().split("\n")
    
    for line in lines:
        if not line.strip():
            continue  # skip empty lines
        
        parts = line.split(" ", 1)  # split into key and rest of line
        if len(parts) == 2:
            key, value = parts
            result[key] = value.strip()
        else:
            # Line has no space - treat value as empty
            result[parts[0]] = ""

    print(json.dumps(result, indent=2))