"""
Post-processing utilities for Matrice SDK.

This module provides a unified, clean interface for post-processing model outputs
with support for various use cases like people counting, customer service analysis,
and more.

Key Features:
- Unified PostProcessor class for all processing needs
- Built-in use case processors for common scenarios
- Flexible configuration management with JSON/YAML support
- Comprehensive validation and error handling
- Processing statistics and insights
- Zone-based analysis and tracking

Quick Start:
    from matrice.deploy.utils.post_processing import PostProcessor
    
    # Simple processing
    processor = PostProcessor()
    result = processor.process_simple(
        raw_results, "people_counting",
        confidence_threshold=0.5
    )
    
    # With configuration file
    result = processor.process_from_file(raw_results, "config.json")
    
    # Get available use cases
    usecases = processor.list_available_usecases()
"""

# Core components - main processing interface
from .processor import (
    PostProcessor,
    process_simple,
    create_config_template,
    list_available_usecases,
    validate_config
)

# Core data structures and base classes
from .core.base import (
    ProcessingResult,
    ProcessingContext,
    ProcessingStatus,
    ResultFormat,
    BaseProcessor,
    BaseUseCase,
    ProcessorRegistry,
    registry
)

# Configuration system
from .core.config import (
    BaseConfig,
    PeopleCountingConfig,
    CustomerServiceConfig,
    ZoneConfig,
    TrackingConfig,
    AlertConfig,
    ConfigManager,
    config_manager,
    ConfigValidationError
)

# Additional config imports
from .usecases.color_detection import ColorDetectionConfig
from .usecases.fire_detection import FireSmokeUseCase, FireSmokeConfig
from .usecases.license_plate_detection import LicensePlateConfig
from .usecases.pothole_segmentation import PotholeConfig
from .usecases.face_emotion import FaceEmotionConfig
from .usecases.parking_space_detection import ParkingSpaceConfig
from .usecases.underwater_pollution_detection import UnderwaterPlasticConfig
from .usecases.pedestrian_detection import PedestrianDetectionConfig
from .usecases.age_detection import AgeDetectionConfig
from .usecases.mask_detection import MaskDetectionConfig
from .usecases.chicken_pose_detection import ChickenPoseDetectionConfig


from .usecases.weld_defect_detection import WeldDefectConfig

from .usecases.car_damage_detection import CarDamageConfig
from .usecases.price_tag_detection import PriceTagConfig
from .usecases.banana_defect_detection import BananaMonitoringConfig
from .usecases.distracted_driver_detection import DistractedDriverConfig
from .usecases.emergency_vehicle_detection import EmergencyVehicleConfig
from .usecases.solar_panel import SolarPanelConfig

# Use case implementations
from .usecases import (
    PeopleCountingUseCase,
    CustomerServiceUseCase,
    AdvancedCustomerServiceUseCase,
    BasicCountingTrackingUseCase,
    LicensePlateUseCase,
    ColorDetectionUseCase,
    PPEComplianceUseCase,
    CarDamageDetectionUseCase,
    VehicleMonitoringUseCase,
    FireSmokeUseCase,
    MaskDetectionUseCase,
    ParkingSpaceUseCase,
    FlareAnalysisUseCase,
    PotholeSegmentationUseCase,
    FaceEmotionUseCase,
    UnderwaterPlasticUseCase,
    PedestrianDetectionUseCase,
    AgeDetectionUseCase,
    BananaMonitoringUseCase,
    WeldDefectUseCase,
    PriceTagUseCase,
    DistractedDriverUseCase,
    EmergencyVehicleUseCase,
    SolarPanelUseCase,
    ChickenPoseDetectionUseCase
)

# Register use cases automatically
_people_counting = PeopleCountingUseCase()
_customer_service = CustomerServiceUseCase()
_advanced_customer_service = AdvancedCustomerServiceUseCase()
_basic_counting_tracking = BasicCountingTrackingUseCase()
_license_plate = LicensePlateUseCase()
_color_detection = ColorDetectionUseCase()
_ppe_compliance = PPEComplianceUseCase()
_vehicle_monitoring = VehicleMonitoringUseCase()
_fire_detection = FireSmokeUseCase()
_flare_analysis = FlareAnalysisUseCase()
_pothole_segmentation = PotholeSegmentationUseCase()
_face_emotion = FaceEmotionUseCase()
_parking_space_detection = ParkingSpaceUseCase()
_underwater_pollution_detection = UnderwaterPlasticUseCase()
_pedestrian_detection = PedestrianDetectionUseCase()
_age_detection = AgeDetectionUseCase()
_mask_detection = MaskDetectionUseCase()
_banana_defect_detection = BananaMonitoringUseCase()
_chicken_pose_detection = ChickenPoseDetectionUseCase()

