// benches/engine_bench.rs

#![recursion_limit = "1024"]

use axioma::prelude::*;
use criterion::{black_box, criterion_group, criterion_main, Criterion};

// Define huge payload macro.
macro_rules! huge_json_payload {
    ($macro_name:ident) => {
        $macro_name!({
            "ordestra_core": {
                "version": "2.0.0-beta",
                "hot_reload_active": true,
                "render_cycle": 144,
                "viewport": {
                    "width": 1920,
                    "height": 1080,
                    "dpi": 2.0,
                    "safe_area": [0, 0, 0, 0]
                }
            },
            "ui_tree": [
                {
                    "id": "navbar",
                    "type": "Flex",
                    "props": { "direction": "row", "padding": [10, 20], "bg": "#111" },
                    "children": [
                        { "id": "logo", "type": "Image", "src": "logo.png" },
                        { "id": "menu", "type": "Grid", "columns": 3, "items": ["Home", "Docs", "GitHub"] }
                    ]
                },
                {
                    "id": "main_content",
                    "type": "ScrollArea",
                    "props": { "scroll_y": true, "max_height": 900 },
                    "children": [
                        {
                            "id": "hero_section",
                            "type": "Container",
                            "nodes": [
                                { "type": "Text", "content": "Lossless Projection" },
                                { "type": "Button", "label": "Get Started", "action": "trigger_auth" }
                            ]
                        },
                        {
                            "id": "data_table",
                            "type": "Table",
                            "rows": [
                                [1, "Alice", "Active", "Admin"],
                                [2, "Bob", "Offline", "User"],
                                [3, "Charlie", "Active", "Editor"],
                                [4, "Dave", "Active", "User"]
                            ]
                        }
                    ]
                }
            ],
            "state_store": {
                "auth": { "logged_in": true, "token": "xyz_789", "permissions": ["read", "write", "execute"] },
                "theme": { "mode": "dark", "primary": "#ff0055", "radius": 8 },
                "cache": { "users": {}, "posts": [], "last_sync": 1712650000 }
            }
        })
    };
}

fn bench_runtime_access(c: &mut Criterion) {
    // Init engines.
    let engine_axioma = huge_json_payload!(axioma);
    let engine_imprint = huge_json_payload!(imprint);
    let engine_kotowari = huge_json_payload!(kotowari);

    let mut group = c.benchmark_group("Engine Runtime Access Speed (Huge Payload)");

    // Benchmark axioma (Lossless O(1) Match Routing).
    // Accessing deep element to measure true cost.
    group.bench_function("axioma_deep_access", |b| {
        b.iter(|| {
            let result = engine_axioma(black_box(5), black_box(5));
            black_box(result.2)
        })
    });

    // Benchmark imprint (Drive O(log N) Skip List).
    group.bench_function("imprint_deep_access", |b| {
        b.iter(|| {
            let result = engine_imprint(black_box(5), black_box(5));
            black_box(result)
        })
    });

    // Benchmark kotowari (Lossy O(1) Match Routing).
    group.bench_function("kotowari_deep_access", |b| {
        b.iter(|| {
            let result = engine_kotowari(black_box(5), black_box(5));
            black_box(result)
        })
    });

    group.finish();
}

// Register criterion group.
criterion_group!(benches, bench_runtime_access);
criterion_main!(benches);
