use std::fmt::Write;
use std::str::FromStr;
use std::sync::Arc;

use axum::extract::Query;
use axum::http::StatusCode;
use axum::response::IntoResponse;
use serde::Deserialize;
use tracing::{info, warn};

use crate::consts::{ENUM_NULL, H3_PRECOMPUTE_MAX, H3_REQUEST_MAX, H3_REQUEST_MIN, HISTOGRAM_BINS};
use crate::filter::{parse_filters, row_passes_filters};
use crate::state::AppState;

use super::parse::h3_cell_bounds;

#[derive(Deserialize)]
pub struct HexagonStatsParams {
    pub h3: String,
    pub resolution: u8,
    pub filters: Option<String>,
    /// Comma-separated feature names to include in stats response.
    /// When present (even if empty), only listed features are computed.
    /// When absent, all features are returned (backward compatible).
    pub fields: Option<String>,
}

pub async fn get_hexagon_stats(
    state: Arc<AppState>,
    Query(params): Query<HexagonStatsParams>,
) -> Result<impl IntoResponse, (StatusCode, String)> {
    let cell = h3o::CellIndex::from_str(&params.h3).map_err(|error| {
        warn!(h3 = %params.h3, error = %error, "Invalid H3 cell index");
        (
            StatusCode::BAD_REQUEST,
            format!("Invalid H3 cell: {}", error),
        )
    })?;
    let cell_u64: u64 = cell.into();

    let resolution = params.resolution;
    if !(H3_REQUEST_MIN..=H3_REQUEST_MAX).contains(&resolution) {
        warn!(
            resolution,
            "Resolution out of range [{}, {}]", H3_REQUEST_MIN, H3_REQUEST_MAX
        );
        return Err((
            StatusCode::BAD_REQUEST,
            format!(
                "resolution must be between {} and {}",
                H3_REQUEST_MIN, H3_REQUEST_MAX
            ),
        ));
    }
    let h3_str = params.h3.clone();
    let filters_str = params.filters.clone();
    let (parsed_filters, parsed_enum_filters) = parse_filters(
        params.filters.as_deref(),
        &state.data.feature_names,
        &state.data.enum_features,
    );
    let num_filters = parsed_filters.len() + parsed_enum_filters.len();

    // Parse optional `fields` param into sets of feature names.
    // None = include all, Some = only include listed features.
    let field_set: Option<std::collections::HashSet<String>> =
        params.fields.as_ref().map(|fields_str| {
            fields_str
                .split(',')
                .map(|field| field.trim().to_string())
                .filter(|field| !field.is_empty())
                .collect()
        });

    let result = tokio::task::spawn_blocking(move || {
        let start_time = std::time::Instant::now();
        let precomputed = &state.h3_cells;
        let h3_res = h3o::Resolution::try_from(resolution)
            .map_err(|err| format!("Invalid H3 resolution {}: {}", resolution, err))?;
        let need_parent = resolution < H3_PRECOMPUTE_MAX;
        let num_features = state.data.num_features;
        let num_enums = state.data.num_enums;
        let feature_data = &state.data.feature_data;
        let enum_data = &state.data.enum_data;
        let enum_features = &state.data.enum_features;

        let (min_lat, min_lon, max_lat, max_lon) = h3_cell_bounds(cell, 0.001);

        // Resolve cell at requested resolution from precomputed max-resolution cell
        let cell_for_row = |row: usize| -> u64 {
            let max_cell = precomputed[row];
            if !need_parent || max_cell == 0 {
                return max_cell;
            }
            h3o::CellIndex::try_from(max_cell)
                .ok()
                .and_then(|ci| ci.parent(h3_res))
                .map(u64::from)
                .unwrap_or(0)
        };

        // Collect matching rows
        let mut matching_rows: Vec<usize> = Vec::new();
        state
            .grid
            .for_each_in_bounds(min_lat, min_lon, max_lat, max_lon, |row_idx| {
                let row = row_idx as usize;
                if cell_for_row(row) == cell_u64
                    && row_passes_filters(
                        row,
                        &parsed_filters,
                        &parsed_enum_filters,
                        feature_data,
                        num_features,
                        enum_data,
                        num_enums,
                    )
                {
                    matching_rows.push(row);
                }
            });

        let total_count = matching_rows.len();

        // Build JSON directly via string buffer
        let mut output = String::with_capacity(4096);
        output.push_str("{\"count\":");
        write!(output, "{}", total_count).unwrap();

        // Numeric features: compute count, min, max, sum, histogram using global bin edges
        output.push_str(",\"numeric_features\":[");
        let mut first_numeric = true;
        for (feature_index, feature_name) in state.data.feature_names.iter().enumerate() {
            // Skip features not in the requested set (when fields param is present)
            if let Some(ref set) = field_set {
                if !set.contains(feature_name.as_str()) {
                    continue;
                }
            }
            let global_stats = &state.data.feature_stats[feature_index];
            let histogram_min = global_stats.histogram.min;
            let histogram_max = global_stats.histogram.max;
            let bin_width = global_stats.histogram.bin_width;

            let mut count = 0usize;
            let mut min_value = f32::INFINITY;
            let mut max_value = f32::NEG_INFINITY;
            let mut sum = 0.0f64; // keep f64 for mean precision
            let mut bins = vec![0u64; HISTOGRAM_BINS];

            for &row in &matching_rows {
                let value = feature_data[row * num_features + feature_index];
                if value.is_finite() {
                    count += 1;
                    if value < min_value {
                        min_value = value;
                    }
                    if value > max_value {
                        max_value = value;
                    }
                    sum += value as f64;

                    // Bin into histogram using global edges (cast to f64 for bin index math)
                    if bin_width > 0.0 {
                        let bin_index = ((value as f64 - histogram_min as f64) / bin_width as f64)
                            .floor() as isize;
                        let clamped_index =
                            bin_index.max(0).min((HISTOGRAM_BINS - 1) as isize) as usize;
                        bins[clamped_index] += 1;
                    }
                }
            }

            if count == 0 {
                continue;
            }

            if !first_numeric {
                output.push(',');
            }
            first_numeric = false;

            let mean = sum / count as f64;
            output.push_str("{\"name\":");
            write_json_string(&mut output, feature_name);
            write!(output, ",\"count\":{}", count).unwrap();
            write!(output, ",\"min\":{}", format_num(min_value)).unwrap();
            write!(output, ",\"max\":{}", format_num(max_value)).unwrap();
            write!(output, ",\"mean\":{}", format_f64(mean)).unwrap();
            output.push_str(",\"histogram\":{\"min\":");
            write!(output, "{}", format_num(histogram_min)).unwrap();
            output.push_str(",\"max\":");
            write!(output, "{}", format_num(histogram_max)).unwrap();
            output.push_str(",\"bin_width\":");
            write!(output, "{}", format_num(bin_width)).unwrap();
            output.push_str(",\"counts\":[");
            for (bin_index, &bin_count) in bins.iter().enumerate() {
                if bin_index > 0 {
                    output.push(',');
                }
                write!(output, "{}", bin_count).unwrap();
            }
            output.push_str("]}}")
        }

        // Enum features: count per value
        output.push_str("],\"enum_features\":[");
        let mut first_enum = true;
        for enum_feature in enum_features {
            // Skip enum features not in the requested set
            if let Some(ref set) = field_set {
                if !set.contains(enum_feature.name.as_str()) {
                    continue;
                }
            }
            let enum_index = match state.enum_name_to_idx.get(&enum_feature.name) {
                Some(&index) => index,
                None => continue,
            };

            let mut value_counts = vec![0u64; enum_feature.values.len()];
            for &row in &matching_rows {
                let value = enum_data[row * num_enums + enum_index];
                if value != ENUM_NULL && (value as usize) < value_counts.len() {
                    value_counts[value as usize] += 1;
                }
            }

            // Only include if there are any non-zero counts
            let has_values = value_counts.iter().any(|&count| count > 0);
            if !has_values {
                continue;
            }

            if !first_enum {
                output.push(',');
            }
            first_enum = false;

            output.push_str("{\"name\":");
            write_json_string(&mut output, &enum_feature.name);
            output.push_str(",\"counts\":{");
            let mut first_value = true;
            for (value_index, &count) in value_counts.iter().enumerate() {
                if count == 0 {
                    continue;
                }
                if !first_value {
                    output.push(',');
                }
                first_value = false;
                write_json_string(&mut output, &enum_feature.values[value_index]);
                write!(output, ":{}", count).unwrap();
            }
            output.push_str("}}");
        }
        output.push_str("]}");

        let elapsed = start_time.elapsed();
        info!(
            h3 = %h3_str,
            resolution,
            total_count,
            filters = num_filters,
            filters_raw = filters_str.as_deref().unwrap_or("-"),
            ms = format_args!("{:.1}", elapsed.as_secs_f64() * 1000.0),
            "GET /api/hexagon-stats"
        );

        Ok(output)
    })
    .await
    .map_err(|error| (StatusCode::INTERNAL_SERVER_ERROR, error.to_string()))?
    .map_err(|error| (StatusCode::INTERNAL_SERVER_ERROR, error))?;

    Ok((
        [(axum::http::header::CONTENT_TYPE, "application/json")],
        result,
    ))
}

fn write_json_string(output: &mut String, value: &str) {
    output.push('"');
    for character in value.chars() {
        match character {
            '"' => output.push_str("\\\""),
            '\\' => output.push_str("\\\\"),
            '\n' => output.push_str("\\n"),
            '\r' => output.push_str("\\r"),
            '\t' => output.push_str("\\t"),
            other => output.push(other),
        }
    }
    output.push('"');
}

fn format_num(value: f32) -> String {
    let fv = value as f64;
    if fv.fract() == 0.0 && fv.abs() < 1e15 {
        format!("{:.1}", fv)
    } else {
        format!("{}", fv)
    }
}

fn format_f64(value: f64) -> String {
    if value.fract() == 0.0 && value.abs() < 1e15 {
        format!("{:.1}", value)
    } else {
        format!("{}", value)
    }
}
