package theater:simple;

/// OS Process handler interface for managing operating system processes
interface process {

    /// Output processing mode
    variant output-mode {
        /// Process raw bytes without any special handling
        raw,
        /// Process output line by line
        line-by-line,
        /// Process output as JSON objects (newline-delimited)
        json,
        /// Process output in chunks of specified size
        chunked,
    }

    /// Configuration for a process
    record process-config {
        /// Executable path
        program: string,
        /// Command line arguments
        args: list<string>,
        /// Working directory (optional)
        cwd: option<string>,
        /// Environment variables
        env: list<tuple<string, string>>,
        /// Buffer size for stdout/stderr (in bytes)
        buffer-size: u32,
        /// How to process stdout
        stdout-mode: output-mode,
        /// How to process stderr
        stderr-mode: output-mode,
        /// Chunk size for chunked mode (in bytes)
        chunk-size: option<u32>,
        /// Execution timeout in seconds (optional)
        execution-timeout: option<u64>,
    }

    /// Status of a running process
    record process-status {
        /// Process ID
        pid: u64,
        /// Whether the process is running
        running: bool,
        /// Exit code if not running (optional)
        exit-code: option<s32>,
        /// Start time in milliseconds since epoch
        start-time: u64,
    }

    /// Start a new OS process
    os-spawn: func(config: process-config) -> result<u64, string>;

    /// Write to the standard input of a process
    os-write-stdin: func(pid: u64, data: list<u8>) -> result<u32, string>;

    /// Get the status of a process
    os-status: func(pid: u64) -> result<process-status, string>;

    /// Send a signal to a process
    os-signal: func(pid: u64, signal: u32) -> result<_, string>;

    /// Terminate a process
    os-kill: func(pid: u64) -> result<_, string>;
}

/// Process handler export interface
interface process-handlers {
    /// Process output event
    handle-stdout: func(state: option<list<u8>>, params: tuple<u64, list<u8>>) -> result<tuple<option<list<u8>>>, string>;
    
    /// Process error output event
    handle-stderr: func(state: option<list<u8>>, params: tuple<u64, list<u8>>) -> result<tuple<option<list<u8>>>, string>;
    
    /// Process exit event
    handle-exit: func(state: option<list<u8>>, params: tuple<u64, s32>) -> result<tuple<option<list<u8>>>, string>;
}
