//! Mobile platform definitions and configurations
//!
//! This module provides comprehensive platform specifications for mobile deployment including:
//! - iOS and Android platform configurations
//! - Device type definitions and architecture support
//! - Platform-specific optimization settings
//! - Hardware acceleration configurations (Metal, NNAPI, Core ML)
//! - Mobile optimization parameters (quantization, compression, power, thermal)

use std::fmt::Debug;
/// Mobile platform specification
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum MobilePlatform {
    /// iOS platform
    IOS {
        /// Minimum iOS version
        min_version: String,
        /// Target device types
        devices: Vec<IOSDevice>,
    },
    /// Android platform
    Android {
        /// Minimum API level
        min_api_level: u32,
        /// Target architectures
        architectures: Vec<AndroidArchitecture>,
    /// Universal mobile package
    Universal {
        /// iOS configuration
        ios_config: Box<Option<IOSConfig>>,
        /// Android configuration
        android_config: Box<Option<AndroidConfig>>,
}
/// iOS device types
pub enum IOSDevice {
    /// iPhone devices
    IPhone,
    /// iPad devices
    IPad,
    /// Apple TV
    AppleTV,
    /// Apple Watch
    AppleWatch,
    /// Mac with Apple Silicon
    MacAppleSilicon,
/// Android architecture support
pub enum AndroidArchitecture {
    /// ARM64-v8a (64-bit ARM)
    ARM64,
    /// ARMv7a (32-bit ARM)
    ARMv7,
    /// x86_64 (Intel/AMD 64-bit)
    X86_64,
    /// x86 (Intel/AMD 32-bit)
    X86,
/// iOS-specific configuration
pub struct IOSConfig {
    /// Framework bundle identifier
    pub bundle_identifier: String,
    /// Framework version
    pub version: String,
    /// Code signing configuration
    pub code_signing: CodeSigningConfig,
    /// Metal Performance Shaders usage
    pub metal_config: MetalConfig,
    /// Core ML integration
    pub core_ml: CoreMLConfig,
    /// Privacy configuration
    pub privacy_config: PrivacyConfig,
/// Code signing configuration for iOS
pub struct CodeSigningConfig {
    /// Development team ID
    pub team_id: Option<String>,
    /// Code signing identity
    pub identity: Option<String>,
    /// Provisioning profile
    pub provisioning_profile: Option<String>,
    /// Automatic signing
    pub automatic_signing: bool,
/// Metal Performance Shaders configuration
pub struct MetalConfig {
    /// Enable Metal acceleration
    pub enable: bool,
    /// Use Metal Performance Shaders
    pub use_mps: bool,
    /// Custom Metal kernels
    pub custom_kernels: Vec<MetalKernel>,
    /// Memory optimization
    pub memory_optimization: MetalMemoryOptimization,
/// Metal kernel specification
pub struct MetalKernel {
    /// Kernel name
    pub name: String,
    /// Kernel source code
    pub source: String,
    /// Kernel function name
    pub function_name: String,
    /// Thread group size
    pub thread_group_size: (u32, u32, u32),
/// Metal memory optimization settings
pub struct MetalMemoryOptimization {
    /// Use unified memory
    pub unified_memory: bool,
    /// Buffer pooling
    pub buffer_pooling: bool,
    /// Texture compression
    pub texture_compression: bool,
    /// Memory warnings handling
    pub memory_warnings: bool,
/// Core ML integration configuration
pub struct CoreMLConfig {
    /// Enable Core ML integration
    /// Core ML model format version
    pub model_version: CoreMLVersion,
    /// Compute units preference
    pub compute_units: CoreMLComputeUnits,
    /// Model compilation options
    pub compilation_options: CoreMLCompilationOptions,
/// Core ML model format version
pub enum CoreMLVersion {
    /// Core ML 1.0
    V1_0,
    /// Core ML 2.0
    V2_0,
    /// Core ML 3.0
    V3_0,
    /// Core ML 4.0
    V4_0,
    /// Core ML 5.0
    V5_0,
    /// Core ML 6.0
    V6_0,
/// Core ML compute units preference
pub enum CoreMLComputeUnits {
    /// CPU only
    CPUOnly,
    /// CPU and GPU
    CPUAndGPU,
    /// All available units
    All,
    /// CPU and Neural Engine
    CPUAndNeuralEngine,
/// Core ML compilation options
pub struct CoreMLCompilationOptions {
    /// Optimization level
    pub optimization_level: OptimizationLevel,
    /// Precision mode
    pub precision: PrecisionMode,
    /// Specialization
    pub specialization: SpecializationMode,
/// Privacy configuration for iOS
pub struct PrivacyConfig {
    /// Privacy manifest requirements
    pub privacy_manifest: bool,
    /// Data collection description
    pub data_collection: Vec<DataCollection>,
    /// Required permissions
    pub permissions: Vec<Permission>,
/// Data collection description
pub struct DataCollection {
    /// Data type
    pub data_type: String,
    /// Collection purpose
    pub purpose: String,
    /// Is tracking
    pub is_tracking: bool,
/// iOS permission requirement
pub enum Permission {
    /// Camera access
    Camera,
    /// Microphone access
    Microphone,
    /// Photo library access
    PhotoLibrary,
    /// Location access
    Location,
    /// Neural Engine access
    NeuralEngine,
    /// Background processing
    BackgroundProcessing,
/// Android-specific configuration
pub struct AndroidConfig {
    /// Package name
    pub package_name: String,
    /// Version code
    pub version_code: u32,
    /// Version name
    pub version_name: String,
    /// NNAPI configuration
    pub nnapi_config: NNAPIConfig,
    /// GPU delegate configuration
    pub gpu_config: AndroidGPUConfig,
    /// ProGuard/R8 configuration
    pub obfuscation: ObfuscationConfig,
    /// Permissions configuration
    pub permissions: AndroidPermissionsConfig,
/// Android Neural Networks API configuration
pub struct NNAPIConfig {
    /// Enable NNAPI acceleration
    /// Minimum NNAPI version
    pub min_version: u32,
    /// Preferred execution providers
    pub execution_providers: Vec<NNAPIProvider>,
    /// Fallback strategy
    pub fallback_strategy: NNAPIFallback,
/// NNAPI execution provider
pub enum NNAPIProvider {
    /// CPU execution
    CPU,
    /// GPU execution
    GPU,
    /// DSP execution
    DSP,
    /// NPU execution
    NPU,
    /// Vendor-specific
    Vendor(String),
/// NNAPI fallback strategy
pub enum NNAPIFallback {
    /// Fast fallback to CPU
    Fast,
    /// Try all available providers
    Comprehensive,
    /// Custom fallback order
    Custom(Vec<NNAPIProvider>),
/// Android GPU delegate configuration
pub struct AndroidGPUConfig {
    /// Enable GPU acceleration
    /// OpenGL ES version
    pub opengl_version: OpenGLVersion,
    /// Vulkan support
    pub vulkan_support: bool,
    /// GPU memory management
    pub memory_management: GPUMemoryManagement,
/// OpenGL ES version
pub enum OpenGLVersion {
    /// OpenGL ES 2.0
    ES2_0,
    /// OpenGL ES 3.0
    ES3_0,
    /// OpenGL ES 3.1
    ES3_1,
    /// OpenGL ES 3.2
    ES3_2,
/// GPU memory management strategy
pub struct GPUMemoryManagement {
    /// Texture caching
    pub texture_caching: bool,
    /// Memory pressure handling
    pub memory_pressure_handling: bool,
    /// Maximum memory usage (MB)
    pub max_memory_mb: Option<u32>,
/// Code obfuscation configuration
pub struct ObfuscationConfig {
    /// Enable obfuscation
    /// Obfuscation tool
    pub tool: ObfuscationTool,
    /// Keep rules for model classes
    pub keep_rules: Vec<String>,
    pub optimization_level: u8,
/// Obfuscation tool selection
pub enum ObfuscationTool {
    /// ProGuard
    ProGuard,
    /// R8 (recommended)
    R8,
    /// DexGuard
    DexGuard,
/// Android permissions configuration
pub struct AndroidPermissionsConfig {
    pub required: Vec<AndroidPermission>,
    /// Optional permissions
    pub optional: Vec<AndroidPermission>,
    /// Runtime permissions
    pub runtime: Vec<AndroidPermission>,
/// Android permission types
pub enum AndroidPermission {
    /// Internet access
    Internet,
    RecordAudio,
    /// External storage
    WriteExternalStorage,
    /// Read external storage
    ReadExternalStorage,
    /// Wake lock
    WakeLock,
    /// Foreground service
    ForegroundService,
    /// Custom permission
    Custom(String),
/// Mobile optimization configuration
#[derive(Debug, Clone, PartialEq)]
pub struct MobileOptimizationConfig {
    /// Model compression settings
    pub compression: MobileCompressionConfig,
    /// Quantization settings
    pub quantization: MobileQuantizationConfig,
    pub memory: MobileMemoryConfig,
    /// Power management
    pub power: PowerManagementConfig,
    /// Thermal management
    pub thermal: ThermalManagementConfig,
/// Mobile-specific compression configuration
pub struct MobileCompressionConfig {
    /// Pruning strategy
    pub pruning: MobilePruningStrategy,
    /// Knowledge distillation
    pub distillation: MobileDistillationConfig,
    /// Weight sharing
    pub weight_sharing: bool,
    /// Layer fusion
    pub layer_fusion: bool,
/// Mobile pruning strategy
pub struct MobilePruningStrategy {
    /// Pruning type
    pub pruning_type: PruningType,
    /// Sparsity level
    pub sparsity_level: f64,
    /// Structured pruning
    pub structured: bool,
    /// Hardware-aware pruning
    pub hardware_aware: bool,
/// Pruning type for mobile deployment
pub enum PruningType {
    /// Magnitude-based pruning
    Magnitude,
    /// Gradient-based pruning
    Gradient,
    /// Fisher information pruning
    Fisher,
    /// Lottery ticket hypothesis
    LotteryTicket,
/// Mobile distillation configuration
pub struct MobileDistillationConfig {
    /// Enable distillation
    /// Teacher model complexity
    pub teacher_complexity: TeacherComplexity,
    /// Distillation temperature
    pub temperature: f64,
    /// Loss weighting
    pub loss_weighting: DistillationWeighting,
/// Teacher model complexity for distillation
pub enum TeacherComplexity {
    /// Use desktop model as teacher
    Desktop,
    /// Use cloud model as teacher
    Cloud,
    /// Use ensemble as teacher
    Ensemble,
    /// Progressive distillation
    Progressive,
/// Distillation loss weighting
pub struct DistillationWeighting {
    /// Knowledge distillation weight
    pub knowledge_weight: f64,
    /// Ground truth weight
    pub ground_truth_weight: f64,
    /// Feature distillation weight
    pub feature_weight: f64,
/// Mobile quantization configuration
pub struct MobileQuantizationConfig {
    /// Quantization strategy
    pub strategy: QuantizationStrategy,
    /// Bit precision
    pub precision: QuantizationPrecision,
    /// Calibration method
    pub calibration: CalibrationMethod,
    /// Hardware acceleration
    pub hardware_acceleration: bool,
/// Quantization strategy for mobile
pub enum QuantizationStrategy {
    /// Post-training quantization
    PostTraining,
    /// Quantization-aware training
    QAT,
    /// Dynamic quantization
    Dynamic,
    /// Mixed precision
    MixedPrecision,
/// Quantization precision levels
pub struct QuantizationPrecision {
    /// Weight precision (bits)
    pub weights: u8,
    /// Activation precision (bits)
    pub activations: u8,
    /// Bias precision (bits)
    pub bias: Option<u8>,
/// Calibration method for quantization
pub enum CalibrationMethod {
    /// Entropy-based calibration
    Entropy,
    /// Percentile-based calibration
    Percentile,
    /// MSE-based calibration
    MSE,
    /// KL-divergence calibration
    KLDivergence,
/// Mobile memory optimization configuration
pub struct MobileMemoryConfig {
    /// Memory pool strategy
    pub pool_strategy: MemoryPoolStrategy,
    /// Buffer management
    pub buffer_management: BufferManagementConfig,
    /// Memory mapping
    pub memory_mapping: MemoryMappingConfig,
    /// Garbage collection optimization
    pub gc_optimization: GCOptimizationConfig,
/// Memory pool strategy for mobile
pub enum MemoryPoolStrategy {
    /// Fixed-size pools
    Fixed,
    /// Dynamic pools
    /// Buddy allocator
    Buddy,
    /// Slab allocator
    Slab,
/// Buffer management configuration
pub struct BufferManagementConfig {
    pub pooling: bool,
    /// Buffer alignment
    pub alignment: u32,
    /// Prefault pages
    pub prefault: bool,
    /// Memory advice
    pub memory_advice: MemoryAdvice,
/// Memory advice for buffer management
pub enum MemoryAdvice {
    /// Normal access pattern
    Normal,
    /// Sequential access
    Sequential,
    /// Random access
    Random,
    /// Will need soon
    WillNeed,
    /// Don't need anymore
    DontNeed,
/// Memory mapping configuration
pub struct MemoryMappingConfig {
    /// Use memory mapping for model weights
    /// Map private or shared
    pub map_private: bool,
    /// Lock pages in memory
    pub lock_pages: bool,
    /// Huge pages support
    pub huge_pages: bool,
/// Garbage collection optimization
pub struct GCOptimizationConfig {
    /// Minimize allocations
    pub minimize_allocations: bool,
    /// Object pooling
    pub object_pooling: bool,
    /// Weak references
    pub weak_references: bool,
    /// Manual memory management
    pub manual_management: bool,
/// Power management configuration
pub struct PowerManagementConfig {
    /// Power mode selection
    pub power_mode: PowerMode,
    /// CPU frequency scaling
    pub cpu_scaling: CPUScalingConfig,
    /// GPU power management
    pub gpu_power: GPUPowerConfig,
    /// Battery optimization
    pub battery_optimization: BatteryOptimizationConfig,
/// Power mode for inference
pub enum PowerMode {
    /// Maximum performance
    Performance,
    /// Balanced mode
    Balanced,
    /// Power saving mode
    PowerSave,
    /// Adaptive mode
    Adaptive,
/// CPU frequency scaling configuration
pub struct CPUScalingConfig {
    /// Governor type
    pub governor: CPUGovernor,
    /// Minimum frequency
    pub min_frequency: Option<u32>,
    /// Maximum frequency
    pub max_frequency: Option<u32>,
    /// Performance cores preference
    pub performance_cores: bool,
/// CPU governor type
pub enum CPUGovernor {
    /// Performance governor
    /// Powersave governor
    Powersave,
    /// OnDemand governor
    OnDemand,
    /// Conservative governor
    Conservative,
    /// Interactive governor
    Interactive,
    /// Schedutil governor
    Schedutil,
/// GPU power management configuration
pub struct GPUPowerConfig {
    /// GPU frequency scaling
    pub frequency_scaling: bool,
    /// Dynamic voltage scaling
    pub voltage_scaling: bool,
    /// GPU idle timeout
    pub idle_timeout_ms: u32,
    /// Power gating
    pub power_gating: bool,
/// Battery optimization configuration
pub struct BatteryOptimizationConfig {
    /// Battery level monitoring
    pub level_monitoring: bool,
    /// Adaptive inference frequency
    pub adaptive_frequency: bool,
    /// Low battery mode
    pub low_battery_mode: LowBatteryMode,
    /// Charging state awareness
    pub charging_awareness: bool,
/// Low battery mode configuration
pub struct LowBatteryMode {
    /// Battery threshold percentage
    pub threshold_percentage: u8,
    /// Reduced precision
    pub reduced_precision: bool,
    /// Skip non-critical inference
    pub skip_non_critical: bool,
    /// Suspend background processing
    pub suspend_background: bool,
/// Thermal management configuration
pub struct ThermalManagementConfig {
    /// Thermal monitoring
    pub monitoring: ThermalMonitoringConfig,
    /// Throttling strategy
    pub throttling: ThermalThrottlingConfig,
    /// Cooling strategies
    pub cooling: CoolingConfig,
/// Thermal monitoring configuration
pub struct ThermalMonitoringConfig {
    /// Enable thermal monitoring
    /// Temperature sensors
    pub sensors: Vec<ThermalSensor>,
    /// Monitoring frequency
    pub frequency_ms: u32,
    /// Temperature thresholds
    pub thresholds: ThermalThresholds,
/// Thermal sensor types
pub enum ThermalSensor {
    /// CPU temperature
    /// GPU temperature
    /// Battery temperature
    Battery,
    /// System temperature
    System,
    /// Custom sensor
/// Temperature thresholds for thermal management
pub struct ThermalThresholds {
    /// Warning temperature (°C)
    pub warning: f32,
    /// Critical temperature (°C)
    pub critical: f32,
    /// Emergency temperature (°C)
    pub emergency: f32,
/// Thermal throttling configuration
pub struct ThermalThrottlingConfig {
    /// Enable throttling
    pub strategy: ThrottlingStrategy,
    /// Performance degradation steps
    pub degradation_steps: Vec<PerformanceDegradation>,
/// Thermal throttling strategy
pub enum ThrottlingStrategy {
    /// Linear throttling
    Linear,
    /// Exponential throttling
    Exponential,
    /// Step-wise throttling
    StepWise,
    /// Adaptive throttling
/// Performance degradation configuration
pub struct PerformanceDegradation {
    /// Temperature threshold for this step
    pub temperature_threshold: f32,
    /// CPU frequency reduction (percentage)
    pub cpu_reduction: f32,
    /// GPU frequency reduction (percentage)
    pub gpu_reduction: f32,
    /// Model precision reduction
    pub precision_reduction: Option<u8>,
    /// Inference frequency reduction
    pub inference_reduction: f32,
/// Cooling strategies configuration
pub struct CoolingConfig {
    /// Active cooling methods
    pub active_cooling: Vec<ActiveCooling>,
    /// Passive cooling methods
    pub passive_cooling: Vec<PassiveCooling>,
    /// Workload distribution
    pub workload_distribution: WorkloadDistributionConfig,
/// Active cooling methods
pub enum ActiveCooling {
    /// Fan control
    Fan,
    /// Liquid cooling
    Liquid,
    /// Thermal pads
    ThermalPads,
/// Passive cooling methods
pub enum PassiveCooling {
    /// Heat spreaders
    HeatSpreaders,
    /// Thermal throttling
    ThermalThrottling,
    /// Duty cycling
    DutyCycling,
    /// Clock gating
    ClockGating,
/// Workload distribution for thermal management
pub struct WorkloadDistributionConfig {
    /// Distribute across cores
    pub distribute_cores: bool,
    /// Migrate hot tasks
    pub migrate_hot_tasks: bool,
    /// Load balancing
    pub load_balancing: bool,
    /// Thermal-aware scheduling
    pub thermal_scheduling: bool,
/// Optimization level for mobile deployment
pub enum OptimizationLevel {
    /// No optimization
    None,
    /// Basic optimization
    Basic,
    /// Aggressive optimization
    Aggressive,
    /// Custom optimization
    Custom(Vec<OptimizationPass>),
/// Individual optimization pass
pub enum OptimizationPass {
    /// Dead code elimination
    DeadCodeElimination,
    /// Constant folding
    ConstantFolding,
    /// Loop unrolling
    LoopUnrolling,
    /// Vectorization
    Vectorization,
    /// Instruction scheduling
    InstructionScheduling,
    /// Register allocation
    RegisterAllocation,
/// Precision mode for mobile inference
pub enum PrecisionMode {
    /// Full precision (FP32)
    Full,
    /// Half precision (FP16)
    Half,
    Mixed,
    /// Integer quantization
    Integer(u8),
/// Specialization mode for mobile optimization
pub enum SpecializationMode {
    /// No specialization
    /// Hardware specialization
    Hardware,
    /// Input shape specialization
    InputShape,
    /// Full specialization
impl Default for MobileOptimizationConfig {
    fn default() -> Self {
        Self {
            compression: MobileCompressionConfig {
                pruning: MobilePruningStrategy {
                    pruning_type: PruningType::Magnitude,
                    sparsity_level: 0.5,
                    structured: true,
                    hardware_aware: true,
                },
                distillation: MobileDistillationConfig {
                    enable: true,
                    teacher_complexity: TeacherComplexity::Desktop,
                    temperature: 3.0,
                    loss_weighting: DistillationWeighting {
                        knowledge_weight: 0.7,
                        ground_truth_weight: 0.3,
                        feature_weight: 0.1,
                    },
                weight_sharing: true,
                layer_fusion: true,
            },
            quantization: MobileQuantizationConfig {
                strategy: QuantizationStrategy::PostTraining,
                precision: QuantizationPrecision {
                    weights: 8,
                    activations: 8,
                    bias: Some(32),
                calibration: CalibrationMethod::Entropy,
                hardware_acceleration: true,
            memory: MobileMemoryConfig {
                pool_strategy: MemoryPoolStrategy::Dynamic,
                buffer_management: BufferManagementConfig {
                    pooling: true,
                    alignment: 16,
                    prefault: false,
                    memory_advice: MemoryAdvice::Sequential,
                memory_mapping: MemoryMappingConfig {
                    map_private: true,
                    lock_pages: false,
                    huge_pages: false,
                gc_optimization: GCOptimizationConfig {
                    minimize_allocations: true,
                    object_pooling: true,
                    weak_references: true,
                    manual_management: false,
            power: PowerManagementConfig {
                power_mode: PowerMode::Balanced,
                cpu_scaling: CPUScalingConfig {
                    governor: CPUGovernor::OnDemand,
                    min_frequency: None,
                    max_frequency: None,
                    performance_cores: true,
                gpu_power: GPUPowerConfig {
                    frequency_scaling: true,
                    voltage_scaling: true,
                    idle_timeout_ms: 100,
                    power_gating: true,
                battery_optimization: BatteryOptimizationConfig {
                    level_monitoring: true,
                    adaptive_frequency: true,
                    low_battery_mode: LowBatteryMode {
                        threshold_percentage: 20,
                        reduced_precision: true,
                        skip_non_critical: true,
                        suspend_background: true,
                    charging_awareness: true,
            thermal: ThermalManagementConfig {
                monitoring: ThermalMonitoringConfig {
                    sensors: vec![ThermalSensor::CPU, ThermalSensor::GPU],
                    frequency_ms: 1000,
                    thresholds: ThermalThresholds {
                        warning: 70.0,
                        critical: 80.0,
                        emergency: 90.0,
                throttling: ThermalThrottlingConfig {
                    strategy: ThrottlingStrategy::Adaptive,
                    degradation_steps: vec![
                        PerformanceDegradation {
                            temperature_threshold: 70.0,
                            cpu_reduction: 10.0,
                            gpu_reduction: 10.0,
                            precision_reduction: None,
                            inference_reduction: 5.0,
                        },
                            temperature_threshold: 80.0,
                            cpu_reduction: 25.0,
                            gpu_reduction: 25.0,
                            precision_reduction: Some(4),
                            inference_reduction: 15.0,
                    ],
                cooling: CoolingConfig {
                    active_cooling: vec![],
                    passive_cooling: vec![
                        PassiveCooling::ThermalThrottling,
                        PassiveCooling::DutyCycling,
                    workload_distribution: WorkloadDistributionConfig {
                        distribute_cores: true,
                        migrate_hot_tasks: true,
                        load_balancing: true,
                        thermal_scheduling: true,
        }
    }
