//! Test official DBN crate decoder to understand API
//! Run with: cargo script test_dbn_decoder.rs

use anyhow::Result;
use std::fs::File;
use std::io::BufReader;

fn main() -> Result<()> {
    // Use official dbn crate decoder
    use dbn::decode::dbn::Decoder;
    use dbn::RecordRef;

    let file_path = "test_data/real/databento/ml_training_small/6E.FUT_ohlcv-1m_2024-01-02.dbn";
    println!("Testing DBN decoder on: {}", file_path);

    let file = File::open(file_path)?;
    let reader = BufReader::new(file);

    // Create decoder
    let mut decoder = Decoder::new(reader)?;

    // Read metadata
    let metadata = decoder.metadata();
    println!("\nMetadata:");
    println!("  Version: {:?}", metadata.version);
    println!("  Dataset: {:?}", metadata.dataset);
    println!("  Schema: {:?}", metadata.schema);
    println!("  Start: {:?}", metadata.start);
    println!("  End: {:?}", metadata.end);
    println!("  Symbols: {:?}", metadata.symbols);

    // Count OHLCV records
    let mut ohlcv_count = 0;
    let mut other_count = 0;
    let mut total_count = 0;

    println!("\nDecoding records...");
    for (idx, record) in decoder.enumerate() {
        let record = record?;
        total_count += 1;

        match record {
            RecordRef::Ohlcv(ohlcv) => {
                ohlcv_count += 1;
                if ohlcv_count <= 3 {
                    println!("  Record {}: OHLCV - open={}, high={}, low={}, close={}, volume={}",
                             idx, ohlcv.open, ohlcv.high, ohlcv.low, ohlcv.close, ohlcv.volume);
                }
            }
            _ => {
                other_count += 1;
                if other_count <= 3 {
                    println!("  Record {}: {:?}", idx, record);
                }
            }
        }
    }

    println!("\nSummary:");
    println!("  Total records: {}", total_count);
    println!("  OHLCV records: {}", ohlcv_count);
    println!("  Other records: {}", other_count);

    Ok(())
}
