//! Compliance Module
//!
//! Comprehensive compliance features for GDPR, SOC2, and ISO 27001
//! requirements.
//!
//! # Features
//! - GDPR data subject rights (access, erasure, rectification, portability)
//! - SOC2 Trust Service Criteria verification
//! - ISO 27001 Annex A controls verification
//! - Data protection controls (encryption, access control)
//! - Incident response management
//! - Automated compliance reporting
//!
//! # Example
//! ```ignore
//! use provisioning_orchestrator::compliance::{ComplianceService, ComplianceConfig};
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let config = ComplianceConfig::default();
//! let compliance = ComplianceService::new(config).await?;
//!
//! // Export user data for GDPR compliance
//! let export = compliance.gdpr.export_personal_data("user123").await?;
//!
//! // Generate SOC2 report
//! let report = compliance.soc2.generate_report().await?;
//! # Ok(())
//! # }
//! ```

pub mod access_control;
pub mod api;
pub mod data_protection;
pub mod gdpr;
pub mod incident_response;
pub mod iso27001;
pub mod soc2;
pub mod types;

use std::sync::Arc;

pub use access_control::{AccessControlConfig, AccessControlMatrix};
use anyhow::Result;
pub use api::compliance_routes;
pub use data_protection::{DataProtection, DataProtectionConfig};
pub use gdpr::{GdprService, GdprServiceConfig};
pub use incident_response::{IncidentResponseConfig, IncidentResponseService};
pub use iso27001::{Iso27001Compliance, Iso27001Config};
pub use soc2::{Soc2Compliance, Soc2Config};
pub use types::*;

use crate::audit::AuditLogger;

/// Complete compliance service
pub struct ComplianceService {
    /// GDPR compliance service
    pub gdpr: Arc<GdprService>,

    /// SOC2 compliance service
    pub soc2: Arc<Soc2Compliance>,

    /// ISO 27001 compliance service
    pub iso27001: Arc<Iso27001Compliance>,

    /// Data protection controls
    pub data_protection: Arc<DataProtection>,

    /// Access control matrix
    pub access_control: Arc<AccessControlMatrix>,

    /// Incident response service
    pub incident_response: Arc<IncidentResponseService>,
}

impl ComplianceService {
    /// Create a new compliance service
    pub async fn new(audit_logger: Arc<AuditLogger>, config: ComplianceConfig) -> Result<Self> {
        let gdpr = Arc::new(GdprService::new(audit_logger.clone(), config.gdpr).await?);

        let soc2 = Arc::new(Soc2Compliance::new(audit_logger.clone(), config.soc2).await?);

        let iso27001 =
            Arc::new(Iso27001Compliance::new(audit_logger.clone(), config.iso27001).await?);

        let data_protection = Arc::new(DataProtection::new(config.data_protection).await?);

        let access_control = Arc::new(AccessControlMatrix::new(config.access_control).await?);

        let incident_response = Arc::new(
            IncidentResponseService::new(audit_logger.clone(), config.incident_response).await?,
        );

        Ok(Self {
            gdpr,
            soc2,
            iso27001,
            data_protection,
            access_control,
            incident_response,
        })
    }

    /// Health check for all compliance services
    pub async fn health_check(&self) -> Result<ComplianceHealthStatus> {
        Ok(ComplianceHealthStatus {
            gdpr_operational: self.gdpr.health_check().await.is_ok(),
            soc2_operational: self.soc2.health_check().await.is_ok(),
            iso27001_operational: self.iso27001.health_check().await.is_ok(),
            data_protection_operational: self.data_protection.health_check().await.is_ok(),
            incident_response_operational: self.incident_response.health_check().await.is_ok(),
        })
    }
}

/// Compliance service configuration
#[derive(Debug, Clone, Default, serde::Deserialize)]
pub struct ComplianceConfig {
    /// GDPR configuration
    pub gdpr: GdprServiceConfig,

    /// SOC2 configuration
    pub soc2: Soc2Config,

    /// ISO 27001 configuration
    pub iso27001: Iso27001Config,

    /// Data protection configuration
    pub data_protection: DataProtectionConfig,

    /// Access control configuration
    pub access_control: AccessControlConfig,

    /// Incident response configuration
    pub incident_response: IncidentResponseConfig,
}

/// Compliance health status
#[derive(Debug, Clone, serde::Serialize)]
pub struct ComplianceHealthStatus {
    pub gdpr_operational: bool,
    pub soc2_operational: bool,
    pub iso27001_operational: bool,
    pub data_protection_operational: bool,
    pub incident_response_operational: bool,
}

#[cfg(test)]
mod tests;

#[cfg(test)]
mod module_tests {
    use super::*;

    #[test]
    fn test_module_exports() {
        // Ensure all types are properly exported
        let _: Option<ComplianceConfig> = None;
        let _: Option<ComplianceHealthStatus> = None;
    }
}
