use askama::Template;
use axum::{
    extract::State,
    response::{Html, IntoResponse},
};
use interim_models::workspace_user_perm::WorkspaceUserPerm;

use crate::{
    app::AppDbConn, errors::AppError, navigator::Navigator, settings::Settings, user::CurrentUser,
};

pub(super) async fn get(
    State(settings): State<Settings>,
    CurrentUser(user): CurrentUser,
    navigator: Navigator,
    AppDbConn(mut app_db): AppDbConn,
) -> Result<impl IntoResponse, AppError> {
    let workspace_perms = WorkspaceUserPerm::belonging_to_user(user.id)
        .fetch_all(&mut app_db)
        .await?;

    #[derive(Template)]
    #[template(path = "workspaces_multi/list.html")]
    struct ResponseTemplate {
        navigator: Navigator,
        settings: Settings,
        workspace_perms: Vec<WorkspaceUserPerm>,
    }

    Ok(Html(
        ResponseTemplate {
            navigator,
            settings,
            workspace_perms,
        }
        .render()?,
    ))
}
