//! Fetch historical data from the MMSDM archive.
//!
//! ```sh
//! cargo run --example historical
//! ```

use nemweb::Region;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    // ── Historical dispatch prices ─────────────────────────────────
    // Date range auto-routes to the MMSDM monthly archives.
    let prices = nemweb::dispatch()
        .from("2024-01-01")?
        .to("2024-01-31")?
        .fetch()
        .await?;
    println!("=== Dispatch Prices Jan 2024: {} records ===", prices.len());

    // ── With region filter ─────────────────────────────────────────
    let vic = nemweb::dispatch()
        .region(Region::VIC1)
        .from("2024-01-01")?
        .to("2024-01-07")?
        .fetch()
        .await?;
    println!("=== VIC1 first week: {} records ===", vic.len());

    // ── Historical trading prices ──────────────────────────────────
    let trading = nemweb::trading()
        .from("2024-01-01")?
        .to("2024-01-07")?
        .fetch()
        .await?;
    println!(
        "=== Trading Prices Jan 1-7 2024: {} records ===",
        trading.len()
    );

    // ── Historical demand ──────────────────────────────────────────
    let demand = nemweb::demand()
        .region(Region::NSW1)
        .from("2024-01-01")?
        .to("2024-01-07")?
        .fetch()
        .await?;
    println!("=== NSW1 Demand Jan 1-7 2024: {} records ===", demand.len());
    if let Some(d) = demand.first() {
        println!(
            "  first: demand={:.0}MW gen={:.0}MW | {}",
            d.total_demand, d.available_generation, d.settlement_date
        );
    }

    // ── Historical interconnector flows ────────────────────────────
    let flows = nemweb::interconnector()
        .id("VIC1-NSW1")
        .from("2024-01-01")?
        .to("2024-01-07")?
        .fetch()
        .await?;
    println!(
        "=== VIC1-NSW1 Interconnector Jan 1-7 2024: {} records ===",
        flows.len()
    );

    // ── Historical SCADA ───────────────────────────────────────────
    let scada = nemweb::scada()
        .duid("BASTYAN")
        .from("2024-01-01")?
        .to("2024-01-01")?
        .fetch()
        .await?;
    println!("=== BASTYAN SCADA Jan 1 2024: {} records ===", scada.len());

    // ── Omit .to() — defaults to today ─────────────────────────────
    // Fetches from the start date through today. Keep the range short
    // unless you want to hit many months of MMSDM archive at once.
    let recent = nemweb::dispatch()
        .region(Region::QLD1)
        .from("2024-02-01")?
        .to("2024-02-07")?
        .fetch()
        .await?;
    println!("=== QLD1 first week Feb 2024: {} records ===", recent.len());

    // ── Trading with region filter ─────────────────────────────────
    let trading_sa = nemweb::trading()
        .region(Region::SA1)
        .from("2024-01-01")?
        .to("2024-01-07")?
        .fetch()
        .await?;
    println!(
        "=== SA1 Trading Jan 1-7 2024: {} records ===",
        trading_sa.len()
    );

    Ok(())
}
