use super::exec::execute_sudo_command;
use super::super::error::BtrfsError;
use crate::process::CommandResult;

pub fn execute(device: &str, label: Option<&str>) -> Result<CommandResult, BtrfsError> {
    let mut args = Vec::new();
    if let Some(l) = label {
        args.push("-L");
        args.push(l);
    }
    args.push(device);
    execute_sudo_command("mkfs.btrfs", &args)
}

pub fn parse_uuid(output: &str) -> Result<String, BtrfsError> {
    for line in output.lines() {
        if line.trim().starts_with("UUID:") {
            let parts: Vec<&str> = line.split(':').collect();
            if parts.len() >= 2 {
                return Ok(parts[1].trim().to_string());
            }
        }
    }
    Err(BtrfsError::ParseError("Could not find UUID in mkfs.btrfs output".to_string()))
}

pub fn execute_and_parse_uuid(device: &str, label: Option<&str>) -> Result<String, BtrfsError> {
    let result = execute(device, label)?;
    parse_uuid(&result.stdout)
}

