"""
Spark Simplicity - Enterprise Connection Management Package
==========================================================

Comprehensive enterprise connection management suite providing secure, high-performance
connectivity to external systems for Spark data processing workflows. This package delivers
production-ready connection managers with singleton patterns, automatic retry logic, and
comprehensive error handling for seamless integration with corporate infrastructure.

**Supported Connection Types:**
    - **Database Connectivity**: Enterprise JDBC connections with SQL Server optimization
    - **SFTP File Transfer**: Secure file transfer with automatic retry and directory management
    - **REST API Integration**: HTTP/HTTPS API connectivity with intelligent retry strategies
    - **Email Communication**: SMTP email delivery with SSL/TLS encryption and bulk capabilities

**Core Connection Managers:**
    **Database Connections** (``JdbcSqlServerConnection``):
    - **SQL Server Optimization**: Specialized JDBC configuration for Microsoft SQL Server
    - **Connection Pooling**: Singleton pattern with efficient resource management
    - **Query Performance**: Optimized fetch sizes and configurable query partitioning
    - **Security Features**: Encrypted connections with certificate validation
    - **Enterprise Ready**: Production-grade error handling and monitoring integration

    **SFTP Connections** (``SftpConnection``):
    - **Secure File Transfer**: SSH-based encrypted file operations with authentication
    - **Automatic Retry**: Exponential backoff for transient network issues
    - **Directory Management**: Automatic directory creation and file system operations
    - **Connection Reuse**: Singleton pattern for efficient resource utilization
    - **Production Safety**: Comprehensive error handling and connection lifecycle management

    **REST API Connections** (``RestApiConnection``):
    - **HTTP Method Support**: Complete REST API method coverage (GET, POST, PUT, PATCH, DELETE)
    - **Intelligent Retry**: Configurable retry strategies with exponential backoff
    - **Session Management**: Persistent HTTP sessions for optimal connection reuse
    - **Authentication**: Flexible authentication mechanisms (token, basic, custom)
    - **Response Handling**: Intelligent content-type detection and parsing

    **Email Connections** (``EmailSender``):
    - **Secure SMTP**: SSL/TLS encrypted email delivery with certificate validation
    - **Message Formats**: Plain text, HTML, and attachment support
    - **Bulk Processing**: Mass email campaigns with personalization capabilities
    - **Enterprise Integration**: Compatible with corporate email systems and gateways
    - **Delivery Tracking**: Comprehensive error handling and status reporting

**Enterprise Architecture Features:**
    **Singleton Pattern Implementation**:
    - One connection instance per unique configuration (host, port, credentials)
    - Efficient resource utilization preventing connection proliferation
    - Automatic connection reuse across multiple operations
    - Thread-safe connection management for concurrent access
    - Memory optimization through intelligent connection lifecycle management

    **Security & Compliance**:
    - **Encrypted Connections**: SSL/TLS encryption for all network communication
    - **Certificate Validation**: Configurable certificate verification for enterprise security
    - **Credential Protection**: Secure handling of authentication credentials
    - **Enterprise Standards**: Compliance with corporate security policies and standards
    - **Audit Logging**: Comprehensive logging for operational monitoring and compliance

    **Performance Optimization**:
    - **Connection Pooling**: Efficient reuse of established connections
    - **Retry Strategies**: Intelligent retry logic with exponential backoff
    - **Resource Management**: Automatic cleanup and connection lifecycle control
    - **Network Optimization**: Optimized settings for enterprise network environments
    - **Memory Efficiency**: Minimized resource footprint with smart caching strategies

**Integration Patterns:**
    **ETL Pipeline Integration**:
    - Seamless data extraction from enterprise databases and APIs
    - Reliable file transfer for data ingestion and export workflows
    - Email notifications for pipeline status and error reporting
    - Centralized connection management for complex data workflows

    **Data Processing Workflows**:
    - High-performance database connectivity for analytical processing
    - API integration for real-time data enrichment and validation
    - Secure file operations for distributed data processing
    - Automated email reporting for business intelligence and monitoring

    **Enterprise System Integration**:
    - Corporate database connectivity (SQL Server, Oracle, PostgreSQL)
    - Legacy system integration through SFTP and file-based workflows
    - Modern API integration for microservices and web service connectivity
    - Email integration for business process automation and notifications

**Connection Selection Guide:**
    **Database Operations** - Use ``JdbcSqlServerConnection`` for:
    - Data warehouse and OLAP system connectivity
    - Transactional system data extraction
    - Complex analytical queries and reporting
    - High-volume data processing with SQL Server databases

    **File Transfer Operations** - Use ``SftpConnection`` for:
    - Secure file exchange with external systems
    - Legacy system integration requiring file-based data transfer
    - Batch processing with file-based input/output
    - Automated file distribution and collection workflows

    **API Integration** - Use ``RestApiConnection`` for:
    - Modern web service and microservice integration
    - Real-time data enrichment and validation
    - RESTful API consumption and data synchronization
    - External service integration for data processing workflows

    **Email Communication** - Use ``EmailSender`` for:
    - Automated notification and alerting systems
    - Report distribution and business intelligence delivery
    - Operational monitoring and error notification
    - Customer communication and business process automation

**Usage Patterns:**
    Standard connection establishment and usage:

     from spark_simplicity.connections import (
    ...     JdbcSqlServerConnection,
    ...     SftpConnection,
    ...     RestApiConnection,
    ...     EmailSender
    ... )

     # Database connectivity for data processing
     db_config = {
    ...     'host': 'sqlserver.company.com',
    ...     'database': 'datawarehouse',
    ...     'user': 'spark_user',
    ...     'password': 'secure_password'
    ... }
     db_conn = JdbcSqlServerConnection(spark, db_config, logger)
     data_df = db_conn.query("SELECT * FROM sales_data WHERE date >= '2024-01-01'")

    Multi-system integration workflow:

     # Combine multiple connection types for complete workflow

     # 1. Extract data from database
     sales_data = db_conn.query("SELECT * FROM daily_sales")

     # 2. Enrich with external API data
     api_conn = RestApiConnection(spark, api_config, logger)
     enrichment_data = api_conn.get('/customer-segments')

     # 3. Process and export results
     processed_data = sales_data.join(enrichment_data, 'customer_id')

     # 4. Transfer files via SFTP
     sftp_conn = SftpConnection(spark, sftp_config, logger)
     sftp_conn.put('processed_results.csv', '/remote/reports/')

     # 5. Send email notifications
     email_sender = EmailSender(**email_config)
     email_sender.send_simple_email(
    ...     'team@company.com',
    ...     'Data Processing Complete',
    ...     'Daily sales processing completed successfully.'
    ... )

    Enterprise production deployment:

     # Production-ready configuration with error handling
     try:
    ...     # Initialize connections with comprehensive configuration
    ...     db_conn = JdbcSqlServerConnection(spark, db_config, logger)
    ...
    ...     # Execute business logic with connection reuse
    ...     quarterly_results = db_conn.query(quarterly_query, partitioning_config)
    ...
    ...     # Process results and distribute via multiple channels
    ...     process_and_distribute(quarterly_results, sftp_conn, email_sender)
    ...
    ... except Exception as e:
    ...     logger.error(f"Workflow execution failed: {e}")
    ...     email_sender.send_simple_email(admin_email, 'ALERT: Processing Failed', str(e))
    ...     raise

**Performance Considerations:**
    **Connection Efficiency**: Singleton pattern minimizes connection overhead
    **Resource Management**: Automatic cleanup prevents resource leaks
    **Network Optimization**: Intelligent retry and timeout strategies
    **Memory Usage**: Optimized for large-scale data processing workflows
    **Concurrent Access**: Thread-safe implementations for parallel processing

**Security Best Practices:**
    **Credential Management**: Never hard-code credentials; use secure configuration
    **Network Security**: Always use encrypted connections in production environments
    **Certificate Validation**: Enable certificate verification for production deployments
    **Access Control**: Implement proper authentication and authorization controls
    **Audit Logging**: Monitor all connection activities for security compliance

**Operational Monitoring:**
    All connection managers provide comprehensive logging for:
    - Connection establishment and lifecycle events
    - Performance metrics and resource utilization
    - Error conditions and retry attempts
    - Security events and authentication activities
    - Operational statistics for performance optimization

See Also:
    - Session management: ``spark_simplicity.session`` for optimized Spark configuration
    - I/O operations: ``spark_simplicity.io`` for data reading and writing capabilities
    - Utilities: ``spark_simplicity.utils`` for DataFrame operations and optimization
    - Logging: ``spark_simplicity.logger`` for centralized logging configuration

Note:
    This connections package provides the foundation for all external system integration
    in Spark Simplicity, with each connection manager optimized for its specific protocol
    while maintaining consistent interfaces and enterprise-grade reliability suitable
    for both development and production environments.
"""

from .database_connection import JdbcSqlServerConnection
from .email_connection import EmailSender
from .rest_api_connection import RestApiConnection
from .sftp_connection import SftpConnection

__all__ = [
    "EmailSender",
    "JdbcSqlServerConnection",
    "RestApiConnection",
    "SftpConnection",
]
