use once_cell::sync::OnceCell;
use runmat_builtins::Value;
use runmat_thread_local::runmat_thread_local;
use runmat_time::unix_timestamp_ms;
use std::cell::RefCell;
use std::sync::{Arc, RwLock};

/// Identifies the console stream that received the text.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ConsoleStream {
    Stdout,
    Stderr,
    ClearScreen,
}

/// Single console write (line or chunk) captured during execution.
#[derive(Clone, Debug)]
pub struct ConsoleEntry {
    pub stream: ConsoleStream,
    pub text: String,
    pub timestamp_ms: u64,
}

type StreamForwarder = dyn Fn(&ConsoleEntry) + Send + Sync + 'static;

runmat_thread_local! {
    static THREAD_BUFFER: RefCell<Vec<ConsoleEntry>> = const { RefCell::new(Vec::new()) };
    static LAST_VALUE_OUTPUT: RefCell<Option<Value>> = const { RefCell::new(None) };
}

static FORWARDER: OnceCell<RwLock<Option<Arc<StreamForwarder>>>> = OnceCell::new();

fn now_ms() -> u64 {
    unix_timestamp_ms().min(u64::MAX as u128) as u64
}

/// Record console output for the current thread while also forwarding it to any
/// registered listener (used by wasm bindings for live streaming).
pub fn record_console_output(stream: ConsoleStream, text: impl Into<String>) {
    let entry = ConsoleEntry {
        stream,
        text: text.into(),
        timestamp_ms: now_ms(),
    };
    THREAD_BUFFER.with(|buf| buf.borrow_mut().push(entry.clone()));

    if let Some(forwarder) = FORWARDER
        .get()
        .and_then(|lock| lock.read().ok().map(|guard| guard.as_ref().cloned()))
        .flatten()
    {
        forwarder(&entry);
    }
}

/// Record a control event that asks the host to clear the visible console.
pub fn record_clear_screen() {
    record_console_output(ConsoleStream::ClearScreen, String::new());
}

/// Record a line-oriented console entry, ensuring the stream text ends with a newline.
pub fn record_console_line(stream: ConsoleStream, text: impl Into<String>) {
    let mut text = text.into();
    if !text.ends_with('\n') {
        text.push('\n');
    }
    record_console_output(stream, text);
}

/// Clears the per-thread console buffer. Call this before execution begins so
/// each run only returns fresh output.
pub fn reset_thread_buffer() {
    THREAD_BUFFER.with(|buf| buf.borrow_mut().clear());
    LAST_VALUE_OUTPUT.with(|value| value.borrow_mut().take());
}

/// Drain (and return) the buffered console entries for the current thread.
pub fn take_thread_buffer() -> Vec<ConsoleEntry> {
    THREAD_BUFFER.with(|buf| buf.borrow_mut().drain(..).collect())
}

/// Install (or remove) a global forwarder for console output. Passing `None`
/// removes the current listener.
pub fn install_forwarder(forwarder: Option<Arc<StreamForwarder>>) {
    let lock = FORWARDER.get_or_init(|| RwLock::new(None));
    if let Ok(mut guard) = lock.write() {
        *guard = forwarder;
    }
}

/// Convenience helper to record formatted value output (matching MATLAB's `name = value` layout).
pub fn record_value_output(label: Option<&str>, value: &Value) {
    LAST_VALUE_OUTPUT.with(|last| {
        *last.borrow_mut() = Some(value.clone());
    });
    let value_text = match value {
        Value::Object(obj) if obj.is_class("datetime") => {
            crate::builtins::datetime::datetime_display_text(value)
                .ok()
                .flatten()
                .unwrap_or_else(|| value.to_string())
        }
        Value::Object(obj) if obj.is_class("duration") => {
            crate::builtins::duration::duration_display_text(value)
                .ok()
                .flatten()
                .unwrap_or_else(|| value.to_string())
        }
        _ => value.to_string(),
    };
    let text = if let Some(name) = label {
        if is_unlabeled_nd_page_display(&value_text) {
            inject_label_into_nd_page_headers(name, &value_text)
        } else if value_text.contains('\n') {
            format!("{name} =\n{value_text}")
        } else {
            format!("{name} = {value_text}")
        }
    } else {
        value_text
    };
    record_console_line(ConsoleStream::Stdout, text);
}

pub fn take_last_value_output() -> Option<Value> {
    LAST_VALUE_OUTPUT.with(|value| value.borrow_mut().take())
}

fn is_unlabeled_nd_page_display(text: &str) -> bool {
    text.lines()
        .any(|line| line.trim_start().starts_with("(:, :") && line.trim_end().ends_with('='))
}

fn inject_label_into_nd_page_headers(label: &str, text: &str) -> String {
    let mut out = String::new();
    for (idx, line) in text.lines().enumerate() {
        if idx > 0 {
            out.push('\n');
        }
        let trimmed = line.trim_start();
        if trimmed.starts_with("(:, :") && trimmed.trim_end().ends_with('=') {
            out.push_str(label);
            out.push_str(trimmed);
        } else {
            out.push_str(line);
        }
    }
    out
}
