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

/// Defines a rule for managing the lifecycle of objects within a storage bucket.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct LifecycleRule {
    /// Number of days after which objects matching the rule expire.
    pub days: u32,
    /// Optional prefix to filter objects the rule applies to. If None, applies to all objects.
    #[serde(default)]
    pub prefix: Option<String>,
}

/// Represents an object storage bucket.
#[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 Storage {
    /// Name of the the storage bucket.
    /// For names with dots, each dot-separated label must be ≤ 63 characters.
    #[builder(start_fn)]
    pub id: String,

    /// Allows public read access to objects without authentication.
    /// Default: `false`
    #[serde(default)]
    #[builder(default)]
    pub public_read: bool,

    /// Enables object versioning.
    /// Default: `false`
    #[serde(default)]
    #[builder(default)]
    pub versioning: bool,

    /// List of rules for automatic object management (e.g., expiration).
    /// Default: `[]` (empty list)
    #[serde(default)]
    #[builder(default)]
    pub lifecycle_rules: Vec<LifecycleRule>,
}

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

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

/// Outputs generated by a successfully provisioned Storage bucket.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct StorageOutputs {
    /// The globally unique name of the bucket.
    pub bucket_name: String,
    // Add other outputs like region or URL if needed later
}

impl ResourceOutputsDefinition for StorageOutputs {
    fn get_resource_type(&self) -> ResourceType {
        Storage::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::<StorageOutputs>() == Some(self)
    }

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

// Implementation of ResourceDefinition trait for Storage
impl ResourceDefinition for Storage {
    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 Storage type to use the existing validate_update method
        let new_storage = new_config
            .as_any()
            .downcast_ref::<Storage>()
            .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_storage.id {
            return Err(AlienError::new(ErrorData::InvalidResourceUpdate {
                resource_id: self.id.clone(),
                reason: "the 'id' field is immutable".to_string(),
            }));
        }
        // Add other validation rules here if needed
        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::<Storage>() == Some(self)
    }

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