use {super::*, crate::wallet::Wallet};

#[derive(Debug, Parser)]
pub(crate) struct Sats {
  #[arg(
    long,
    help = "Find satoshis listed in first column of tab-separated value file <TSV>."
  )]
  tsv: Option<PathBuf>,
}

#[derive(Serialize, Deserialize)]
pub struct OutputTsv {
  pub sat: String,
  pub output: OutPoint,
}

#[derive(Serialize, Deserialize)]
pub struct OutputRare {
  pub sat: Sat,
  pub output: OutPoint,
  pub offset: u64,
  pub rarity: Rarity,
}

impl Sats {
  pub(crate) fn run(&self, options: Options) -> SubcommandResult {
    let index = Index::open(&options)?;

    if !index.has_sat_index() {
      bail!("sats requires index created with `--index-sats` flag");
    }

    index.update()?;

    let utxos = index.get_unspent_output_ranges(Wallet::load(&options)?)?;

    if let Some(path) = &self.tsv {
      let mut output = Vec::new();
      for (outpoint, sat) in sats_from_tsv(
        utxos,
        &fs::read_to_string(path)
          .with_context(|| format!("I/O error reading `{}`", path.display()))?,