use crate::{CalckeyDbError, CalckeyModel};
use ck::{poll, poll_vote};
use sea_orm::{ColumnTrait, EntityTrait, QueryFilter};

pub struct PollResolver(CalckeyModel);

impl PollResolver {
    pub fn new(db: CalckeyModel) -> Self {
        PollResolver(db)
    }

    pub async fn get_poll(&self, note_id: &str) -> Result<Option<poll::Model>, CalckeyDbError> {
        Ok(poll::Entity::find()
            .filter(poll::Column::NoteId.eq(note_id))
            .one(self.0.inner())
            .await?)
    }

    pub async fn get_poll_votes_by(
        &self,
        note_id: &str,
        user_id: &str,
    ) -> Result<Vec<poll_vote::Model>, CalckeyDbError> {
        Ok(poll_vote::Entity::find()
            .filter(
                poll_vote::Column::NoteId
                    .eq(note_id)
                    .and(poll_vote::Column::UserId.eq(user_id)),
            )
            .all(self.0.inner())
            .await?)
    }
}
