use anyhow::{Result, anyhow};
use bc_envelope::prelude::*;
use clap::Args;

use crate::{EnvelopeArgs, EnvelopeArgsLike};

/// Retrieve the assertion at the given index.
#[derive(Debug, Args)]
#[group(skip)]
pub struct CommandArgs {
    /// The index of the assertion to retrieve.
    index: usize,

    #[command(flatten)]
    envelope_args: EnvelopeArgs,
}

impl EnvelopeArgsLike for CommandArgs {
    fn envelope(&self) -> Option<&str> { self.envelope_args.envelope() }
}

impl crate::Exec for CommandArgs {
    fn exec(&self) -> Result<String> {
        let envelope = self.read_envelope()?;
        let assertions = envelope.assertions();
        let assertion = assertions
            .get(self.index)
            .ok_or_else(|| anyhow!("Index out of bounds"))?;
        Ok(assertion.ur_string())
    }
}
