use crate::request::prelude::*;
use twilight_model::id::{GuildId, RoleId};

/// Delete a role in a guild, by id.
pub struct DeleteRole<'a> {
    fut: Option<Pending<'a, ()>>,
    guild_id: GuildId,
    http: &'a Client,
    role_id: RoleId,
    reason: Option<String>,
}

impl<'a> DeleteRole<'a> {
    pub(crate) fn new(http: &'a Client, guild_id: GuildId, role_id: RoleId) -> Self {
        Self {
            fut: None,
            guild_id,
            http,
            role_id,
            reason: None,
        }
    }

    fn start(&mut self) -> Result<()> {
        let request = if let Some(reason) = &self.reason {
            let headers = audit_header(&reason)?;
            Request::from((
                headers,
                Route::DeleteRole {
                    guild_id: self.guild_id.0,
                    role_id: self.role_id.0,
                },
            ))
        } else {
            Request::from(Route::DeleteRole {
                guild_id: self.guild_id.0,
                role_id: self.role_id.0,
            })
        };

        self.fut.replace(Box::pin(self.http.verify(request)));

        Ok(())
    }
}

impl<'a> AuditLogReason for DeleteRole<'a> {
    fn reason(mut self, reason: impl Into<String>) -> Result<Self, AuditLogReasonError> {
        self.reason
            .replace(AuditLogReasonError::validate(reason.into())?);

        Ok(self)
    }
}

poll_req!(DeleteRole<'_>, ());
