use crate::error::ApiResult;
use crate::repo::UserRepo;
use std::sync::Arc;

pub struct UserService {
	repo: Arc<dyn UserRepo>
}

impl UserService {
	pub fn new(repo: Arc<dyn UserRepo>) -> Self {
		Self { repo }
	}

	pub async fn get_display_name(&self, uid: i64) -> ApiResult<String> {
		// TODO: redis caching for display names

		let user = self.repo.get_by_id(uid)
			.await.ok_or(crate::error::AppError::NotFound)?;

		Ok(user.nickname.unwrap_or_else(|| user.username))
	}

	pub async fn get_username(&self, uid: i64) -> ApiResult<String> {
		 self.repo.get_by_id(uid)
			.await.ok_or(crate::error::AppError::NotFound)
			.map(|u| u.username)
	}
}