use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;
use serde::{Deserialize, Serialize};
use chrono::{DateTime, Utc};
use uuid::Uuid;
use crate::{Result, EconomyError};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Account {
pub id: String,
pub agent_id: String,
pub created_at: DateTime<Utc>,
pub status: AccountStatus,
pub metadata: HashMap<String, String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum AccountStatus {
Active,
Suspended,
Closed,
}
pub struct AccountManager {
accounts: Arc<RwLock<HashMap<String, Account>>>,
}
impl AccountManager {
pub fn new() -> Self {
Self {
accounts: Arc::new(RwLock::new(HashMap::new())),
}
}
pub async fn create_account(&mut self, agent_id: String) -> Result<Account> {
let account = Account {
id: Uuid::new_v4().to_string(),
agent_id,
created_at: Utc::now(),
status: AccountStatus::Active,
metadata: HashMap::new(),
};
self.accounts.write().await.insert(account.id.clone(), account.clone());
Ok(account)
}
pub async fn get_account(&self, account_id: &str) -> Result<Account> {
self.accounts
.read()
.await
.get(account_id)
.cloned()
.ok_or_else(|| EconomyError::AccountNotFound(account_id.to_string()))
}
pub async fn get_account_count(&self) -> Result<u64> {
Ok(self.accounts.read().await.len() as u64)
}
}
impl Default for AccountManager {
fn default() -> Self {
Self::new()
}
}