use std::path::PathBuf;

use serde::Deserialize;

use super::{COMMANDS_PATH, melding::CommandRecipe};

#[derive(Debug, Deserialize)]
pub struct Command {
	pub name: String,
	pub order: u32,
	pub category: String,
	pub char: Vec<char>,
	#[serde(default)]
	pub info: Option<String>,
	#[serde(default)]
	pub recipes: Vec<CommandRecipe>,
}

impl Command {
	pub fn import() -> Vec<Command> {
		let mut commands = vec![];
		let paths = std::fs::read_dir(COMMANDS_PATH)
			.unwrap()
			.filter_map(|f| f.ok())
			.map(|f| f.path())
			.filter_map(|p| match p.extension().is_some_and(|e| e == "toml") {
				true => Some(p),
				false => None,
			})
			.collect::<Vec<PathBuf>>();

		for path in paths {
			let str = std::fs::read_to_string(path).unwrap();
			let cmd = toml::from_str::<Command>(&str).unwrap();

			commands.push(cmd);
		}

		commands
	}
}
