use diesel::{
    dsl::{auto_type, AsSelect},
    pg::Pg,
    prelude::*,
};
use uuid::Uuid;

use crate::schema::channel_selections;

#[derive(Associations, Clone, Debug, Identifiable, Queryable, Selectable)]
#[diesel(belongs_to(crate::channels::Channel))]
#[diesel(belongs_to(crate::projects::Project))]
#[diesel(primary_key(channel_id, project_id))]
#[diesel(check_for_backend(Pg))]
pub struct ChannelSelection {
    pub project_id: Uuid,
    pub channel_id: Uuid,
}

impl ChannelSelection {
    #[auto_type(no_type_alias)]
    pub fn all() -> _ {
        let select: AsSelect<ChannelSelection, Pg> = Self::as_select();
        channel_selections::table.select(select)
    }

    #[auto_type(no_type_alias)]
    pub fn with_channel<'a>(channel_id: &'a Uuid) -> _ {
        channel_selections::channel_id.eq(channel_id)
    }

    #[auto_type(no_type_alias)]
    pub fn with_project<'a>(project_id: &'a Uuid) -> _ {
        channel_selections::project_id.eq(project_id)
    }
}
