use crate::api::auth::Session;
use crate::error::ApiResult;
use crate::model::space::Channel;
use crate::model::space::{Space, SpaceDto};
use crate::repo::{ChannelRepo, SpaceRepo};
use crate::svc::chat_svc::ChatService;
use rocket::State;
use rocket::serde::json::Json;
use std::sync::Arc;

#[get("/spaces")]
pub async fn list_spaces(space_repo: &State<Arc<dyn SpaceRepo>>) -> ApiResult<Json<Vec<Space>>> {
    let spaces = space_repo.get_all().await?;
    Ok(Json(spaces))
}

#[get("/spaces/<space_id>/channels")]
pub async fn list_channels(
    space_id: i64,
    channel_repo: &State<Arc<dyn ChannelRepo>>,
) -> ApiResult<Json<Vec<Channel>>> {
    let channels = channel_repo.get_by_space_id(space_id).await?;
    Ok(Json(channels))
}

#[get("/accessible_channels")]
pub async fn get_accessible_channels(
    session: Session,
    svc: &State<ChatService>,
) -> ApiResult<Json<Vec<SpaceDto>>> {
    let space = svc.get_accessible_channels(session.uid).await?;
    Ok(Json(space))
}