_weld_defect_detection = WeldDefectUseCase()
_pricetag_detection = PriceTagUseCase()
_car_damage = CarDamageDetectionUseCase()
_distracted_driver = DistractedDriverUseCase()
_emergency_vehicle_detection = EmergencyVehicleUseCase()
_solar_panel = SolarPanelUseCase()

registry.register_use_case(_people_counting.category, _people_counting.name, PeopleCountingUseCase)
registry.register_use_case(_customer_service.category, _customer_service.name, CustomerServiceUseCase)
registry.register_use_case(_advanced_customer_service.category, _advanced_customer_service.name, AdvancedCustomerServiceUseCase)
registry.register_use_case(_basic_counting_tracking.category, _basic_counting_tracking.name, BasicCountingTrackingUseCase)
registry.register_use_case(_license_plate.category, _license_plate.name, LicensePlateUseCase)
registry.register_use_case(_color_detection.category, _color_detection.name, ColorDetectionUseCase)
registry.register_use_case(_ppe_compliance.category, _ppe_compliance.name, PPEComplianceUseCase)
registry.register_use_case(_vehicle_monitoring.category,_vehicle_monitoring.name,VehicleMonitoringUseCase)
registry.register_use_case(_fire_detection.category,_fire_detection.name,FireSmokeUseCase)
registry.register_use_case(_flare_analysis.category,_flare_analysis.name,FlareAnalysisUseCase)
registry.register_use_case(_pothole_segmentation.category, _pothole_segmentation.name, PotholeSegmentationUseCase)
registry.register_use_case(_face_emotion.category, _face_emotion.name, FaceEmotionUseCase)
registry.register_use_case(_parking_space_detection.category, _parking_space_detection.name, ParkingSpaceUseCase )
registry.register_use_case(_underwater_pollution_detection.category, _underwater_pollution_detection.name, UnderwaterPlasticUseCase)
registry.register_use_case(_pedestrian_detection.category, _pedestrian_detection.name, PedestrianDetectionUseCase)
registry.register_use_case(_age_detection.category, _age_detection.name, AgeDetectionUseCase)
registry.register_use_case(_pricetag_detection.category, _pricetag_detection.name, PriceTagUseCase)
registry.register_use_case(_weld_defect_detection.category, _weld_defect_detection.name, WeldDefectUseCase  )
registry.register_use_case(_mask_detection.category, _mask_detection.name, MaskDetectionUseCase)
registry.register_use_case(_banana_defect_detection.category, _banana_defect_detection.name, BananaMonitoringUseCase)
registry.register_use_case(_chicken_pose_detection.category, _chicken_pose_detection.name, ChickenPoseDetectionUseCase)


registry.register_use_case(_car_damage.category, _car_damage.name, CarDamageDetectionUseCase)
registry.register_use_case(_distracted_driver.category, _distracted_driver.name, DistractedDriverUseCase)

registry.register_use_case(_emergency_vehicle_detection.category, _emergency_vehicle_detection.name, EmergencyVehicleUseCase)
registry.register_use_case(_solar_panel.category, _solar_panel.name, SolarPanelUseCase)

# Utility functions - organized by category
from .utils import (  # noqa: E402
    # Geometry utilities
    point_in_polygon,
    get_bbox_center,
    calculate_distance,
    calculate_bbox_overlap,
    calculate_iou,
    get_bbox_area,
    normalize_bbox,
    denormalize_bbox,
    line_segments_intersect,
    
    # Format utilities
    convert_to_coco_format,
    convert_to_yolo_format,
    convert_to_tracking_format,
    convert_detection_to_tracking_format,
    convert_tracking_to_detection_format,
    match_results_structure,
    
    # Filter utilities
    filter_by_confidence,
    filter_by_categories,
    calculate_bbox_fingerprint,
    clean_expired_tracks,
    remove_duplicate_detections,
    apply_category_mapping,
    filter_by_area,
    
    # Counting utilities
    count_objects_by_category,
    count_objects_in_zones,
    count_unique_tracks,
    calculate_counting_summary,
    
    # Tracking utilities
    track_objects_in_zone,
    detect_line_crossings,
    analyze_track_movements,
    filter_tracks_by_duration,
    
    # New utilities
    create_people_counting_config,
    create_customer_service_config,
    create_advanced_customer_service_config,
    create_basic_counting_tracking_config,
    create_zone_from_bbox,
    create_polygon_zone,
    create_config_from_template,
    validate_zone_polygon,
    get_use_case_examples,
    create_retail_store_zones,
    create_office_zones
)

# Convenience functions for backward compatibility and simple usage
def process_usecase(raw_results, usecase: str, category: str = "general", **config):
    """
    Process raw results with a specific use case.
    
    Args:
        raw_results: Raw model output
        usecase: Use case name ('people_counting', 'customer_service', etc.)
        category: Use case category (default: 'general')
        **config: Configuration parameters
        
    Returns:
        ProcessingResult: Processing result with insights
        
    Example:
        result = process_usecase(
            raw_results, "people_counting",
            confidence_threshold=0.5,
            zones={"entrance": [[0, 0], [100, 0], [100, 100], [0, 100]]}
        )
    """
    return process_simple(raw_results, usecase, category, **config)


