use super::super::comment::comment::CommentValue;
use super::super::database::Database;
use super::super::user::user::UserValue;
use super::topic::Topic;
use crate::identify_temporalize;
use juniper::graphql_interface;

identify_temporalize!(
    pub struct Problem {
        name: String,
        commenter_ids: Vec<String>,
        comment_ids: Vec<String>,
    }
);

impl Problem {
    pub fn new(id: &str, name: &str, commenter_ids: &[&str], comment_ids: &[&str]) -> Self {
        Self {
            model_name: "Topic",
            model_alias: "disucssion",
            id: id.to_owned(),
            created: "".to_string(),
            updated: "".to_string(),
            name: name.to_owned(),
            commenter_ids: commenter_ids
                .to_owned()
                .into_iter()
                .map(ToOwned::to_owned)
                .collect(),
            comment_ids: comment_ids
                .to_owned()
                .into_iter()
                .map(ToOwned::to_owned)
                .collect(),
        }
    }
}

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

#[graphql_interface]
impl Topic for Problem {
    fn id(&self) -> &str {
        &self.id
    }
    fn name(&self) -> Option<&str> {
        Some(&self.name)
    }
    fn commenters(&self, ctx: &Database) -> Vec<UserValue> {
        ctx.get_commenters(self)
    }
    fn comments(&self, ctx: &Database) -> Vec<CommentValue> {
        ctx.get_comments(self)
    }
}
