use poise::{serenity_prelude::CreateEmbed, CreateReply};
use serde::{Deserialize, Serialize};

use crate::{
    consts::THEME_COLOR,
    models::CtxData,
    utils::{Extract, Recordable},
    Context, Error,
};

#[derive(Serialize, Deserialize, Extract)]
pub struct Options;

impl Recordable for Options {
    async fn run(self, ctx: Context<'_>) -> Result<(), Error> {
        let mut user_data = ctx.author_data().await?;
        user_data.allowed_dm = true;
        user_data.commit_changes(&ctx.data().database).await;

        ctx.send(
            CreateReply::default().ephemeral(true).embed(
                CreateEmbed::new()
                    .title("DMs permitted")
                    .description("You will receive a message if a user sets a DM reminder for you.")
                    .color(*THEME_COLOR),
            ),
        )
        .await?;

        Ok(())
    }
}

/// Allow other users to set reminders in your direct messages
#[poise::command(slash_command, rename = "allow", identifying_name = "set_allowed_dm")]
#[cfg(not(test))]
pub async fn set(ctx: Context<'_>) -> Result<(), Error> {
    (Options {}).run(ctx).await
}
