//! Tracing-based logging setup.
//!
//! TUI applications cannot write logs to stdout/stderr without
//! corrupting the terminal, so logs go to a file. The path comes
//! from the CLI (`--log-file`) or the `RDBMS_PLAYGROUND_LOG_FILE`
//! environment variable; if neither is set we default to
//! `~/.rdbms-playground/playground.log` and create directories as
//! needed.

use std::fs::{File, OpenOptions, create_dir_all};
use std::path::{Path, PathBuf};

use anyhow::{Context, Result};
use tracing_subscriber::{EnvFilter, fmt, layer::SubscriberExt, util::SubscriberInitExt};

const DEFAULT_LOG_DIR: &str = ".rdbms-playground";
const DEFAULT_LOG_FILE: &str = "playground.log";

/// Initialise tracing to write to the given file path, or to a
/// platform-default path when `path` is `None`.
pub fn init(path: Option<&Path>) -> Result<PathBuf> {
    let chosen = match path {
        Some(p) => p.to_path_buf(),
        None => default_log_path()?,
    };
    if let Some(parent) = chosen.parent() {
        create_dir_all(parent)
            .with_context(|| format!("create log directory {}", parent.display()))?;
    }
    let file = open_log_file(&chosen)?;
    let filter = EnvFilter::try_from_env("RDBMS_PLAYGROUND_LOG")
        .unwrap_or_else(|_| EnvFilter::new("info"));
    let layer = fmt::layer()
        .with_writer(file)
        .with_ansi(false)
        .with_target(true);
    tracing_subscriber::registry()
        .with(filter)
        .with(layer)
        .init();
    tracing::info!(path = %chosen.display(), "logging initialised");
    Ok(chosen)
}

fn open_log_file(path: &Path) -> Result<File> {
    OpenOptions::new()
        .create(true)
        .append(true)
        .open(path)
        .with_context(|| format!("open log file {}", path.display()))
}

fn default_log_path() -> Result<PathBuf> {
    let home = home_dir().context("could not determine HOME directory for default log path")?;
    Ok(home.join(DEFAULT_LOG_DIR).join(DEFAULT_LOG_FILE))
}

fn home_dir() -> Option<PathBuf> {
    // std::env::home_dir is deprecated; do the lookup ourselves with
    // the platform-conventional environment variables. Once we add a
    // proper path strategy (project storage, ADR-0004) this can be
    // replaced with the chosen helper crate.
    if let Some(p) = std::env::var_os("HOME") {
        return Some(PathBuf::from(p));
    }
    if let (Some(drive), Some(path)) =
        (std::env::var_os("HOMEDRIVE"), std::env::var_os("HOMEPATH"))
    {
        let mut combined = PathBuf::from(drive);
        combined.push(path);
        return Some(combined);
    }
    std::env::var_os("USERPROFILE").map(PathBuf::from)
}
