//! Cedar Policy Engine Integration
//!
//! Provides comprehensive policy management using Cedar policy language
//! with configuration-driven approach following PAP principles.

pub mod context;
pub mod engine;
pub mod hooks;
pub mod templates;
pub mod validation;
pub mod versioning;

use std::collections::HashMap;

use cedar_policy::{Authorizer, Decision, Policy, PolicyId, PolicySet, Request, Response};
pub use context::*;
pub use engine::*;
pub use hooks::*;
use serde::{Deserialize, Serialize};
pub use templates::*;
use uuid::Uuid;
pub use validation::*;
pub use versioning::*;

/// Policy evaluation result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PolicyResult {
    pub decision: PolicyDecision,
    pub policy_id: Option<String>,
    pub errors: Vec<String>,
    pub warnings: Vec<String>,
    pub context: HashMap<String, serde_json::Value>,
}

/// Policy decision enumeration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum PolicyDecision {
    Allow,
    Deny,
    NotApplicable,
}

impl From<Decision> for PolicyDecision {
    fn from(decision: Decision) -> Self {
        match decision {
            Decision::Allow => PolicyDecision::Allow,
            Decision::Deny => PolicyDecision::Deny,
        }
    }
}

/// Policy metadata for versioning and management
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PolicyMetadata {
    pub id: String,
    pub name: String,
    pub description: String,
    pub version: String,
    pub created_at: chrono::DateTime<chrono::Utc>,
    pub updated_at: chrono::DateTime<chrono::Utc>,
    pub created_by: String,
    pub tags: Vec<String>,
    pub category: PolicyCategory,
    pub priority: u32,
    pub enabled: bool,
}

/// Policy categories for organization
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum PolicyCategory {
    Authentication,
    Authorization,
    Infrastructure,
    Workflow,
    Compliance,
    Security,
    Anomaly,
}

/// Request context for policy evaluation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PolicyRequestContext {
    pub principal: Principal,
    pub action: Action,
    pub resource: Resource,
    pub environment: HashMap<String, serde_json::Value>,
}

/// Principal (user/service) information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Principal {
    pub id: String,
    pub entity_type: String,
    pub attributes: HashMap<String, serde_json::Value>,
}

/// Action being performed
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Action {
    pub id: String,
    pub entity_type: String,
    pub attributes: HashMap<String, serde_json::Value>,
}

/// Resource being accessed
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Resource {
    pub id: String,
    pub entity_type: String,
    pub attributes: HashMap<String, serde_json::Value>,
}
