use std::path::PathBuf;

use thiserror::Error;

#[derive(Debug, Error)]
pub enum PathsError {
    #[error("could not determine home directory")]
    NoHome,

    #[error("could not determine install directory")]
    NoInstallDir,
}

pub fn repositories_dir() -> Result<PathBuf, PathsError> {
    Ok(dolly_dir()?.join("repositories"))
}

pub fn recipes_dir() -> Result<PathBuf, PathsError> {
    Ok(dolly_dir()?.join("recipes"))
}

pub fn default_install_dir() -> Result<PathBuf, PathsError> {
    dirs::executable_dir()
        .or_else(|| dirs::home_dir().map(|h| h.join(".local/bin")))
        .ok_or(PathsError::NoInstallDir)
}

fn dolly_dir() -> Result<PathBuf, PathsError> {
    Ok(dirs::home_dir().ok_or(PathsError::NoHome)?.join(".dolly"))
}
