use serenity::all::{
  CommandInteraction,
  Context,
  CreateInteractionResponse,
  CreateInteractionResponseMessage,
  CreateMessage,
  EditInteractionResponse,
  InteractionResponseFlags,
  Message,
  ModalInteraction,
  UserId,
};

pub async fn process_message(ctx: &Context, command: &CommandInteraction, private: bool) {
  create_message_response(ctx, command, "Processing...".to_string(), private).await;
}

pub async fn user_dm(ctx: &Context, user_id: &UserId, content: String) -> Option<Message> {
  let data = CreateMessage::new().content(content.to_owned());
  match user_id.dm(ctx, data).await {
    Ok(message) => Some(message),
    Err(err) => {
      log::error!("Failed to create direct message for {content}\n{err}");
      None
    }
  }
}

pub async fn create_message_response(
  ctx: &Context,
  command: &CommandInteraction,
  content: String,
  private: bool,
) {
  let mut data = CreateInteractionResponseMessage::new().content(content.to_owned());
  if private {
    data = data.flags(InteractionResponseFlags::EPHEMERAL);
  }
  let builder = CreateInteractionResponse::Message(data);
  match command.create_response(&ctx.http, builder).await {
    Ok(_) => {}
    Err(err) => {
      log::error!("Failed to create message response for {content}\n{err}");
    }
  };
}

pub async fn create_modal_response(ctx: &Context, modal: &ModalInteraction) {
  let data = CreateInteractionResponseMessage::new();
  let builder = CreateInteractionResponse::Message(data);
  match modal.create_response(&ctx.http, builder).await {
    Ok(_) => {}
    Err(err) => {
      log::error!("Failed to create modal response\n{err}");
    }
  }
}

pub async fn edit_response(ctx: &Context, command: &CommandInteraction, content: String) {
  let builder = EditInteractionResponse::new().content(content.to_owned());
  match command.edit_response(&ctx.http, builder).await {
    Ok(_) => {}
    Err(err) => {
      log::error!("Failed to create response for {content}\n{err}");
    }
  }
}
