use inquire::Text;

use crate::{
    commands::edit::{map_empty, obligatory},
    entities::Address,
};

pub(crate) fn edit_address(address: &mut Address) -> crate::database::Result<bool> {
    let orig = address.clone();

    println!();
    println!("Please enter name & address...");
    println!();
    address.name = Text::new("Name?")
        .with_validator(obligatory)
        .with_default(&orig.name)
        .prompt()?;
    address.street = Text::new("Street (incl. number)?")
        .with_validator(obligatory)
        .with_default(&orig.street)
        .prompt()?;
    address.zip = Text::new("Postal Code (ZIP)?")
        .with_validator(obligatory)
        .with_default(&orig.zip)
        .prompt()?;
    address.city = Text::new("City?")
        .with_default(&orig.city)
        .with_validator(obligatory)
        .prompt()?;
    address.country = map_empty(
        Text::new("Country?")
            .with_default(&orig.country.clone().unwrap_or("-".into()))
            .with_help_message("enter '-' to remove")
            .prompt()?,
    );

    Ok(orig != *address)
}
