use std::str::FromStr;

use serde::{Deserialize, Serialize};

use crate::plant_state::PlantWateringMode;
use crate::PLANT_COUNT;

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(default)]
pub struct NetworkConfig {
    pub ap_ssid: heapless::String<32>,
    pub ssid: Option<heapless::String<32>>,
    pub password: Option<heapless::String<64>>,
    pub mqtt_url: Option<heapless::String<128>>,
    pub base_topic: Option<heapless::String<64>>,
}
impl Default for NetworkConfig {
    fn default() -> Self {
        Self {
            ap_ssid: heapless::String::from_str("PlantCtrl Init").unwrap(),
            ssid: None,
            password: None,
            mqtt_url: None,
            base_topic: None,
        }
    }
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(default)]
pub struct NightLampConfig {
    pub enabled: bool,
    pub night_lamp_hour_start: u8,
    pub night_lamp_hour_end: u8,
    pub night_lamp_only_when_dark: bool,
    pub low_soc_cutoff: u8,
    pub low_soc_restore: u8,
}
impl Default for NightLampConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            night_lamp_hour_start: 19,
            night_lamp_hour_end: 2,
            night_lamp_only_when_dark: true,
            low_soc_cutoff: 30,
            low_soc_restore: 50,
        }
    }
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(default)]
pub struct TankConfig {
    pub tank_sensor_enabled: bool,
    pub tank_allow_pumping_if_sensor_error: bool,
    pub tank_useable_ml: u32,
    pub tank_warn_percent: u8,
    pub tank_empty_percent: u8,
    pub tank_full_percent: u8,
}
impl Default for TankConfig {
    fn default() -> Self {
        Self {
            tank_sensor_enabled: false,
            tank_allow_pumping_if_sensor_error: true,
            tank_useable_ml: 50000,
            tank_warn_percent: 40,
            tank_empty_percent: 5,
            tank_full_percent: 95,
        }
    }
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
#[serde(default)]
pub struct PlantControllerConfig {
    pub network: NetworkConfig,
    pub tank: TankConfig,
    pub night_lamp: NightLampConfig,
    pub plants: [PlantConfig; PLANT_COUNT],
    pub timezone: Option<String>,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(default)]
pub struct PlantConfig {
    pub mode: PlantWateringMode,
    pub target_moisture: f32,
    pub pump_time_s: u16,
    pub pump_cooldown_min: u16,
    pub pump_hour_start: u8,
    pub pump_hour_end: u8,
    pub sensor_a: bool,
    pub sensor_b: bool,
    pub max_consecutive_pump_count: u8,
    pub moisture_sensor_min_frequency: Option<f32>, // Optional min frequency
    pub moisture_sensor_max_frequency: Option<f32>, // Optional max frequency
}

impl Default for PlantConfig {
    fn default() -> Self {
        Self {
            mode: PlantWateringMode::OFF,
            target_moisture: 40.,
            pump_time_s: 30,
            pump_cooldown_min: 60,
            pump_hour_start: 9,
            pump_hour_end: 20,
            sensor_a: true,
            sensor_b: false,
            max_consecutive_pump_count: 10,
            moisture_sensor_min_frequency: None, // No override by default
            moisture_sensor_max_frequency: None, // No override by default
        }
    }
}
