use async_trait::async_trait;
use serde::Serialize;

use crate::topology::Topology;

use super::Application;
/// An ApplicationFeature provided by harmony, such as Backups, Monitoring, MultisiteAvailability,
/// ContinuousIntegration, ContinuousDelivery
#[async_trait]
pub trait ApplicationFeature<T: Topology>:
    std::fmt::Debug + Send + Sync + ApplicationFeatureClone<T>
{
    async fn ensure_installed(&self, topology: &T) -> Result<(), String>;
    fn name(&self) -> String;
}

trait ApplicationFeatureClone<T: Topology> {
    fn clone_box(&self) -> Box<dyn ApplicationFeature<T>>;
}

impl<A, T: Topology> ApplicationFeatureClone<T> for A
where
    A: ApplicationFeature<T> + Clone + 'static,
{
    fn clone_box(&self) -> Box<dyn ApplicationFeature<T>> {
        Box::new(self.clone())
    }
}

impl<T: Topology> Serialize for Box<dyn ApplicationFeature<T>> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        todo!()
    }
}

impl<T: Topology> Clone for Box<dyn ApplicationFeature<T>> {
    fn clone(&self) -> Self {
        self.clone_box()
    }
}
