//! Fetch the latest real-time data across all six data types.
//!
//! ```sh
//! cargo run --example realtime
//! ```

use nemweb::Region;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    // ── Dispatch prices (5-min intervals) ──────────────────────────
    let prices = nemweb::dispatch().fetch().await?;
    println!("=== Dispatch Prices ===");
    for p in &prices {
        println!(
            "  {} | ${:.2}/MWh | {}",
            p.region, p.price, p.settlement_date
        );
    }

    // ── Dispatch — filter by region ────────────────────────────────
    let nsw = nemweb::dispatch().region(Region::NSW1).fetch().await?;
    println!("\n=== Dispatch NSW1 Only ===");
    println!("  ${:.2}/MWh", nsw[0].price);

    // ── Trading prices (30-min settlement intervals) ───────────────
    let trading = nemweb::trading().fetch().await?;
    println!("\n=== Trading Prices ===");
    for p in &trading {
        println!(
            "  {} | ${:.2}/MWh | {}",
            p.region, p.price, p.settlement_date
        );
    }

    // ── Trading — filter by region ─────────────────────────────────
    let qld = nemweb::trading().region(Region::QLD1).fetch().await?;
    println!("\n=== Trading QLD1 Only ===");
    for p in &qld {
        println!("  ${:.2}/MWh | {}", p.price, p.settlement_date);
    }

    // ── Region demand & generation ─────────────────────────────────
    let demand = nemweb::demand().fetch().await?;
    println!("\n=== Region Demand ===");
    for d in &demand {
        println!(
            "  {} | demand={:.0}MW gen={:.0}MW net_ix={:.0}MW",
            d.region, d.total_demand, d.available_generation, d.net_interchange
        );
    }

    // ── Demand — filter by region ──────────────────────────────────
    let sa = nemweb::demand().region(Region::SA1).fetch().await?;
    println!("\n=== Demand SA1 Only ===");
    for d in &sa {
        println!(
            "  demand={:.0}MW gen={:.0}MW",
            d.total_demand, d.available_generation
        );
    }

    // ── Interconnector flows ───────────────────────────────────────
    let flows = nemweb::interconnector().fetch().await?;
    println!("\n=== Interconnector Flows ===");
    for f in &flows {
        println!(
            "  {} | {:.0}MW (losses={:.1}MW)",
            f.interconnector_id, f.mw_flow, f.mw_losses
        );
    }

    // ── Interconnector — filter by ID ──────────────────────────────
    let vic_nsw = nemweb::interconnector().id("VIC1-NSW1").fetch().await?;
    println!("\n=== VIC1-NSW1 Only ===");
    for f in &vic_nsw {
        println!("  {:.0}MW (losses={:.1}MW)", f.mw_flow, f.mw_losses);
    }

    // ── Pre-dispatch forecasts ─────────────────────────────────────
    let forecasts = nemweb::predispatch().fetch().await?;
    println!(
        "\n=== Pre-dispatch ({} records, first 5) ===",
        forecasts.len()
    );
    for f in forecasts.iter().take(5) {
        println!("  {} | ${:.2}/MWh | {}", f.region, f.price, f.forecast_time);
    }

    // ── Pre-dispatch — filter by region ────────────────────────────
    let tas = nemweb::predispatch().region(Region::TAS1).fetch().await?;
    println!(
        "\n=== Pre-dispatch TAS1 ({} records, first 3) ===",
        tas.len()
    );
    for f in tas.iter().take(3) {
        println!("  ${:.2}/MWh | {}", f.price, f.forecast_time);
    }

    // ── Unit SCADA ─────────────────────────────────────────────────
    let scada = nemweb::scada().fetch().await?;
    println!("\n=== Unit SCADA ({} units, first 5) ===", scada.len());
    for s in scada.iter().take(5) {
        println!("  {} | {:.1}MW", s.duid, s.scada_value);
    }

    // ── SCADA — filter by DUID ─────────────────────────────────────
    let unit = nemweb::scada().duid("BASTYAN").fetch().await?;
    println!("\n=== BASTYAN Only ===");
    for s in &unit {
        println!("  {:.1}MW | {}", s.scada_value, s.settlement_date);
    }

    Ok(())
}