def get_config_template(usecase: str) -> dict:
    """
    Get configuration template for a use case.
    
    Args:
        usecase: Use case name
        
    Returns:
        dict: Configuration template
    """
    return create_config_template(usecase)


def get_available_usecases() -> dict:
    """
    Get all available use cases organized by category.
    
    Returns:
        dict: Available use cases by category
    """
    return list_available_usecases()


def create_processor() -> PostProcessor:
    """
    Create a new PostProcessor instance.
    
    Returns:
        PostProcessor: New processor instance
    """
    return PostProcessor()



# Main exports for external use
__all__ = [
    # Main processor class
    'PostProcessor',
    
    # Core data structures
    'ProcessingResult',
    'ProcessingContext',
    'ProcessingStatus',
    'ResultFormat',
    
    # Configuration classes
    'BaseConfig',
    'PeopleCountingConfig', 
    'CustomerServiceConfig',
    'ColorDetectionConfig',
    'LicensePlateConfig',
    'MaskDetectionConfig',
    'CarDamageConfig',
    'ParkingSpaceConfig',
    'PotholeConfig',
    'VehicleMonitoringConfig',
    'ZoneConfig',
    'TrackingConfig',
    'AlertConfig',
    'ConfigManager',
    'config_manager',
    'ConfigValidationError',
    'FireSmokeConfig',
    'FlareAnalysisConfig',
    'FaceEmotionConfig',
    'UnderwaterPlasticConfig',
    'PedestrianDetectionConfig',
    'AgeDetectionConfig',
    'WeldDefectConfig',
    'PriceTagConfig',
    'BananaMonitoringConfig',
    'DistractedDriverConfig',
    'EmergencyVehicleConfig',
    'SolarPanelConfig',
    'ChickenPoseDetectionConfig',

    # Use case classes
    'PeopleCountingUseCase',
    'CustomerServiceUseCase',
    'AdvancedCustomerServiceUseCase',
    'BasicCountingTrackingUseCase',
    'LicensePlateUseCase',
    'ColorDetectionUseCase',
    'PPEComplianceUseCase',
    'PotholeSegmentationUseCase',
    'MaskDetectionUseCase',
    'VehicleMonitoringUseCase',
    'FireSmokeUseCase',
    'CarDamageDetectionUseCase',
    'ParkingSpaceUseCase',
    'FlareAnalysisUseCase',
    'FaceEmotionUseCase',
    'UnderwaterPlasticUseCase',
    'PedestrianDetectionUseCase',
    'AgeDetectionUseCase',
    'WeldDefectUseCase',
    'BananaMonitoringUseCase',
    'PriceTagUseCase',
    'DistractedDriverUseCase',
    'EmergencyVehicleUseCase',
    'SolarPanelUseCase',
    'ChickenPoseDetectionUseCase',
    
    # Base classes for extension
    'BaseProcessor',
    'BaseUseCase',
    'ProcessorRegistry',
    'registry',
    
    # Convenience functions
    'process_simple',
    'process_usecase',
    'create_config_template',
    'get_config_template',
    'list_available_usecases',
    'get_available_usecases',
    'validate_config',
    'create_processor',
    
    # Geometry utilities
    'point_in_polygon',
    'get_bbox_center',
    'calculate_distance',
    'calculate_bbox_overlap',
    'calculate_iou',
    'get_bbox_area',
    'normalize_bbox',
    'denormalize_bbox',
    'line_segments_intersect',
    
    # Format utilities
    'convert_to_coco_format',
    'convert_to_yolo_format',
    'convert_to_tracking_format',
    'convert_detection_to_tracking_format',
    'convert_tracking_to_detection_format',
    'match_results_structure',
    
    # Filter utilities
    'filter_by_confidence',
    'filter_by_categories',
    'calculate_bbox_fingerprint',
    'clean_expired_tracks',
    'remove_duplicate_detections',
    'apply_category_mapping',
    'filter_by_area',
    
    # Counting utilities
    'count_objects_by_category',
    'count_objects_in_zones',
    'count_unique_tracks',
    'calculate_counting_summary',
    
    # Tracking utilities
    'track_objects_in_zone',
    'detect_line_crossings',
    'analyze_track_movements',
    'filter_tracks_by_duration',
    
    # New utilities
    'create_people_counting_config',
    'create_customer_service_config',
    'create_advanced_customer_service_config',
    'create_basic_counting_tracking_config',
    'create_zone_from_bbox',
    'create_polygon_zone',
    'create_config_from_template',
    'validate_zone_polygon',
    'get_use_case_examples',
    'create_retail_store_zones',
    'create_office_zones',
    
    # Functions
    'list_available_usecases',
    'create_config_from_template'
]
