use super::super::topic::topic::TopicValue;
use super::user::{User, UserValue};
use crate::identify_temporalize;
use juniper::{graphql_interface, graphql_object};

use super::super::database::Database;

identify_temporalize!(
    pub struct Creator {
        name: String,
        topic_ids: Vec<String>,
    }
);

impl Creator {
    pub fn new(id: &str, name: &str, topic_ids: &[&str]) -> Self {
        Self {
            model_name: "User",
            model_alias: "creator",
            id: id.to_owned(),
            created: "".to_string(),
            updated: "".to_string(),
            name: name.to_owned(),
            topic_ids: topic_ids
                .to_owned()
                .into_iter()
                .map(ToOwned::to_owned)
                .collect(),
        }
    }
}

#[graphql_object(context = Database, impl = UserValue)]
impl Creator {
    fn id(&self) -> &str {
        &self.id
    }
    fn name(&self) -> Option<&str> {
        Some(self.name.as_str())
    }
    fn topics(&self, ctx: &Database) -> Vec<TopicValue> {
        ctx.get_topics(self)
    }
}

#[graphql_interface]
impl User for Creator {
    fn id(&self) -> &str {
        &self.id
    }

    fn name(&self) -> Option<&str> {
        Some(&self.name)
    }

    fn topics(&self, ctx: &Database) -> Vec<TopicValue> {
        ctx.get_topics(self)
    }
    fn topic_ids(&self) -> &[String] {
        &self.topic_ids
    }
}
