use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
use serde_json;

#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "namespaces")]
pub struct Model {
    #[sea_orm(primary_key, column_type = "Text", auto_increment = false)]
    pub id: String,
    #[sea_orm(unique, column_type = "Text")]
    pub name: String,
    pub owner: String,
    pub owner_ref: Option<String>,
    pub labels: Option<Json>,
    pub created_by: String,
    pub updated_at: DateTimeWithTimeZone,
    pub created_at: DateTimeWithTimeZone,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {}

impl Model {
    // Create a new secret with encrypted value
    pub fn new(
        id: String,
        name: String,
        owner: String,
        created_by: String,
        labels: Option<Json>,
    ) -> Result<Self, String> {
        let now = chrono::Utc::now().into();

        Ok(Self {
            id,
            name: name.clone(),
            owner,
            owner_ref: None,
            labels,
            created_by,
            updated_at: now,
            created_at: now,
        })
    }

    pub fn to_v1(&self) -> crate::resources::v1::namespaces::models::V1Namespace {
        crate::resources::v1::namespaces::models::V1Namespace {
            kind: "Namespace".to_string(),
            metadata: crate::models::V1ResourceMeta {
                id: self.id.clone(),
                name: self.name.clone(),
                namespace: "".to_string(),
                labels: self
                    .labels
                    .as_ref()
                    .map(|json| serde_json::from_value(json.clone()).unwrap_or_default()),
                owner: self.owner.clone(),
                owner_ref: self.owner_ref.clone(),
                created_by: self.created_by.clone(),
                created_at: self.created_at.timestamp(),
                updated_at: self.updated_at.timestamp(),
            },
        }
    }
}
