// Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2019-Present Datadog, Inc.
use serde::de::{Error, MapAccess, Visitor};
use serde::{Deserialize, Deserializer, Serialize};
use serde_with::skip_serializing_none;
use std::fmt::{self, Formatter};

/// Attributes of a permission.
#[non_exhaustive]
#[skip_serializing_none]
#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct PermissionAttributes {
    /// Creation time of the permission.
    #[serde(rename = "created")]
    pub created: Option<chrono::DateTime<chrono::Utc>>,
    /// Description of the permission.
    #[serde(rename = "description")]
    pub description: Option<String>,
    /// Displayed name for the permission.
    #[serde(rename = "display_name")]
    pub display_name: Option<String>,
    /// Display type.
    #[serde(rename = "display_type")]
    pub display_type: Option<String>,
    /// Name of the permission group.
    #[serde(rename = "group_name")]
    pub group_name: Option<String>,
    /// Name of the permission.
    #[serde(rename = "name")]
    pub name: Option<String>,
    /// List of alias names for the permission.
    #[serde(rename = "name_aliases")]
    pub name_aliases: Option<Vec<String>>,
    /// Whether or not the permission is restricted.
    #[serde(rename = "restricted")]
    pub restricted: Option<bool>,
    #[serde(flatten)]
    pub additional_properties: std::collections::BTreeMap<String, serde_json::Value>,
    #[serde(skip)]
    #[serde(default)]
    pub(crate) _unparsed: bool,
}

impl PermissionAttributes {
    pub fn new() -> PermissionAttributes {
        PermissionAttributes {
            created: None,
            description: None,
            display_name: None,
            display_type: None,
            group_name: None,
            name: None,
            name_aliases: None,
            restricted: None,
            additional_properties: std::collections::BTreeMap::new(),
            _unparsed: false,
        }
    }

    pub fn created(mut self, value: chrono::DateTime<chrono::Utc>) -> Self {
        self.created = Some(value);
        self
    }

    pub fn description(mut self, value: String) -> Self {
        self.description = Some(value);
        self
    }

    pub fn display_name(mut self, value: String) -> Self {
        self.display_name = Some(value);
        self
    }

    pub fn display_type(mut self, value: String) -> Self {
        self.display_type = Some(value);
        self
    }

    pub fn group_name(mut self, value: String) -> Self {
        self.group_name = Some(value);
        self
    }

    pub fn name(mut self, value: String) -> Self {
        self.name = Some(value);
        self
    }

    pub fn name_aliases(mut self, value: Vec<String>) -> Self {
        self.name_aliases = Some(value);
        self
    }

    pub fn restricted(mut self, value: bool) -> Self {
        self.restricted = Some(value);
        self
    }

    pub fn additional_properties(
        mut self,
        value: std::collections::BTreeMap<String, serde_json::Value>,
    ) -> Self {
        self.additional_properties = value;
        self
    }
}

impl Default for PermissionAttributes {
    fn default() -> Self {
        Self::new()
    }
}

impl<'de> Deserialize<'de> for PermissionAttributes {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        struct PermissionAttributesVisitor;
        impl<'a> Visitor<'a> for PermissionAttributesVisitor {
            type Value = PermissionAttributes;

            fn expecting(&self, f: &mut Formatter<'_>) -> fmt::Result {
                f.write_str("a mapping")
            }

            fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
            where
                M: MapAccess<'a>,
            {
                let mut created: Option<chrono::DateTime<chrono::Utc>> = None;
                let mut description: Option<String> = None;
                let mut display_name: Option<String> = None;
                let mut display_type: Option<String> = None;
                let mut group_name: Option<String> = None;
                let mut name: Option<String> = None;
                let mut name_aliases: Option<Vec<String>> = None;
                let mut restricted: Option<bool> = None;
                let mut additional_properties: std::collections::BTreeMap<
                    String,
                    serde_json::Value,
                > = std::collections::BTreeMap::new();
                let mut _unparsed = false;

                while let Some((k, v)) = map.next_entry::<String, serde_json::Value>()? {
                    match k.as_str() {
                        "created" => {
                            if v.is_null() {
                                continue;
                            }
                            created = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
                        }
                        "description" => {
                            if v.is_null() {
                                continue;
                            }
                            description =
                                Some(serde_json::from_value(v).map_err(M::Error::custom)?);
                        }
                        "display_name" => {
                            if v.is_null() {
                                continue;
                            }
                            display_name =
                                Some(serde_json::from_value(v).map_err(M::Error::custom)?);
                        }
                        "display_type" => {
                            if v.is_null() {
                                continue;
                            }
                            display_type =
                                Some(serde_json::from_value(v).map_err(M::Error::custom)?);
                        }
                        "group_name" => {
                            if v.is_null() {
                                continue;
                            }
                            group_name = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
                        }
                        "name" => {
                            if v.is_null() {
                                continue;
                            }
                            name = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
                        }
                        "name_aliases" => {
                            if v.is_null() {
                                continue;
                            }
                            name_aliases =
                                Some(serde_json::from_value(v).map_err(M::Error::custom)?);
                        }
                        "restricted" => {
                            if v.is_null() {
                                continue;
                            }
                            restricted = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
                        }
                        &_ => {
                            if let Ok(value) = serde_json::from_value(v.clone()) {
                                additional_properties.insert(k, value);
                            }
                        }
                    }
                }

                let content = PermissionAttributes {
                    created,
                    description,
                    display_name,
                    display_type,
                    group_name,
                    name,
                    name_aliases,
                    restricted,
                    additional_properties,
                    _unparsed,
                };

                Ok(content)
            }
        }

        deserializer.deserialize_any(PermissionAttributesVisitor)
    }
}
