#![allow(clippy::result_large_err)]

//! typedialog - Interactive forms and prompts library
//!
//! A powerful library and CLI tool for creating interactive forms and prompts
//! with support for multiple rendering backends (CLI, TUI, Web).
//! Works with piped input for batch processing and scripts.
//!
//! # Features
//!
//! - 9 interactive prompt types (text, confirm, select, multi-select, password, custom, editor, date, form)
//! - TOML-based form definitions for declarative UI
//! - Multiple rendering backends: CLI (inquire), TUI (ratatui), Web (axum/HTMX)
//! - Stdin-based fallback for non-interactive contexts
//! - JSON/YAML output formats
//! - Nickel schema integration for configuration management
//! - Both library and CLI tool usage
//!
//! # Quick Start as Library
//!
//! ## Recommended: Using the Prelude
//!
//! ```no_run
//! use typedialog_core::prelude::*;
//! use typedialog_core::form_parser;
//!
//! # async fn example() -> Result<()> {
//! let mut backend = BackendFactory::create(BackendType::Cli)?;
//! let form = form_parser::parse_toml(r#"name = "User Registration""#)?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Simple Prompts with prompt_api
//!
//! ```no_run
//! use typedialog_core::prompt_api::{self, Result};
//!
//! fn example() -> Result<()> {
//!     let name = prompt_api::text("Enter your name", None, None)?;
//!     let confirmed = prompt_api::confirm("Continue?", Some(true), None)?;
//!     println!("Hello, {}!", name);
//!     Ok(())
//! }
//! ```
//!
//! ## Advanced: Custom Backends
//!
//! ```ignore
//! use typedialog_core::advanced::{FormBackend, RenderContext};
//!
//! // Implement your custom backend
//! struct MyBackend;
//!
//! #[async_trait::async_trait]
//! impl FormBackend for MyBackend {
//!     // ... implement required methods
//! }
//! ```
//!
//! # Module Organization
//!
//! - [`prelude`] - Common types for form execution (recommended starting point)
//! - [`prompt_api`] - Direct prompt functions without forms
//! - [`advanced`] - Advanced APIs for extending backends
//! - Individual modules: [`error`], [`backends`], [`form_parser`], [`helpers`], etc.
//!
//! # Quick Start as CLI
//!
//! ```bash
//! # Text prompt with piped input
//! echo "Alice" | typedialog text "Enter name"
//!
//! # Form execution
//! cat input.txt | typedialog form myform.toml
//!
//! # Selection
//! echo "Admin" | typedialog select "Choose role" Admin User Guest
//!
//! # Nickel integration
//! typedialog nickel-to-form schema.ncl -o form.toml --flatten
//! typedialog form-to-nickel form.toml results.json -o output.ncl --validate
//! ```

// ============================================================================
// CORE MODULES (always compiled)
// ============================================================================

pub mod error;
pub mod helpers;

/// Common CLI patterns and help text
pub mod cli_common;

// ============================================================================
// FOCUSED API MODULES (recommended entry points)
// ============================================================================

/// Prelude module for common form execution use cases
///
/// Import with `use typedialog_core::prelude::*;` for quick access.
pub mod prelude;

/// Prompt-only API for direct interactive prompts without forms
#[cfg(any(feature = "cli", feature = "tui", feature = "web"))]
pub mod prompt_api;

/// Advanced API for extending backends and custom implementations
#[cfg(any(feature = "cli", feature = "tui", feature = "web"))]
pub mod advanced;

// ============================================================================
// BACKEND MODULES (feature-gated)
// ============================================================================

#[cfg(any(feature = "cli", feature = "tui", feature = "web"))]
pub mod backends;

#[cfg(any(feature = "cli", feature = "tui", feature = "web"))]
pub mod form_parser;

#[cfg(any(feature = "cli", feature = "tui", feature = "web"))]
pub mod prompts;

// ============================================================================
// OPTIONAL FEATURE MODULES
// ============================================================================

#[cfg(any(feature = "cli", feature = "tui", feature = "web"))]
pub mod autocompletion;

#[cfg(any(feature = "cli", feature = "tui", feature = "web"))]
pub mod nickel;

#[cfg(feature = "ai_backend")]
pub mod ai;

#[cfg(feature = "i18n")]
pub mod config;

#[cfg(feature = "i18n")]
pub mod i18n;

#[cfg(feature = "templates")]
pub mod templates;

#[cfg(feature = "encryption")]
pub mod encryption_bridge;

#[cfg(feature = "encryption")]
pub use encrypt;

// ============================================================================
// RE-EXPORTS (backward compatibility - prefer prelude/prompt_api/advanced)
// ============================================================================

// Core exports (always available)
#[deprecated(
    since = "0.2.0",
    note = "Use `typedialog_core::prelude::*` or `error::Result` directly"
)]
pub use error::{Error, ErrorWrapper, Result};

#[deprecated(
    since = "0.2.0",
    note = "Use `typedialog_core::prelude::*` or `helpers::format_results` directly"
)]
pub use helpers::{format_results, to_json_string, to_json_value};

// Backend exports (require at least one backend)
#[cfg(any(feature = "cli", feature = "tui", feature = "web"))]
#[deprecated(
    since = "0.2.0",
    note = "Use `typedialog_core::advanced::*` or specific module imports"
)]
pub use autocompletion::{FilterCompleter, HistoryCompleter, PatternCompleter};

#[cfg(any(feature = "cli", feature = "tui", feature = "web"))]
#[deprecated(
    since = "0.2.0",
    note = "Use `typedialog_core::prelude::*` for BackendFactory/BackendType, or `advanced::*` for FormBackend/RenderContext"
)]
pub use backends::{BackendFactory, BackendType, FormBackend, RenderContext};

#[cfg(any(feature = "cli", feature = "tui", feature = "web"))]
#[deprecated(
    since = "0.2.0",
    note = "Use `typedialog_core::prelude::*` or `form_parser::*` directly"
)]
pub use form_parser::{DisplayItem, FieldDefinition, FieldType, FormDefinition};

// Optional feature exports
#[cfg(feature = "i18n")]
#[deprecated(
    since = "0.2.0",
    note = "Use `typedialog_core::prelude::TypeDialogConfig` or `config::TypeDialogConfig` directly"
)]
pub use config::TypeDialogConfig;

#[cfg(feature = "i18n")]
#[deprecated(
    since = "0.2.0",
    note = "Use `typedialog_core::prelude::*` or `i18n::*` directly"
)]
pub use i18n::{I18nBundle, LocaleResolver};

#[cfg(feature = "templates")]
#[deprecated(
    since = "0.2.0",
    note = "Use `typedialog_core::prelude::TemplateEngine` or `advanced::TemplateContextBuilder`"
)]
pub use templates::{TemplateContextBuilder, TemplateEngine};

/// Library version
pub const VERSION: &str = env!("CARGO_PKG_VERSION");

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_version() {
        // VERSION is compile-time constant from Cargo.toml, verify it's valid
        assert!(VERSION.chars().next().unwrap().is_ascii_digit());
        assert!(VERSION.contains('.'));
    }

    #[test]
    #[cfg(any(feature = "cli", feature = "tui", feature = "web"))]
    fn test_backend_factory_cli() {
        let result = BackendFactory::create(BackendType::Cli);
        #[cfg(feature = "cli")]
        assert!(result.is_ok());
        #[cfg(not(feature = "cli"))]
        assert!(result.is_err());
    }
}
