use askama::Template;
use axum::{
    debug_handler,
    extract::{Path, State},
    response::{Html, IntoResponse},
};
use interim_models::{
    portal::Portal,
    workspace::Workspace,
    workspace_user_perm::{self, WorkspaceUserPerm},
};
use interim_pgtypes::pg_class::PgClass;
use serde::Deserialize;
use sqlx::postgres::types::Oid;
use uuid::Uuid;

use crate::{
    app::{App, AppDbConn},
    errors::{AppError, forbidden},
    navigator::{Navigator, NavigatorPage as _},
    settings::Settings,
    user::CurrentUser,
    workspace_nav::{NavLocation, RelLocation, WorkspaceNav},
    workspace_pooler::{RoleAssignment, WorkspacePooler},
};

#[derive(Debug, Deserialize)]
pub(super) struct PathParams {
    portal_id: Uuid,
    rel_oid: u32,
    workspace_id: Uuid,
}

/// HTTP GET handler for portal settings, including renaming and deletion.
#[debug_handler(state = App)]
pub(super) async fn get(
    State(settings): State<Settings>,
    CurrentUser(user): CurrentUser,
    AppDbConn(mut app_db): AppDbConn,
    Path(PathParams {
        portal_id,
        rel_oid,
        workspace_id,
    }): Path<PathParams>,
    navigator: Navigator,
    State(mut pooler): State<WorkspacePooler>,
) -> Result<impl IntoResponse, AppError> {
    // Check workspace authorization.
    let workspace_perms = WorkspaceUserPerm::belonging_to_user(user.id)
        .fetch_all(&mut app_db)
        .await?;
    if workspace_perms.iter().all(|p| {
        p.workspace_id != workspace_id || p.perm != workspace_user_perm::PermissionValue::Connect
    }) {
        return Err(forbidden!("access denied to workspace"));
    }
    // FIXME ensure workspace corresponds to rel/portal, and that user has
    // permission to access/alter both as needed.

    let workspace = Workspace::with_id(workspace_id)
        .fetch_one(&mut app_db)
        .await?;

    let mut workspace_client = pooler
        .acquire_for(workspace.id, RoleAssignment::User(user.id))
        .await?;

    let rel = PgClass::with_oid(Oid(rel_oid))
        .fetch_one(&mut workspace_client)
        .await?;

    let portal = Portal::with_id(portal_id).fetch_one(&mut app_db).await?;

    #[derive(Debug, Template)]
    #[template(path = "relations_single/portal_settings.html")]
    struct ResponseTemplate {
        navigator: Navigator,
        portal: Portal,
        rel: PgClass,
        settings: Settings,
        workspace_nav: WorkspaceNav,
    }
    Ok(Html(
        ResponseTemplate {
            workspace_nav: WorkspaceNav::builder()
                .navigator(navigator.clone())
                .workspace(workspace)
                .populate_rels(&mut app_db, &mut workspace_client)
                .await?
                .current(NavLocation::Rel(Oid(rel_oid), Some(RelLocation::Sharing)))
                .build()?,
            navigator,
            portal,
            rel,
            settings,
        }
        .render()?,
    ))
}
