"""Generate example chart images showcasing all chart types.

Run from the project root:
    uv run python examples/generate_examples.py
"""

from __future__ import annotations

import random
from pathlib import Path

from kapsarc_mcp_server.charts import render_chart

OUTPUT_DIR = Path(__file__).resolve().parent


def save(name: str, img) -> None:
    path = OUTPUT_DIR / f"{name}.png"
    path.write_bytes(img.data)
    print(f"  Saved {path.name} ({len(img.data):,} bytes)")


def main() -> None:
    print("Generating example charts...\n")

    # 1. Line chart — time series (auto-detected)
    records = [
        {"year": 2015, "production": 10.2},
        {"year": 2016, "production": 10.5},
        {"year": 2017, "production": 10.0},
        {"year": 2018, "production": 10.7},
        {"year": 2019, "production": 9.8},
        {"year": 2020, "production": 9.2},
        {"year": 2021, "production": 9.5},
        {"year": 2022, "production": 10.4},
        {"year": 2023, "production": 10.6},
        {"year": 2024, "production": 10.9},
    ]
    save("line_chart", render_chart(records, title="Saudi Arabia Oil Production (Million bpd)"))

    # 2. Bar chart — categorical comparison (auto-detected)
    records = [
        {"country": "Saudi Arabia", "production": 10.5},
        {"country": "United States", "production": 12.9},
        {"country": "Russia", "production": 10.8},
        {"country": "Canada", "production": 4.7},
        {"country": "China", "production": 4.1},
        {"country": "Iraq", "production": 4.5},
        {"country": "UAE", "production": 3.2},
        {"country": "Brazil", "production": 3.0},
    ]
    save("bar_chart", render_chart(records, title="Top Oil Producers 2024 (Million bpd)"))

    # 3. Pie chart — proportional breakdown (auto-detected)
    records = [
        {"source": "Oil", "share": 31.0},
        {"source": "Natural Gas", "share": 24.0},
        {"source": "Coal", "share": 27.0},
        {"source": "Nuclear", "share": 4.0},
        {"source": "Renewables", "share": 14.0},
    ]
    save("pie_chart", render_chart(records, title="Global Energy Mix 2024"))

    # 4. Multi-line chart — series comparison
    records = []
    data = {
        "Saudi Arabia": [10.2, 10.5, 10.0, 10.7, 9.8, 9.2, 9.5, 10.4],
        "United States": [9.4, 8.8, 9.4, 10.9, 12.3, 11.3, 11.2, 11.9],
        "Russia": [10.9, 11.2, 11.3, 11.4, 11.5, 10.7, 10.9, 10.8],
    }
    for country, vals in data.items():
        for i, v in enumerate(vals):
            records.append({"year": 2015 + i, "production": v, "country": country})
    save(
        "multiline_chart",
        render_chart(
            records,
            x_field="year",
            y_field="production",
            series_field="country",
            title="Oil Production Comparison (Million bpd)",
        ),
    )

    # 5. Scatter plot — two numeric fields
    random.seed(42)
    records = [
        {
            "gdp_per_capita": 10000 + i * 2000,
            "energy_consumption": 50 + i * 8 + random.randint(-10, 10),
        }
        for i in range(20)
    ]
    save(
        "scatter_chart",
        render_chart(
            records,
            x_field="gdp_per_capita",
            y_field="energy_consumption",
            chart_type="scatter",
            title="GDP vs Energy Consumption",
        ),
    )

    # 6. Horizontal bar chart — many categories
    records = [
        {"sector": "Transportation", "consumption": 28.3},
        {"sector": "Industrial", "consumption": 24.1},
        {"sector": "Residential", "consumption": 16.8},
        {"sector": "Commercial", "consumption": 12.5},
        {"sector": "Petrochemical Feedstock", "consumption": 8.2},
        {"sector": "Agriculture", "consumption": 4.7},
        {"sector": "Power Generation", "consumption": 3.1},
        {"sector": "Construction", "consumption": 1.4},
        {"sector": "Mining", "consumption": 0.9},
    ]
    save(
        "hbar_chart",
        render_chart(
            records,
            chart_type="hbar",
            title="Energy Consumption by Sector (Mtoe)",
        ),
    )

    # 7. Line chart — electricity prices over time
    records = [
        {"year": 2018, "price_usd_mwh": 62.4},
        {"year": 2019, "price_usd_mwh": 58.1},
        {"year": 2020, "price_usd_mwh": 45.3},
        {"year": 2021, "price_usd_mwh": 78.9},
        {"year": 2022, "price_usd_mwh": 132.5},
        {"year": 2023, "price_usd_mwh": 95.7},
        {"year": 2024, "price_usd_mwh": 83.2},
    ]
    save(
        "electricity_prices",
        render_chart(records, title="Average Electricity Price (USD/MWh)"),
    )

    # 8. Bar chart — renewable energy capacity by country
    records = [
        {"country": "China", "capacity_gw": 1340},
        {"country": "United States", "capacity_gw": 420},
        {"country": "India", "capacity_gw": 195},
        {"country": "Germany", "capacity_gw": 155},
        {"country": "Brazil", "capacity_gw": 175},
        {"country": "Japan", "capacity_gw": 115},
    ]
    save(
        "renewables_capacity",
        render_chart(records, title="Renewable Energy Capacity 2024 (GW)"),
    )

    # 9. Pie chart — GCC electricity generation by fuel
    records = [
        {"fuel": "Natural Gas", "share": 62.0},
        {"fuel": "Oil", "share": 25.0},
        {"fuel": "Solar", "share": 8.0},
        {"fuel": "Wind", "share": 3.0},
        {"fuel": "Nuclear", "share": 2.0},
    ]
    save(
        "gcc_power_mix",
        render_chart(records, title="GCC Electricity Generation by Fuel"),
    )

    # 10. Multi-bar chart — CO2 emissions comparison
    records = []
    emissions_data = {
        "China": [9.8, 10.1, 10.5, 10.7, 11.0],
        "United States": [5.3, 5.1, 4.7, 5.0, 4.9],
        "India": [2.5, 2.6, 2.4, 2.7, 2.9],
    }
    for country, vals in emissions_data.items():
        for i, v in enumerate(vals):
            records.append({"year": 2020 + i, "emissions_gt": v, "country": country})
    save(
        "co2_emissions_comparison",
        render_chart(
            records,
            x_field="year",
            y_field="emissions_gt",
            series_field="country",
            chart_type="multi_bar",
            title="CO2 Emissions by Country (Gt)",
        ),
    )

    # 11. Scatter plot — solar irradiance vs capacity factor
    random.seed(99)
    records = [
        {
            "solar_irradiance_kwh_m2": 1400 + i * 60,
            "capacity_factor_pct": 14 + i * 1.3 + random.uniform(-2, 2),
        }
        for i in range(15)
    ]
    save(
        "solar_scatter",
        render_chart(
            records,
            x_field="solar_irradiance_kwh_m2",
            y_field="capacity_factor_pct",
            chart_type="scatter",
            title="Solar Irradiance vs Capacity Factor",
        ),
    )

    # 12. Multi-line chart — renewable share of electricity
    records = []
    renew_data = {
        "Germany": [36, 38, 42, 44, 47, 50, 52],
        "Saudi Arabia": [0.1, 0.3, 0.5, 1.2, 2.5, 3.8, 5.0],
        "Global Average": [25, 26, 28, 29, 30, 32, 34],
    }
    for region, vals in renew_data.items():
        for i, v in enumerate(vals):
            records.append({"year": 2018 + i, "renewable_pct": v, "region": region})
    save(
        "renewable_share_trend",
        render_chart(
            records,
            x_field="year",
            y_field="renewable_pct",
            series_field="region",
            title="Renewable Share of Electricity (%)",
        ),
    )

    # 13. Bar chart — hydrogen production costs
    records = [
        {"method": "Grey (SMR)", "cost_usd_kg": 1.5},
        {"method": "Blue (SMR+CCS)", "cost_usd_kg": 2.5},
        {"method": "Green (Electrolysis)", "cost_usd_kg": 5.0},
        {"method": "Turquoise (Pyrolysis)", "cost_usd_kg": 3.2},
        {"method": "Pink (Nuclear)", "cost_usd_kg": 4.0},
    ]
    save(
        "hydrogen_costs",
        render_chart(records, title="Hydrogen Production Cost (USD/kg)"),
    )

    # 14. Line chart — Brent crude oil price
    records = [
        {"year": 2015, "price_usd": 52.4},
        {"year": 2016, "price_usd": 43.7},
        {"year": 2017, "price_usd": 54.3},
        {"year": 2018, "price_usd": 71.3},
        {"year": 2019, "price_usd": 64.2},
        {"year": 2020, "price_usd": 41.8},
        {"year": 2021, "price_usd": 70.7},
        {"year": 2022, "price_usd": 99.0},
        {"year": 2023, "price_usd": 82.5},
        {"year": 2024, "price_usd": 78.6},
    ]
    save(
        "brent_crude_price",
        render_chart(records, title="Brent Crude Oil Price (USD/barrel)"),
    )

    # 15. Pie chart — Saudi Arabia GDP composition
    records = [
        {"sector": "Oil & Gas", "share": 42.0},
        {"sector": "Government Services", "share": 17.0},
        {"sector": "Finance & Real Estate", "share": 14.0},
        {"sector": "Manufacturing", "share": 12.0},
        {"sector": "Trade & Hospitality", "share": 10.0},
        {"sector": "Other", "share": 5.0},
    ]
    save(
        "saudi_gdp_composition",
        render_chart(records, title="Saudi Arabia GDP Composition 2024"),
    )

    print("\nDone! All charts saved to examples/")


if __name__ == "__main__":
    main()
