use super::super::models::types::{ExecRequest, SystemProfile};
use super::profile::system_profile_from_outputs;
use super::{
    posix_build_exec_command, RemoteShell, SystemProfileCommands, SystemProfileOutputs, SystemType,
};

pub(crate) struct PosixLinuxShell;

impl RemoteShell for PosixLinuxShell {
    fn system_type(&self) -> SystemType {
        SystemType::Linux
    }

    fn build_exec_command(&self, req: &ExecRequest) -> String {
        posix_build_exec_command(req)
    }

    fn build_system_profile_commands(&self) -> SystemProfileCommands {
        SystemProfileCommands {
            os_info: ExecRequest {
                command: "cat /etc/os-release".to_string(),
                use_shell: true,
                ..ExecRequest::default()
            },
            arch: ExecRequest {
                command: "uname -m".to_string(),
                use_shell: true,
                ..ExecRequest::default()
            },
            shell: ExecRequest {
                command: "printf %s \"$SHELL\"".to_string(),
                use_shell: true,
                ..ExecRequest::default()
            },
            package_manager: ExecRequest {
                command: "command -v apt-get || command -v dnf || command -v yum || command -v apk"
                    .to_string(),
                use_shell: true,
                ..ExecRequest::default()
            },
        }
    }

    fn parse_system_profile(&self, outputs: &SystemProfileOutputs) -> SystemProfile {
        system_profile_from_outputs(
            &outputs.os_info,
            &outputs.arch,
            &outputs.shell,
            &outputs.package_manager,
        )
    }
}
