//! Types for failure analysis

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;

/// Complete failure context collected from a request execution
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FailureContext {
    /// Request details
    pub request: RequestDetails,
    /// Response details (if available)
    pub response: Option<ResponseDetails>,
    /// Active chaos configurations
    pub chaos_configs: Vec<ChaosConfigInfo>,
    /// Active consistency rules
    pub consistency_rules: Vec<ConsistencyRuleInfo>,
    /// Contract validation results
    pub contract_validation: Option<ContractValidationInfo>,
    /// Behavioral rules/personas
    pub behavioral_rules: Vec<BehavioralRuleInfo>,
    /// Hook execution results
    pub hook_results: Vec<HookExecutionInfo>,
    /// Error message (if any)
    pub error_message: Option<String>,
    /// Timestamp of the failure
    pub timestamp: DateTime<Utc>,
}

/// Request details
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RequestDetails {
    /// HTTP method
    pub method: String,
    /// Request path
    pub path: String,
    /// Request headers
    pub headers: HashMap<String, String>,
    /// Query parameters
    pub query_params: HashMap<String, String>,
    /// Request body (if any)
    pub body: Option<Value>,
}

/// Response details
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResponseDetails {
    /// HTTP status code
    pub status_code: u16,
    /// Response headers
    pub headers: HashMap<String, String>,
    /// Response body (if any)
    pub body: Option<Value>,
    /// Response timing (milliseconds)
    pub duration_ms: Option<u64>,
}

/// Chaos configuration information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChaosConfigInfo {
    /// Configuration name/identifier
    pub name: String,
    /// Whether this config is enabled
    pub enabled: bool,
    /// Configuration details
    pub details: Value,
}

/// Consistency rule information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConsistencyRuleInfo {
    /// Rule name/identifier
    pub name: String,
    /// Whether this rule is enabled
    pub enabled: bool,
    /// Rule description
    pub description: Option<String>,
    /// Whether this rule was triggered
    pub triggered: bool,
    /// Rule details
    pub details: Value,
}

/// Contract validation information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContractValidationInfo {
    /// Whether validation passed
    pub passed: bool,
    /// Validation errors (if any)
    pub errors: Vec<String>,
    /// Contract details
    pub contract_details: Value,
}

/// Behavioral rule/persona information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BehavioralRuleInfo {
    /// Rule/persona name
    pub name: String,
    /// Whether this rule is active
    pub active: bool,
    /// Rule description
    pub description: Option<String>,
    /// Rule details
    pub details: Value,
}

/// Hook execution information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HookExecutionInfo {
    /// Hook name
    pub name: String,
    /// Whether hook executed successfully
    pub success: bool,
    /// Hook type
    pub hook_type: String,
    /// Error message (if execution failed)
    pub error: Option<String>,
}

/// Failure narrative generated by AI
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FailureNarrative {
    /// Human-readable summary of the failure
    pub summary: String,
    /// Detailed explanation of why the failure occurred
    pub explanation: String,
    /// Stack trace showing the chain of events
    pub stack_trace: Vec<NarrativeFrame>,
    /// Contributing factors
    pub contributing_factors: Vec<ContributingFactor>,
    /// Suggested fixes
    pub suggested_fixes: Vec<String>,
    /// Confidence score (0.0-1.0)
    pub confidence: f64,
}

/// A frame in the narrative stack trace
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NarrativeFrame {
    /// Frame description
    pub description: String,
    /// What triggered this frame
    pub trigger: String,
    /// Rule/persona/contract that caused this
    pub source: String,
    /// Source type (rule, persona, contract, chaos, etc.)
    pub source_type: String,
}

/// A contributing factor to the failure
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContributingFactor {
    /// Factor description
    pub description: String,
    /// Factor type
    pub factor_type: String,
    /// Impact level (high, medium, low)
    pub impact: String,
}
