use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use utoipa::ToSchema;
use uuid::Uuid;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToSchema)]
#[serde(rename_all = "snake_case")]
pub enum BillingTarget {
    User,
    Workspace,
}
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct AgentRunRequest {
    pub session_id: Uuid,
    pub conversation_id: Option<Uuid>,
    pub input: String,
    #[serde(default)]
    pub stream: bool,
    pub max_steps: Option<usize>,
    pub timeout_secs: Option<u64>,
}
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct AgentRunResponse {
    pub message_id: Uuid,
    pub conversation_id: Uuid,
    pub output: String,
    pub steps: Vec<AgentStepInfo>,
    pub usage: AgentUsageInfo,
    pub cost: Option<AgentCostInfo>,
}
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct AgentStepInfo {
    pub index: usize,
    pub assistant: Option<String>,
    pub tool_calls: Vec<AgentToolCallInfo>,
    pub reflection: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct AgentToolCallInfo {
    pub id: String,
    pub name: String,
    pub arguments: Value,
    pub output: Option<Value>,
    pub error: Option<String>,
    pub elapsed_ms: Option<i64>,
}
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct AgentUsageInfo {
    pub input_tokens: i64,
    pub output_tokens: i64,
    pub total_tokens: i64,
}
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct AgentCostInfo {
    pub amount: String,
    pub currency: String,
}
#[derive(Debug, Clone)]
pub struct SessionContext {
    pub session_id: Uuid,
    pub user_id: Option<Uuid>,
    pub workspace_id: Option<Uuid>,
    pub system_prompt: String,
    pub model_version_id: Uuid,
    pub provider_model_name: String,
    pub temperature: Option<f32>,
    pub max_output_tokens: Option<i32>,
    pub tool_policy_json: Option<String>,
    pub toolset_json: Option<String>,
    pub variables_json: Option<String>,
    pub iteration_budget: Option<i32>,
    pub memory_provider: Option<String>,
    #[allow(dead_code)]
    pub source: Option<String>,
    #[allow(dead_code)]
    pub parent_session_id: Option<Uuid>,
    pub billing_target: BillingTarget,
}
#[derive(Debug, Clone)]
pub struct BillingRecord {
    pub invocation_id: Uuid,
    pub session_id: Uuid,
    pub model_version_id: Uuid,
    pub input_tokens: i64,
    pub output_tokens: i64,
    pub cached_input_tokens: i64,
    pub cache_read_tokens: i64,
    pub cache_write_tokens: i64,
    pub reasoning_tokens: i64,
    pub total_tokens: i64,
    pub cost: Option<rust_decimal::Decimal>,
    pub currency: Option<String>,
    pub created_at: DateTime<Utc>,
}
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct RunPersistState {
    pub message_id: Uuid,
    pub conversation_id: Uuid,
    pub invocation_id: Uuid,
    pub billing: BillingRecord,
}
