"""
Alert type definitions with i18n key mappings.

Each alert type maps to:
- title_key: i18n key for title (e.g., "alerts.critical_stock_shortage.title")
- title_params: parameter mappings from metadata to i18n params
- message_variants: different message keys based on context
- message_params: parameter mappings for message

When adding new alert types:
1. Add entry to ALERT_TEMPLATES
2. Ensure corresponding translations exist in frontend/src/locales/*/alerts.json
3. Document required metadata fields
"""

# Alert type templates
ALERT_TEMPLATES = {
    # ==================== INVENTORY ALERTS ====================

    "critical_stock_shortage": {
        "title_key": "alerts.critical_stock_shortage.title",
        "title_params": {
            "ingredient_name": "ingredient_name"
        },
        "message_variants": {
            "with_po_pending": "alerts.critical_stock_shortage.message_with_po_pending",
            "with_po_created": "alerts.critical_stock_shortage.message_with_po_created",
            "with_hours": "alerts.critical_stock_shortage.message_with_hours",
            "with_date": "alerts.critical_stock_shortage.message_with_date",
            "generic": "alerts.critical_stock_shortage.message_generic"
        },
        "message_params": {
            "ingredient_name": "ingredient_name",
            "current_stock_kg": "current_stock",
            "required_stock_kg": "required_stock",
            "hours_until": "hours_until",
            "production_day_name": "production_date",
            "po_id": "po_id",
            "po_amount": "po_amount",
            "delivery_day_name": "delivery_date"
        }
    },

    "low_stock_warning": {
        "title_key": "alerts.low_stock.title",
        "title_params": {
            "ingredient_name": "ingredient_name"
        },
        "message_variants": {
            "with_po": "alerts.low_stock.message_with_po",
            "generic": "alerts.low_stock.message_generic"
        },
        "message_params": {
            "ingredient_name": "ingredient_name",
            "current_stock_kg": "current_stock",
            "minimum_stock_kg": "minimum_stock"
        }
    },

    "overstock_warning": {
        "title_key": "alerts.overstock_warning.title",
        "title_params": {
            "ingredient_name": "ingredient_name"
        },
        "message_variants": {
            "generic": "alerts.overstock_warning.message"
        },
        "message_params": {
            "ingredient_name": "ingredient_name",
            "current_stock_kg": "current_stock",
            "maximum_stock_kg": "maximum_stock",
            "excess_amount_kg": "excess_amount"
        }
    },

    "expired_products": {
        "title_key": "alerts.expired_products.title",
        "title_params": {
            "count": "expired_count"
        },
        "message_variants": {
            "with_names": "alerts.expired_products.message_with_names",
            "generic": "alerts.expired_products.message_generic"
        },
        "message_params": {
            "expired_count": "expired_count",
            "product_names": "product_names",
            "total_value_eur": "total_value"
        }
    },

    "urgent_expiry": {
        "title_key": "alerts.urgent_expiry.title",
        "title_params": {
            "ingredient_name": "ingredient_name"
        },
        "message_variants": {
            "generic": "alerts.urgent_expiry.message"
        },
        "message_params": {
            "ingredient_name": "ingredient_name",
            "days_until_expiry": "days_until_expiry",
            "quantity_kg": "quantity"
        }
    },

    "temperature_breach": {
        "title_key": "alerts.temperature_breach.title",
        "title_params": {
            "location": "location"
        },
        "message_variants": {
            "generic": "alerts.temperature_breach.message"
        },
        "message_params": {
            "location": "location",
            "temperature": "temperature",
            "max_threshold": "max_threshold",
            "duration_minutes": "duration_minutes"
        }
    },

    "stock_depleted_by_order": {
        "title_key": "alerts.stock_depleted_by_order.title",
        "title_params": {
            "ingredient_name": "ingredient_name"
        },
        "message_variants": {
            "with_supplier": "alerts.stock_depleted_by_order.message_with_supplier",
            "generic": "alerts.stock_depleted_by_order.message_generic"
        },
        "message_params": {
            "ingredient_name": "ingredient_name",
            "shortage_kg": "shortage_amount",
            "supplier_name": "supplier_name",
            "supplier_contact": "supplier_contact"
        }
    },

    # ==================== PRODUCTION ALERTS ====================

    "production_delay": {
        "title_key": "alerts.production_delay.title",
        "title_params": {
            "product_name": "product_name",
            "batch_number": "batch_number"
        },
        "message_variants": {
            "with_customers": "alerts.production_delay.message_with_customers",
            "with_orders": "alerts.production_delay.message_with_orders",
            "generic": "alerts.production_delay.message_generic"
        },
        "message_params": {
            "product_name": "product_name",
            "batch_number": "batch_number",
            "delay_minutes": "delay_minutes",
            "affected_orders": "affected_orders",
            "customer_names": "customer_names"
        }
    },

    "equipment_failure": {
        "title_key": "alerts.equipment_failure.title",
        "title_params": {
            "equipment_name": "equipment_name"
        },
        "message_variants": {
            "with_batches": "alerts.equipment_failure.message_with_batches",
            "generic": "alerts.equipment_failure.message_generic"
        },
        "message_params": {
            "equipment_name": "equipment_name",
            "equipment_type": "equipment_type",
            "affected_batches": "affected_batches"
        }
    },

    "maintenance_required": {
        "title_key": "alerts.maintenance_required.title",
        "title_params": {
            "equipment_name": "equipment_name"
        },
        "message_variants": {
            "with_hours": "alerts.maintenance_required.message_with_hours",
            "with_days": "alerts.maintenance_required.message_with_days",
            "generic": "alerts.maintenance_required.message_generic"
        },
        "message_params": {
            "equipment_name": "equipment_name",
            "hours_overdue": "hours_overdue",
            "days_overdue": "days_overdue"
        }
    },

    "low_equipment_efficiency": {
        "title_key": "alerts.low_equipment_efficiency.title",
        "title_params": {
            "equipment_name": "equipment_name"
        },
        "message_variants": {
            "generic": "alerts.low_equipment_efficiency.message"
        },
        "message_params": {
            "equipment_name": "equipment_name",
            "efficiency_percentage": "efficiency_percentage",
            "target_efficiency": "target_efficiency"
        }
    },

    "capacity_overload": {
        "title_key": "alerts.capacity_overload.title",
        "title_params": {
            "date": "planned_date"
        },
        "message_variants": {
            "generic": "alerts.capacity_overload.message"
        },
        "message_params": {
            "planned_date": "planned_date",
            "capacity_percentage": "capacity_percentage",
            "equipment_count": "equipment_count"
        }
    },

    "quality_control_failure": {
        "title_key": "alerts.quality_control_failure.title",
        "title_params": {
            "product_name": "product_name",
            "batch_number": "batch_number"
        },
        "message_variants": {
            "generic": "alerts.quality_control_failure.message"
        },
        "message_params": {
            "product_name": "product_name",
            "batch_number": "batch_number",
            "check_type": "check_type",
            "quality_score": "quality_score",
            "defect_count": "defect_count"
        }
    },

    # ==================== PROCUREMENT ALERTS ====================

    "po_approval_needed": {
        "title_key": "alerts.po_approval_needed.title",
        "title_params": {
            "po_number": "po_number"
        },
        "message_variants": {
            "generic": "alerts.po_approval_needed.message"
        },
        "message_params": {
            "supplier_name": "supplier_name",
            "total_amount": "total_amount",
            "currency": "currency",
            "required_delivery_date": "required_delivery_date",
            "items_count": "items_count"
        }
    },

    "po_approval_escalation": {
        "title_key": "alerts.po_approval_escalation.title",
        "title_params": {
            "po_number": "po_number"
        },
        "message_variants": {
            "generic": "alerts.po_approval_escalation.message"
        },
        "message_params": {
            "po_number": "po_number",
            "supplier_name": "supplier_name",
            "hours_pending": "hours_pending",
            "total_amount": "total_amount"
        }
    },

    "delivery_overdue": {
        "title_key": "alerts.delivery_overdue.title",
        "title_params": {
            "po_number": "po_number"
        },
        "message_variants": {
            "generic": "alerts.delivery_overdue.message"
        },
        "message_params": {
            "po_number": "po_number",
            "supplier_name": "supplier_name",
            "days_overdue": "days_overdue",
            "expected_date": "expected_date"
        }
    },

    # ==================== SUPPLY CHAIN ALERTS ====================

    "supplier_delay": {
        "title_key": "alerts.supplier_delay.title",
        "title_params": {
            "supplier_name": "supplier_name"
        },
        "message_variants": {
            "generic": "alerts.supplier_delay.message"
        },
        "message_params": {
            "supplier_name": "supplier_name",
            "po_count": "po_count",
            "avg_delay_days": "avg_delay_days"
        }
    },

    # ==================== DEMAND ALERTS ====================

    "demand_surge_weekend": {
        "title_key": "alerts.demand_surge_weekend.title",
        "title_params": {},
        "message_variants": {
            "generic": "alerts.demand_surge_weekend.message"
        },
        "message_params": {
            "product_name": "product_name",
            "predicted_demand": "predicted_demand",
            "current_stock": "current_stock"
        }
    },

    "weather_impact_alert": {
        "title_key": "alerts.weather_impact_alert.title",
        "title_params": {},
        "message_variants": {
            "generic": "alerts.weather_impact_alert.message"
        },
        "message_params": {
            "weather_condition": "weather_condition",
            "impact_percentage": "impact_percentage",
            "date": "date"
        }
    },

    # ==================== PRODUCTION BATCH ALERTS ====================

    "production_batch_start": {
        "title_key": "alerts.production_batch_start.title",
        "title_params": {
            "product_name": "product_name"
        },
        "message_variants": {
            "generic": "alerts.production_batch_start.message"
        },
        "message_params": {
            "product_name": "product_name",
            "batch_number": "batch_number",
            "quantity_planned": "quantity_planned",
            "unit": "unit",
            "priority": "priority"
        }
    },

    # ==================== GENERIC FALLBACK ====================

    "generic": {
        "title_key": "alerts.generic.title",
        "title_params": {},
        "message_variants": {
            "generic": "alerts.generic.message"
        },
        "message_params": {
            "event_type": "event_type"
        }
    }
}


