use std::collections::HashMap;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Layer configuration for individual map layers
///
/// Each layer defines the zoom range at which it should be visible.
///
/// # Layer Settings
///
/// | Setting | Description |
/// |---------|-------------|
/// | `minzoom` | Minimum zoom level at which this layer is visible (0-30) |
/// | `maxzoom` | Maximum zoom level at which this layer is visible (0-30) |
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Layer {
    pub minzoom: u8,
    pub maxzoom: u8,
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Settings {
    pub minzoom: u8,
    pub maxzoom: u8,
    pub basezoom: u8,
    pub include_ids: bool,
    pub compress: String,
    pub name: String,
    pub version: String,
    pub description: String,
}

/// The config that is part of the InputArguments and used for processing
///
/// The structure is borrowed from tilemaker's `config.json`.
///
/// # Settings
///
/// | Setting | Description |
/// |---------|-------------|
/// | `minzoom` | Minimum zoom level for tiles (0-30) |
/// | `maxzoom` | Maximum zoom level for tiles (0-30) |
/// | `basezoom` | Base zoom level used for processing (0-30) |
/// | `include_ids` | Whether to include OSM IDs in the output |
/// | `compress` | Compression method (e.g., "gzip", "zstd", "none") |
/// | `name` | Name of the tileset |
/// | `version` | Version of the tileset |
/// | `description` | Description of the tileset |
///
/// # Example
///
/// ```
/// # #[cfg(feature = "serde")]
/// # {
/// use std::collections::HashMap;
/// use serde_json;
/// use tilemake_rs::Config;
///
/// let json_str = r#"
/// {
///   "layers": {
///     "roads": { "minzoom": 12, "maxzoom": 14 },
///     "buildings": { "minzoom": 14, "maxzoom": 14 }
///   },
///   "settings": {
///     "minzoom": 0,
///     "maxzoom": 1,
///     "basezoom": 1,
///     "include_ids": false,
///     "compress": "gzip",
///     "name": "Example",
///     "version": "0.1",
///     "description": "Example with roads and buildings"
///   }
/// }
/// "#;
///
/// let config = Config::from_json(json_str).unwrap();
/// assert_eq!(config.layers["roads"].minzoom, 12);
/// assert_eq!(config.settings.name, "Example");
/// # }
/// ```
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Config {
    pub layers: HashMap<String, Layer>,
    pub settings: Settings,
}

#[cfg(feature = "serde")]
impl Config {
    /// Deserializes the config from a JSON string
    pub fn from_json(json_str: &str) -> Result<Self, serde_json::Error> {
        serde_json::from_str(json_str)
    }
}
