use serde::{Deserialize, Serialize};

use std::fs;

#[derive(Deserialize, Serialize, Debug)]
pub enum UserConfigItem {
    String {
        name: String,
        max_length: u64,
    },
    IPv4 {
        name: String,
    },
    Boolean {
        name: String,
    },
    Number {
        name: String,
        max: Option<i32>,
        min: Option<i32>,
    },
    Submenu(UserConfigData),
}

#[derive(Deserialize, Serialize, Debug)]
pub struct UserConfigData {
    pub name: String,
    pub config_items: Vec<UserConfigItem>,
}

#[derive(thiserror::Error, Debug)]
pub enum ConfigotronError {
    #[error("IO Operation failed: {}", ._0.to_string())]
    IoError(#[from] std::io::Error),

    #[error("Failed to parse YAML File: {}", ._0.to_string())]
    YamlParsingError(#[from] serde_yml::Error),

    #[error("Missing field {} (line {} col {}) {}", .missing_filed, .line, .col, .context)]
    MissingFieldError {
        col: i32,
        line: i32,
        missing_filed: Box<str>,
        context: Box<str>,
    },
}

pub fn read_and_parse_yaml_file(input_file: &str) -> Result<UserConfigData, ConfigotronError> {
    let file_content = fs::read_to_string(&input_file)?;
    let config: UserConfigData = serde_yml::from_str(&file_content)?;
    return Ok(config);
}
