use anyhow::Result;
use clap::Parser;
use io_gmail::v1::rest::settings::{
get_vacation::GmailVacationGet, update_vacation::GmailVacationUpdate,
};
use pimalaya_cli::printer::{Message, Printer};
use crate::gmail::{client::GmailClient, settings::convert::enabled_flag};
#[derive(Debug, Parser)]
pub struct GmailSettingsVacationSetCommand {
#[arg(long, conflicts_with = "disable")]
pub enable: bool,
#[arg(long)]
pub disable: bool,
#[arg(long)]
pub subject: Option<String>,
#[arg(long)]
pub body: Option<String>,
#[arg(long)]
pub html: Option<String>,
#[arg(long)]
pub restrict_to_contacts: Option<bool>,
#[arg(long)]
pub restrict_to_domain: Option<bool>,
#[arg(long, value_name = "EPOCH_MS")]
pub start_time: Option<String>,
#[arg(long, value_name = "EPOCH_MS")]
pub end_time: Option<String>,
}
impl GmailSettingsVacationSetCommand {
pub fn execute(self, printer: &mut impl Printer, client: &mut GmailClient) -> Result<()> {
let mut settings = {
let c = GmailVacationGet::new(&client.auth, &client.user_id)?;
client.run(c)?.response
};
if let Some(enabled) = enabled_flag(self.enable, self.disable) {
settings.enable_auto_reply = enabled;
}
if let Some(subject) = self.subject {
settings.response_subject = Some(subject);
}
if let Some(body) = self.body {
settings.response_body_plain_text = Some(body);
}
if let Some(html) = self.html {
settings.response_body_html = Some(html);
}
if let Some(restrict) = self.restrict_to_contacts {
settings.restrict_to_contacts = Some(restrict);
}
if let Some(restrict) = self.restrict_to_domain {
settings.restrict_to_domain = Some(restrict);
}
if let Some(start) = self.start_time {
settings.start_time = Some(start);
}
if let Some(end) = self.end_time {
settings.end_time = Some(end);
}
let _out = {
let c = GmailVacationUpdate::new(&client.auth, &client.user_id, settings)?;
client.run(c)?
};
printer.out(Message::new("Gmail vacation settings successfully updated"))
}
}