use k8s_openapi::api::apps::v1::Deployment;
use serde::Serialize;
use serde_json::json;

use crate::{
    interpret::Interpret,
    score::Score,
    topology::{OcK8sclient, Topology},
};

use super::resource::{K8sResourceInterpret, K8sResourceScore};

#[derive(Debug, Clone, Serialize)]
pub struct K8sDeploymentScore {
    pub name: String,
    pub image: String,
}

impl<T: Topology + OcK8sclient> Score<T> for K8sDeploymentScore {
    fn create_interpret(&self) -> Box<dyn Interpret<T>> {
        let deployment: Deployment = serde_json::from_value(json!(
            {
                "metadata": {
                    "name": self.name
                },
                "spec": {
                    "selector": {
                        "matchLabels":  {
                            "app": self.name
                        },
                    },
                    "template": {
                        "metadata": {
                            "labels": {
                                "app": self.name
                            },
                        },
                        "spec": {
                            "containers": [
                                {
                                     "image": self.image,
                                      "name": self.image
                                }
                            ]
                        }
                    }
                }
            }
        ))
        .unwrap();
        Box::new(K8sResourceInterpret {
            score: K8sResourceScore::single(deployment.clone()),
        })
    }

    fn name(&self) -> String {
        "K8sDeploymentScore".to_string()
    }
}