# Notification templates (informational events)
NOTIFICATION_TEMPLATES = {
    "po_approved": {
        "title_key": "notifications.po_approved.title",
        "title_params": {
            "po_number": "po_number"
        },
        "message_variants": {
            "generic": "notifications.po_approved.message"
        },
        "message_params": {
            "supplier_name": "supplier_name",
            "total_amount": "total_amount"
        }
    },

    "batch_state_changed": {
        "title_key": "notifications.batch_state_changed.title",
        "title_params": {
            "product_name": "product_name"
        },
        "message_variants": {
            "generic": "notifications.batch_state_changed.message"
        },
        "message_params": {
            "batch_number": "batch_number",
            "new_status": "new_status",
            "quantity": "quantity",
            "unit": "unit"
        }
    },

    "stock_received": {
        "title_key": "notifications.stock_received.title",
        "title_params": {
            "ingredient_name": "ingredient_name"
        },
        "message_variants": {
            "generic": "notifications.stock_received.message"
        },
        "message_params": {
            "quantity_received": "quantity_received",
            "unit": "unit",
            "supplier_name": "supplier_name"
        }
    }
}


# Recommendation templates (optimization suggestions)
RECOMMENDATION_TEMPLATES = {
    "inventory_optimization": {
        "title_key": "recommendations.inventory_optimization.title",
        "title_params": {
            "ingredient_name": "ingredient_name"
        },
        "message_variants": {
            "generic": "recommendations.inventory_optimization.message"
        },
        "message_params": {
            "ingredient_name": "ingredient_name",
            "current_max_kg": "current_max",
            "suggested_max_kg": "suggested_max",
            "recommendation_type": "recommendation_type"
        }
    },

    "production_efficiency": {
        "title_key": "recommendations.production_efficiency.title",
        "title_params": {
            "product_name": "product_name"
        },
        "message_variants": {
            "generic": "recommendations.production_efficiency.message"
        },
        "message_params": {
            "product_name": "product_name",
            "potential_time_saved_minutes": "time_saved",
            "suggestion": "suggestion"
        }
    },

    # ==================== AI INSIGHTS RECOMMENDATIONS ====================

    "ai_yield_prediction": {
        "title_key": "recommendations.ai_yield_prediction.title",
        "title_params": {
            "recipe_name": "recipe_name"
        },
        "message_variants": {
            "generic": "recommendations.ai_yield_prediction.message"
        },
        "message_params": {
            "recipe_name": "recipe_name",
            "predicted_yield_percent": "predicted_yield",
            "confidence_percent": "confidence",
            "recommendation": "recommendation"
        }
    },

    "ai_safety_stock_optimization": {
        "title_key": "recommendations.ai_safety_stock_optimization.title",
        "title_params": {
            "ingredient_name": "ingredient_name"
        },
        "message_variants": {
            "generic": "recommendations.ai_safety_stock_optimization.message"
        },
        "message_params": {
            "ingredient_name": "ingredient_name",
            "suggested_safety_stock_kg": "suggested_safety_stock",
            "current_safety_stock_kg": "current_safety_stock",
            "estimated_savings_eur": "estimated_savings",
            "confidence_percent": "confidence"
        }
    },

    "ai_supplier_recommendation": {
        "title_key": "recommendations.ai_supplier_recommendation.title",
        "title_params": {
            "supplier_name": "supplier_name"
        },
        "message_variants": {
            "generic": "recommendations.ai_supplier_recommendation.message"
        },
        "message_params": {
            "supplier_name": "supplier_name",
            "reliability_score": "reliability_score",
            "recommendation": "recommendation",
            "confidence_percent": "confidence"
        }
    },

    "ai_price_forecast": {
        "title_key": "recommendations.ai_price_forecast.title",
        "title_params": {
            "ingredient_name": "ingredient_name"
        },
        "message_variants": {
            "generic": "recommendations.ai_price_forecast.message"
        },
        "message_params": {
            "ingredient_name": "ingredient_name",
            "predicted_price_eur": "predicted_price",
            "current_price_eur": "current_price",
            "price_trend": "price_trend",
            "recommendation": "recommendation",
            "confidence_percent": "confidence"
        }
    },

    "ai_demand_forecast": {
        "title_key": "recommendations.ai_demand_forecast.title",
        "title_params": {
            "product_name": "product_name"
        },
        "message_variants": {
            "generic": "recommendations.ai_demand_forecast.message"
        },
        "message_params": {
            "product_name": "product_name",
            "predicted_demand": "predicted_demand",
            "forecast_period": "forecast_period",
            "confidence_percent": "confidence",
            "recommendation": "recommendation"
        }
    },

    "ai_business_rule": {
        "title_key": "recommendations.ai_business_rule.title",
        "title_params": {
            "rule_category": "rule_category"
        },
        "message_variants": {
            "generic": "recommendations.ai_business_rule.message"
        },
        "message_params": {
            "rule_category": "rule_category",
            "rule_description": "rule_description",
            "confidence_percent": "confidence",
            "recommendation": "recommendation"
        }
    }
}
