//! Logging configuration for application tracing.
//!
//! Provides file-based logging with daily rotation for tracing output.

use std::fs;
use std::io;
use std::path::Path;

use tracing_appender::non_blocking::WorkerGuard;

/// Initialize file-based logging with daily rotation.
///
/// Creates log directory if needed and configures tracing to write to
/// `{log_dir}/tazuna.YYYY-MM-DD`.
///
/// Returns a `WorkerGuard` that must be held for logging to function.
/// Dropping the guard flushes pending logs.
///
/// # Errors
/// Returns error if log directory creation fails.
pub fn init_file_logging(log_dir: &Path) -> io::Result<WorkerGuard> {
    fs::create_dir_all(log_dir)?;

    let file_appender = tracing_appender::rolling::daily(log_dir, "tazuna");
    let (non_blocking, guard) = tracing_appender::non_blocking(file_appender);

    tracing_subscriber::fmt()
        .with_max_level(tracing::Level::INFO)
        .with_writer(non_blocking)
        .with_ansi(false)
        .init();

    Ok(guard)
}
