// frontend/src/api/types/settings.ts
/**
 * TypeScript types for Tenant Settings
 * Operational configuration for bakery tenants
 */

export interface ProcurementSettings {
  auto_approve_enabled: boolean;
  auto_approve_threshold_eur: number;
  auto_approve_min_supplier_score: number;
  require_approval_new_suppliers: boolean;
  require_approval_critical_items: boolean;
  procurement_lead_time_days: number;
  demand_forecast_days: number;
  safety_stock_percentage: number;
  po_approval_reminder_hours: number;
  po_critical_escalation_hours: number;
  use_reorder_rules: boolean;
  economic_rounding: boolean;
  respect_storage_limits: boolean;
  use_supplier_minimums: boolean;
  optimize_price_tiers: boolean;
}

export interface InventorySettings {
  low_stock_threshold: number;
  reorder_point: number;
  reorder_quantity: number;
  expiring_soon_days: number;
  expiration_warning_days: number;
  quality_score_threshold: number;
  temperature_monitoring_enabled: boolean;
  refrigeration_temp_min: number;
  refrigeration_temp_max: number;
  freezer_temp_min: number;
  freezer_temp_max: number;
  room_temp_min: number;
  room_temp_max: number;
  temp_deviation_alert_minutes: number;
  critical_temp_deviation_minutes: number;
}

export interface ProductionSettings {
  planning_horizon_days: number;
  minimum_batch_size: number;
  maximum_batch_size: number;
  production_buffer_percentage: number;
  working_hours_per_day: number;
  max_overtime_hours: number;
  capacity_utilization_target: number;
  capacity_warning_threshold: number;
  quality_check_enabled: boolean;
  minimum_yield_percentage: number;
  quality_score_threshold: number;
  schedule_optimization_enabled: boolean;
  prep_time_buffer_minutes: number;
  cleanup_time_buffer_minutes: number;
  labor_cost_per_hour_eur: number;
  overhead_cost_percentage: number;
}

export interface SupplierSettings {
  default_payment_terms_days: number;
  default_delivery_days: number;
  excellent_delivery_rate: number;
  good_delivery_rate: number;
  excellent_quality_rate: number;
  good_quality_rate: number;
  critical_delivery_delay_hours: number;
  critical_quality_rejection_rate: number;
  high_cost_variance_percentage: number;
}

export interface POSSettings {
  sync_interval_minutes: number;
  auto_sync_products: boolean;
  auto_sync_transactions: boolean;
}

export interface OrderSettings {
  max_discount_percentage: number;
  default_delivery_window_hours: number;
  dynamic_pricing_enabled: boolean;
  discount_enabled: boolean;
  delivery_tracking_enabled: boolean;
}

export interface ReplenishmentSettings {
  projection_horizon_days: number;
  service_level: number;
  buffer_days: number;
  enable_auto_replenishment: boolean;
  min_order_quantity: number;
  max_order_quantity: number;
  demand_forecast_days: number;
}

export interface SafetyStockSettings {
  service_level: number;
  method: string;
  min_safety_stock: number;
  max_safety_stock: number;
  reorder_point_calculation: string;
}

export interface MOQSettings {
  consolidation_window_days: number;
  allow_early_ordering: boolean;
  enable_batch_optimization: boolean;
  min_batch_size: number;
  max_batch_size: number;
}

export interface SupplierSelectionSettings {
  price_weight: number;
  lead_time_weight: number;
  quality_weight: number;
  reliability_weight: number;
  diversification_threshold: number;
  max_single_percentage: number;
  enable_supplier_score_optimization: boolean;
}

export interface MLInsightsSettings {
  // Inventory ML (Safety Stock Optimization)
  inventory_lookback_days: number;
  inventory_min_history_days: number;

  // Production ML (Yield Prediction)
  production_lookback_days: number;
  production_min_history_runs: number;

  // Procurement ML (Supplier Analysis & Price Forecasting)
  supplier_analysis_lookback_days: number;
  supplier_analysis_min_orders: number;
  price_forecast_lookback_days: number;
  price_forecast_horizon_days: number;

  // Forecasting ML (Dynamic Rules)
  rules_generation_lookback_days: number;
  rules_generation_min_samples: number;

  // Global ML Settings
  enable_ml_insights: boolean;
  ml_insights_auto_trigger: boolean;
  ml_confidence_threshold: number;
}

export interface NotificationSettings {
  // WhatsApp Configuration (Shared Account Model)
  whatsapp_enabled: boolean;
  whatsapp_phone_number_id: string;
  whatsapp_display_phone_number: string;
  whatsapp_default_language: string;

  // Email Configuration
  email_enabled: boolean;
  email_from_address: string;
  email_from_name: string;
  email_reply_to: string;

  // Notification Preferences
  enable_po_notifications: boolean;
  enable_inventory_alerts: boolean;
  enable_production_alerts: boolean;
  enable_forecast_alerts: boolean;

  // Notification Channels
  po_notification_channels: string[];
  inventory_alert_channels: string[];
  production_alert_channels: string[];
  forecast_alert_channels: string[];
}

export interface TenantSettings {
  id: string;
  tenant_id: string;
  procurement_settings: ProcurementSettings;
  inventory_settings: InventorySettings;
  production_settings: ProductionSettings;
  supplier_settings: SupplierSettings;
  pos_settings: POSSettings;
  order_settings: OrderSettings;
  replenishment_settings: ReplenishmentSettings;
  safety_stock_settings: SafetyStockSettings;
  moq_settings: MOQSettings;
  supplier_selection_settings: SupplierSelectionSettings;
  ml_insights_settings: MLInsightsSettings;
  notification_settings: NotificationSettings;
  created_at: string;
  updated_at: string;
}

export interface TenantSettingsUpdate {
  procurement_settings?: Partial<ProcurementSettings>;
  inventory_settings?: Partial<InventorySettings>;
  production_settings?: Partial<ProductionSettings>;
  supplier_settings?: Partial<SupplierSettings>;
  pos_settings?: Partial<POSSettings>;
  order_settings?: Partial<OrderSettings>;
  replenishment_settings?: Partial<ReplenishmentSettings>;
  safety_stock_settings?: Partial<SafetyStockSettings>;
  moq_settings?: Partial<MOQSettings>;
  supplier_selection_settings?: Partial<SupplierSelectionSettings>;
  ml_insights_settings?: Partial<MLInsightsSettings>;
  notification_settings?: Partial<NotificationSettings>;
}

export type SettingsCategory =
  | 'procurement'
  | 'inventory'
  | 'production'
  | 'supplier'
  | 'pos'
  | 'order'
  | 'replenishment'
  | 'safety_stock'
  | 'moq'
  | 'supplier_selection'
  | 'ml_insights'
  | 'notification';

export interface CategoryResetResponse {
  category: string;
  settings: Record<string, any>;
  message: string;
}
