use crate::error::{ErrorData, Result};
use crate::resource::{ResourceDefinition, ResourceOutputsDefinition, ResourceRef, ResourceType};
use alien_error::AlienError;
use bon::Builder;
use serde::{Deserialize, Serialize};
use std::any::Any;
use std::fmt::Debug;

/// Represents a secure vault for storing secrets.
/// This resource provides a platform-agnostic interface over cloud-native secret management services:
/// - AWS: AWS Secrets Manager with prefixed secret names
/// - GCP: Secret Manager with prefixed secret names
/// - Azure: Key Vault resource
///
/// The vault acts as a namespace for secrets and controls access permissions for functions and services.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Builder)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[builder(start_fn = new)]
pub struct Vault {
    /// Unique identifier for the vault. Must contain only alphanumeric characters, hyphens, and underscores ([A-Za-z0-9-_]).
    /// Maximum 64 characters.
    #[builder(start_fn)]
    pub id: String,
}

impl Vault {
    /// The resource type identifier for Vault
    pub const RESOURCE_TYPE: ResourceType = ResourceType::from_static("vault");

    /// Returns the vault's unique identifier.
    pub fn id(&self) -> &str {
        &self.id
    }
}

/// Outputs generated by a successfully provisioned Vault.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct VaultOutputs {
    /// Platform-specific vault identifier.
    /// - AWS: Account and region (e.g., "123456789012:us-west-2") - implicit reference to Secrets Manager
    /// - GCP: Project and location (e.g., "projects/my-project/locations/us-central1") - implicit reference to Secret Manager
    /// - Azure: Key Vault resource ID (e.g., "/subscriptions/.../resourceGroups/.../providers/Microsoft.KeyVault/vaults/my-vault")
    pub vault_id: String,
}

impl ResourceOutputsDefinition for VaultOutputs {
    fn get_resource_type(&self) -> ResourceType {
        Vault::RESOURCE_TYPE.clone()
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn box_clone(&self) -> Box<dyn ResourceOutputsDefinition> {
        Box::new(self.clone())
    }

    fn outputs_eq(&self, other: &dyn ResourceOutputsDefinition) -> bool {
        other.as_any().downcast_ref::<VaultOutputs>() == Some(self)
    }

    fn to_json_value(&self) -> serde_json::Result<serde_json::Value> {
        serde_json::to_value(self)
    }
}

// Implementation of ResourceDefinition trait for Vault
impl ResourceDefinition for Vault {
    fn get_resource_type(&self) -> ResourceType {
        Self::RESOURCE_TYPE
    }

    fn id(&self) -> &str {
        &self.id
    }

    fn get_dependencies(&self) -> Vec<ResourceRef> {
        Vec::new()
    }

    fn validate_update(&self, new_config: &dyn ResourceDefinition) -> Result<()> {
        // Downcast to Vault type to use the existing validate_update method
        let new_vault = new_config.as_any().downcast_ref::<Vault>().ok_or_else(|| {
            AlienError::new(ErrorData::UnexpectedResourceType {
                resource_id: self.id.clone(),
                expected: Self::RESOURCE_TYPE,
                actual: new_config.get_resource_type(),
            })
        })?;

        if self.id != new_vault.id {
            return Err(AlienError::new(ErrorData::InvalidResourceUpdate {
                resource_id: self.id.clone(),
                reason: "the 'id' field is immutable".to_string(),
            }));
        }
        Ok(())
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }

    fn box_clone(&self) -> Box<dyn ResourceDefinition> {
        Box::new(self.clone())
    }

    fn resource_eq(&self, other: &dyn ResourceDefinition) -> bool {
        other.as_any().downcast_ref::<Vault>() == Some(self)
    }

    fn to_json_value(&self) -> serde_json::Result<serde_json::Value> {
        serde_json::to_value(self)
    }
}

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

    #[test]
    fn test_vault_creation() {
        let vault = Vault::new("my-vault".to_string()).build();
        assert_eq!(vault.id, "my-vault");
    }

    #[test]
    fn test_vault_dependencies() {
        let vault = Vault::new("my-vault".to_string()).build();
        let dependencies = vault.get_dependencies();
        assert!(dependencies.is_empty());
    }

    #[test]
    fn test_vault_outputs_equality() {
        let outputs1 = VaultOutputs {
            vault_id: "test-vault".to_string(),
        };

        let outputs2 = VaultOutputs {
            vault_id: "test-vault".to_string(),
        };

        let outputs3 = VaultOutputs {
            vault_id: "different-vault".to_string(),
        };

        assert!(outputs1.outputs_eq(&outputs2));
        assert!(!outputs1.outputs_eq(&outputs3));
    }
}
