use clap::{Args, Parser, Subcommand, ValueEnum};
use serde_json::json;
use uuid::Uuid;
use super::{CmdError, CmdResult, Ctx};
use crate::envelope::Envelope;
use taskfast_client::api::types::{
BidRequest, GetAgentBidsStatusItem, RejectBidBody, RejectBidBodyReason,
};
use taskfast_client::map_api_error;
use taskfast_client::TaskFastClient;
#[derive(Debug, Subcommand)]
pub enum Command {
List(ListArgs),
Create(CreateArgs),
Cancel(CancelArgs),
Accept(AcceptArgs),
Reject(RejectArgs),
}
#[derive(Debug, Parser)]
pub struct ListArgs {
#[arg(long)]
pub cursor: Option<String>,
#[arg(long, default_value_t = 20)]
pub limit: i64,
#[arg(long)]
pub status: Option<BidStatusFilter>,
}
#[derive(Debug, Clone, Copy, ValueEnum, PartialEq, Eq)]
pub enum BidStatusFilter {
Pending,
AcceptedPendingEscrow,
Accepted,
Rejected,
Withdrawn,
}
impl From<BidStatusFilter> for GetAgentBidsStatusItem {
fn from(f: BidStatusFilter) -> Self {
match f {
BidStatusFilter::Pending => Self::Pending,
BidStatusFilter::AcceptedPendingEscrow => Self::AcceptedPendingEscrow,
BidStatusFilter::Accepted => Self::Accepted,
BidStatusFilter::Rejected => Self::Rejected,
BidStatusFilter::Withdrawn => Self::Withdrawn,
}
}
}
#[derive(Debug, Parser)]
pub struct CreateArgs {
pub task_id: String,
#[arg(long)]
pub price: String,
#[arg(long)]
pub pitch: Option<String>,
}
#[derive(Debug, Parser)]
pub struct CancelArgs {
pub id: String,
}
#[derive(Debug, Args)]
pub struct AcceptArgs {
pub id: String,
}
#[derive(Debug, Args)]
pub struct RejectArgs {
pub id: String,
#[arg(long)]
pub reason: Option<String>,
}
pub async fn run(ctx: &Ctx, cmd: Command) -> CmdResult {
match cmd {
Command::List(args) => list(ctx, args).await,
Command::Create(args) => create(ctx, args).await,
Command::Cancel(args) => cancel(ctx, args).await,
Command::Accept(args) => accept(ctx, args).await,
Command::Reject(args) => reject(ctx, args).await,
}
}
async fn list(ctx: &Ctx, args: ListArgs) -> CmdResult {
let client = ctx.client()?;
let data = list_bids(&client, &args).await?;
Ok(Envelope::success(ctx.environment, ctx.dry_run, data))
}
async fn list_bids(
client: &TaskFastClient,
args: &ListArgs,
) -> Result<serde_json::Value, CmdError> {
let status = args.status.map(|f| vec![GetAgentBidsStatusItem::from(f)]);
let resp = match client
.inner()
.get_agent_bids(
None,
args.cursor.as_deref(),
taskfast_client::page_limit(args.limit),
status.as_ref(),
)
.await
{
Ok(v) => v.into_inner(),
Err(e) => return Err(map_api_error(e).await.into()),
};
Ok(json!({
"bids": resp.data,
"meta": resp.meta,
}))
}
async fn create(ctx: &Ctx, args: CreateArgs) -> CmdResult {
let task_id = Uuid::parse_str(&args.task_id)
.map_err(|e| CmdError::Usage(format!("task id must be a UUID: {e}")))?;
if args.price.trim().is_empty() {
return Err(CmdError::Usage("--price must not be empty".into()));
}
if ctx.dry_run {
return Ok(Envelope::success(
ctx.environment,
ctx.dry_run,
json!({
"action": "would_create_bid",
"task_id": task_id.to_string(),
"price": args.price,
"pitch": args.pitch,
}),
));
}
let client = ctx.client()?;
let body = BidRequest {
price: args.price.clone(),
pitch: args.pitch.clone(),
};
let bid = match client.inner().create_bid(&task_id, &body).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!({ "bid": bid }),
))
}
async fn cancel(ctx: &Ctx, args: CancelArgs) -> CmdResult {
let bid_id = Uuid::parse_str(&args.id)
.map_err(|e| CmdError::Usage(format!("bid id must be a UUID: {e}")))?;
if ctx.dry_run {
return Ok(Envelope::success(
ctx.environment,
ctx.dry_run,
json!({
"action": "would_cancel_bid",
"bid_id": bid_id.to_string(),
}),
));
}
let client = ctx.client()?;
let bid = match client.inner().withdraw_bid(&bid_id).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!({ "bid": bid }),
))
}
async fn accept(ctx: &Ctx, args: AcceptArgs) -> CmdResult {
let bid_id = Uuid::parse_str(&args.id)
.map_err(|e| CmdError::Usage(format!("bid id must be a UUID: {e}")))?;
if ctx.dry_run {
return Ok(Envelope::success(
ctx.environment,
ctx.dry_run,
json!({
"action": "would_accept_bid",
"bid_id": bid_id.to_string(),
}),
));
}
let client = ctx.client()?;
let resp = match client.inner().accept_bid(&bid_id).await {
Ok(v) => v.into_inner(),
Err(e) => return Err(map_api_error(e).await.into()),
};
let next_action = resp.next_action;
let next_action_command = resp.next_action_command.clone();
let poster_signature_deadline = resp.poster_signature_deadline;
let signing_url = resp.signing_url.clone();
Ok(Envelope::success(
ctx.environment,
ctx.dry_run,
json!({
"bid": resp,
"next_action": next_action,
"next_action_command": next_action_command,
"poster_signature_deadline": poster_signature_deadline,
"signing_url": signing_url,
}),
))
}
async fn reject(ctx: &Ctx, args: RejectArgs) -> CmdResult {
let bid_id = Uuid::parse_str(&args.id)
.map_err(|e| CmdError::Usage(format!("bid id must be a UUID: {e}")))?;
let reason_field = match args.reason.as_deref() {
Some(s) if s.trim().is_empty() => {
return Err(CmdError::Usage(
"--reason must not be empty when passed".into(),
));
}
Some(s) => Some(
RejectBidBodyReason::try_from(s)
.map_err(|e| CmdError::Usage(format!("--reason invalid: {e}")))?,
),
None => None,
};
if ctx.dry_run {
return Ok(Envelope::success(
ctx.environment,
ctx.dry_run,
json!({
"action": "would_reject_bid",
"bid_id": bid_id.to_string(),
"reason": args.reason,
}),
));
}
let client = ctx.client()?;
let body = RejectBidBody {
reason: reason_field,
};
let resp = match client.inner().reject_bid(&bid_id, &body).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!({ "bid": resp }),
))
}