//! Core functionality for Topaz Video AI integration

pub mod ffmpeg;
pub mod models;
pub mod processor;

// Re-export main types
pub use processor::{TvaiProcessor, TvaiConfig, ProcessResult, ProcessMetadata, ProgressCallback, ProcessingOptions};
pub use ffmpeg::FfmpegManager;

use thiserror::Error;

/// Main error type for the tvai library
#[derive(Error, Debug)]
pub enum TvaiError {
    #[error("FFmpeg not found: {0}")]
    FfmpegNotFound(String),
    
    #[error("FFmpeg execution failed: {0}")]
    FfmpegError(String),
    
    #[error("Topaz Video AI not found at path: {0}")]
    TopazNotFound(String),
    
    #[error("Invalid parameter: {0}")]
    InvalidParameter(String),
    
    #[error("File not found: {0}")]
    FileNotFound(String),
    
    #[error("IO error: {0}")]
    IoError(#[from] std::io::Error),
    
    #[error("Processing failed: {0}")]
    ProcessingError(String),

    #[error("GPU not available")]
    GpuNotAvailable,

    #[error("Unsupported format: {0}")]
    UnsupportedFormat(String),

    #[error("Configuration error: {0}")]
    ConfigurationError(String),

    #[error("GPU error: {0}")]
    GpuError(String),

    #[error("Insufficient resources: {0}")]
    InsufficientResources(String),

    #[error("Operation cancelled: {0}")]
    OperationCancelled(String),

    #[error("Network error: {0}")]
    NetworkError(String),

    #[error("Permission denied: {0}")]
    PermissionDenied(String),
}

impl TvaiError {
    /// Get user-friendly error message with suggestions
    pub fn user_friendly_message(&self) -> String {
        match self {
            TvaiError::TopazNotFound(path) => {
                format!(
                    "Topaz Video AI not found at '{}'.\n\
                    Suggestions:\n\
                    • Install Topaz Video AI from https://www.topazlabs.com/\n\
                    • Check if the installation path is correct\n\
                    • Try running with administrator privileges",
                    path
                )
            }
            TvaiError::FfmpegError(msg) => {
                format!(
                    "FFmpeg processing error: {}\n\
                    Suggestions:\n\
                    • Check if the input file is valid and not corrupted\n\
                    • Ensure sufficient disk space for output\n\
                    • Try with different quality settings\n\
                    • Check if GPU drivers are up to date",
                    msg
                )
            }
            TvaiError::InvalidParameter(msg) => {
                format!(
                    "Invalid parameter: {}\n\
                    Suggestions:\n\
                    • Check the parameter values and ranges\n\
                    • Refer to the documentation for valid options\n\
                    • Use preset configurations for tested settings",
                    msg
                )
            }
            TvaiError::GpuError(msg) => {
                format!(
                    "GPU error: {}\n\
                    Suggestions:\n\
                    • Update GPU drivers to the latest version\n\
                    • Try disabling GPU acceleration\n\
                    • Check if GPU has sufficient memory\n\
                    • Restart the application",
                    msg
                )
            }
            TvaiError::UnsupportedFormat(format) => {
                format!(
                    "Unsupported file format: {}\n\
                    Supported formats:\n\
                    • Video: MP4, AVI, MOV, MKV, WMV\n\
                    • Image: PNG, JPG, TIFF, BMP\n\
                    Suggestion: Convert your file to a supported format first",
                    format
                )
            }
            TvaiError::InsufficientResources(msg) => {
                format!(
                    "Insufficient resources: {}\n\
                    Suggestions:\n\
                    • Close other applications to free up memory\n\
                    • Use lower quality settings\n\
                    • Process smaller files or segments\n\
                    • Ensure sufficient disk space",
                    msg
                )
            }
            TvaiError::PermissionDenied(msg) => {
                format!(
                    "Permission denied: {}\n\
                    Suggestions:\n\
                    • Run the application as administrator\n\
                    • Check file and folder permissions\n\
                    • Ensure the output directory is writable\n\
                    • Close any applications using the files",
                    msg
                )
            }
            _ => self.to_string(),
        }
    }

    /// Get error category for logging and analytics
    pub fn category(&self) -> &'static str {
        match self {
            TvaiError::FfmpegNotFound(_) => "installation",
            TvaiError::FfmpegError(_) => "processing",
            TvaiError::TopazNotFound(_) => "installation",
            TvaiError::InvalidParameter(_) => "parameter",
            TvaiError::FileNotFound(_) => "io",
            TvaiError::IoError(_) => "io",
            TvaiError::ProcessingError(_) => "processing",
            TvaiError::GpuNotAvailable => "gpu",
            TvaiError::UnsupportedFormat(_) => "format",
            TvaiError::ConfigurationError(_) => "configuration",
            TvaiError::GpuError(_) => "gpu",
            TvaiError::InsufficientResources(_) => "resources",
            TvaiError::OperationCancelled(_) => "cancelled",
            TvaiError::NetworkError(_) => "network",
            TvaiError::PermissionDenied(_) => "permission",
        }
    }

    /// Check if error is recoverable
    pub fn is_recoverable(&self) -> bool {
        match self {
            TvaiError::FfmpegNotFound(_) => false,
            TvaiError::FfmpegError(_) => true, // Might work with different settings
            TvaiError::TopazNotFound(_) => false,
            TvaiError::InvalidParameter(_) => true,
            TvaiError::FileNotFound(_) => false,
            TvaiError::IoError(_) => false,
            TvaiError::ProcessingError(_) => true,
            TvaiError::GpuNotAvailable => true, // Can fallback to CPU
            TvaiError::UnsupportedFormat(_) => false,
            TvaiError::ConfigurationError(_) => true,
            TvaiError::GpuError(_) => true, // Can fallback to CPU
            TvaiError::InsufficientResources(_) => true,
            TvaiError::OperationCancelled(_) => true,
            TvaiError::NetworkError(_) => true,
            TvaiError::PermissionDenied(_) => false,
        }
    }
}
