# Enhanced YSP Message Analysis Demo

## Test the new ISO8583 field parsing with your actual data

### Sample YSP Data (from your logs)
```
60007820000200723C478108C09206164854980600736740000000000000006800100512470700007512470710053105601100510000078200067820003935443435424631303442343132333435363731313233343536373839303132333435373834376666663838366663373133626531620264396632373031383039663130303730363031313230336130613830323966333730343166363135376264396633363032303335623935303530303830303430303030396130333235303931373963303130303966303230363030303030303030303030303566326130323033353638323032333830303966316130323033353639663033303630303030303030303030303039663333303365306530633839663334303334323033303039663335303132323966316530383332333333373333333033303330333138343037613030303030303030333130313039663039303230303936396634313034303030303033353139663236303834373732313533336161653835343265000630303030303101
```

### IEx Commands to Test

Start an IEx session:
```bash
cd /home/prem/mercurypay/mercury_device_middlelayer
iex -S mix
```

#### 1. Quick Summary Analysis
```elixir
# Your actual hex data from logs
hex_data = "60007820000200723C478108C09206164854980600736740000000000000006800100512470700007512470710053105601100510000078200067820003935443435424631303442343132333435363731313233343536373839303132333435373834376666663838366663373133626531620264396632373031383039663130303730363031313230336130613830323966333730343166363135376264396633363032303335623935303530303830303430303030396130333235303931373963303130303966303230363030303030303030303030303566326130323033353638323032333830303966316130323033353639663033303630303030303030303030303039663333303365306530633839663334303334323033303039663335303132323966316530383332333333373333333033303330333138343037613030303030303030333130313039663039303230303936396634313034303030303033353139663236303834373732313533336161653835343265000630303030303101"

# Convert to binary
binary_data = Base.decode16!(hex_data)

# Quick summary
YspProcessor.print_message_summary(binary_data)
```

#### 2. Detailed Field Analysis
```elixir
# Full field-by-field breakdown
YspProcessor.analyze_message_fields(binary_data)
```

#### 3. Customized Analysis
```elixir
# Without raw hex dump for cleaner output
YspProcessor.analyze_message_fields(binary_data, show_raw: false)

# With smaller hex chunks for easier reading
YspProcessor.analyze_message_fields(binary_data, hex_chunk_size: 16)
```

#### 4. Test with YSP Framing
```elixir
# Test framing the message
{:ok, framed} = YspMessageFraming.frame_message(binary_data)

# Analyze the framed message
YspProcessor.analyze_message_fields(framed)

# Quick summary of framed message
YspProcessor.print_message_summary(framed)
```

#### 5. Export Analysis to String
```elixir
# Get analysis as string for logging
analysis = YspProcessor.format_message_analysis(binary_data, show_raw: false)
IO.puts(analysis)

# Write to file
File.write!("message_analysis.txt", analysis)
```

### Expected Output Features

The enhanced formatter should show:

1. **Message Structure**:
   ```
   ╭─ ISO8583 Message (578 bytes) ─╮

   📋 Message Type Indicator (MTI)
      Raw: 6000
      Type: Unknown (6000)

   🗺️  Primary Bitmap
      Hex: 78 20 00 02 00 72 3C 47
      Fields present: 2, 3, 4, 11, 12, 13, 14, 22, 24, 25, 35, 37, 41, 42, 55, 62
   ```

2. **Individual Fields**:
   ```
   📊 Message Fields
      DE002: "4435424631303442343132333435363731313233343536373839303132333435" (Primary Account Number)
      DE003: "000000" (Processing Code)
      DE004: "000000000068" (Transaction Amount)
      DE011: "001005" (System Trace Audit Number)
   ```

3. **Raw Hex with Offsets**:
   ```
   🔍 Raw Hex Data
      0000: 60 00 78 20 00 02 00 72  3C 47 81 08 C0 92 06 16
      0010: 48 54 98 06 00 73 67 40  00 00 00 00 00 00 00 00
   ```

### Benefits

- **Field Identification**: Easily see which fields are present and their values
- **Structure Understanding**: Clear breakdown of MTI, bitmap, and field data
- **Debug Efficiency**: No more manual hex parsing
- **Logging Ready**: Export analysis to files or logs

### Troubleshooting

If the parser fails, it will fallback to the basic hex printing, so you'll always get readable output.