---
to: "{{ project_name }}/src/cmds/{{ noun }}/{{ verb }}.rs"
vars:
  noun: "resource"
  verb: "list"
  noun_description: "resource"
---
//! {{ verb | capitalize }} {{ noun_description }}s command

use clap::Args;

type Result<T> = std::result::Result<T, anyhow::Error>;

#[derive(Debug, Args)]
pub struct {{ verb | capitalize }}Args {
    /// Filter by name pattern
    #[arg(short, long)]
    pub filter: Option<String>,

    /// Maximum number of items to display
    #[arg(short, long)]
    pub limit: Option<usize>,

    /// Output format (json, yaml, table)
    #[arg(short = 'f', long, default_value = "table")]
    pub format: String,

    /// Show all fields
    #[arg(short, long)]
    pub all: bool,
}

pub async fn run(args: &{{ verb | capitalize }}Args) -> Result<()> {
    println!("📋 Listing {{ noun_description }}s...");

    // TODO: Implement {{ verb }} logic here
    let items = vec![
        ("{{ noun }}-1", "First {{ noun_description }}"),
        ("{{ noun }}-2", "Second {{ noun_description }}"),
        ("{{ noun }}-3", "Third {{ noun_description }}"),
    ];

    let filtered_items: Vec<_> = if let Some(filter) = &args.filter {
        items.into_iter()
            .filter(|(name, _)| name.contains(filter))
            .collect()
    } else {
        items.into_iter().collect()
    };

    let limited_items = if let Some(limit) = args.limit {
        &filtered_items[..limit.min(filtered_items.len())]
    } else {
        &filtered_items[..]
    };

    match args.format.as_str() {
        "json" => {
            let json_items: Vec<_> = limited_items
                .iter()
                .map(|(name, desc)| serde_json::json!({ "name": name, "description": desc }))
                .collect();
            println!("{}", serde_json::to_string_pretty(&json_items)?);
        }
        "table" => {
            println!("\n{:<20} {:<40}", "NAME", "DESCRIPTION");
            println!("{}", "-".repeat(60));
            for (name, desc) in limited_items {
                println!("{:<20} {:<40}", name, desc);
            }
            println!("\nTotal: {} {{ noun_description }}(s)", limited_items.len());
        }
        _ => {
            for (name, desc) in limited_items {
                println!("{}: {}", name, desc);
            }
        }
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_{{ verb }}_basic() {
        let args = {{ verb | capitalize }}Args {
            filter: None,
            limit: None,
            format: "table".to_string(),
            all: false,
        };

        let result = run(&args).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_{{ verb }}_with_filter() {
        let args = {{ verb | capitalize }}Args {
            filter: Some("{{ noun }}-1".to_string()),
            limit: None,
            format: "table".to_string(),
            all: false,
        };

        let result = run(&args).await;
        assert!(result.is_ok());
    }
}
