//! Performance benchmarks for rendering functions
//!
//! Measures rendering performance to ensure TUI remains responsive:
//! - Target: <16ms for 60 FPS rendering
//! - Target: <8ms for 120 FPS rendering
//!
//! Run with: `cargo bench -p syntaxis-tui --bench rendering_performance`

use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use ratatui::backend::TestBackend;
use ratatui::Terminal;
use std::hint::black_box;

// Simulate the types without importing (to avoid dependency issues)
#[derive(Debug, Clone)]
#[allow(dead_code)]
struct MockApp {
    num_projects: usize,
}

fn render_benchmark(c: &mut Criterion) {
    let mut group = c.benchmark_group("rendering");

    // Target 60 FPS (16.67ms per frame)
    group.significance_level(0.1);
    group.sample_size(100);

    // Benchmark 1: Empty project list
    group.bench_function("empty_projects_80x24", |b| {
        b.iter(|| {
            let backend = TestBackend::new(80, 24);
            let _terminal =
                Terminal::new(black_box(backend)).expect("Failed to create test terminal");
        })
    });

    // Benchmark 2: Small project list (5 projects)
    for num_projects in [5, 10, 20].iter() {
        group.bench_with_input(
            BenchmarkId::from_parameter(format!("{}_projects", num_projects)),
            num_projects,
            |b, &num_projects| {
                b.iter(|| {
                    let backend = TestBackend::new(80, 24);
                    let _terminal =
                        Terminal::new(black_box(backend)).expect("Failed to create test terminal");

                    // Simulate rendering complexity with project count
                    for _ in 0..num_projects {
                        let _ = black_box("Project".to_string());
                    }
                })
            },
        );
    }

    // Benchmark 3: Various terminal sizes
    for (width, height) in [(40, 10), (80, 24), (120, 40), (200, 60)].iter() {
        group.bench_with_input(
            BenchmarkId::from_parameter(format!("{}x{}", width, height)),
            &(*width, *height),
            |b, &(width, height)| {
                b.iter(|| {
                    let backend = TestBackend::new(width, height);
                    let _terminal =
                        Terminal::new(black_box(backend)).expect("Failed to create test terminal");
                })
            },
        );
    }

    group.finish();
}

fn layout_computation_benchmark(c: &mut Criterion) {
    let mut group = c.benchmark_group("layout_computation");
    group.significance_level(0.1);
    group.sample_size(1000);

    // Benchmark layout computation at various scales
    for size in [80, 120, 200].iter() {
        group.bench_with_input(
            BenchmarkId::from_parameter(format!("width_{}", size)),
            size,
            |b, &size| {
                b.iter(|| {
                    let _width_third = black_box(size / 3);
                    let _height_third = black_box(24 / 3);
                })
            },
        );
    }

    group.finish();
}

criterion_group!(benches, render_benchmark, layout_computation_benchmark);
criterion_main!(benches);
