use clap::{Parser, ValueEnum};
use serde_json::json;
use super::{CmdResult, Ctx};
use crate::envelope::Envelope;
use taskfast_client::api::types::{ListOpenTasksAssignmentType, ListOpenTasksStatus};
use taskfast_client::map_api_error;
#[derive(Debug, Parser)]
pub struct Args {
#[arg(long)]
pub status: Option<DiscoverStatus>,
#[arg(long)]
pub assignment_type: Option<DiscoverAssignmentType>,
#[arg(long = "capability", value_name = "CAP")]
pub capabilities: Vec<String>,
#[arg(long)]
pub budget_max: Option<f64>,
#[arg(long)]
pub budget_min: Option<f64>,
#[arg(long)]
pub cursor: Option<String>,
#[arg(long, default_value_t = 50)]
pub limit: i64,
}
#[derive(Debug, Clone, Copy, ValueEnum)]
pub enum DiscoverStatus {
Open,
Bidding,
}
impl From<DiscoverStatus> for ListOpenTasksStatus {
fn from(s: DiscoverStatus) -> Self {
match s {
DiscoverStatus::Open => Self::Open,
DiscoverStatus::Bidding => Self::Bidding,
}
}
}
#[derive(Debug, Clone, Copy, ValueEnum)]
pub enum DiscoverAssignmentType {
Open,
Direct,
}
impl From<DiscoverAssignmentType> for ListOpenTasksAssignmentType {
fn from(a: DiscoverAssignmentType) -> Self {
match a {
DiscoverAssignmentType::Open => Self::Open,
DiscoverAssignmentType::Direct => Self::Direct,
}
}
}
pub async fn run(ctx: &Ctx, args: Args) -> CmdResult {
let client = ctx.client()?;
let capabilities_vec: Option<Vec<String>> = if args.capabilities.is_empty() {
None
} else {
Some(args.capabilities.clone())
};
let resp = match client
.inner()
.list_open_tasks(
args.assignment_type.map(Into::into),
args.budget_max,
args.budget_min,
capabilities_vec.as_ref(),
args.cursor.as_deref(),
taskfast_client::page_limit(args.limit),
args.status.map(Into::into),
)
.await
{
Ok(v) => v.into_inner(),
Err(e) => return Err(map_api_error(e).await.into()),
};
Ok(Envelope::success(
ctx.environment,
ctx.dry_run,
json!({
"tasks": resp.data,
"meta": resp.meta,
}),
))
}