import Main from "@/src/components/pages/admin/users/deployment-user";
import { NSQLITE_TURBOCI_ADMIN_USERS } from "@/src/db/types";
import { _n } from "@/src/exports/client-exports";
import defaultAdminProps from "@/src/functions/pages/admin/default-admin-props";
import Layout from "@/src/layouts/admin";
import NSQLite from "@moduletrace/nsqlite";
import { GetServerSideProps } from "next";

export default function AdminSingleDeploymentUser() {
    return (
        <Layout>
            <Main />
        </Layout>
    );
}

export const getServerSideProps: GetServerSideProps = async (ctx) => {
    return await defaultAdminProps({
        ctx,
        async propsFn({ user, props, query }) {
            if (!user.super_admin) {
                return `/admin`;
            }

            const users_res = await NSQLite.select<NSQLITE_TURBOCI_ADMIN_USERS>(
                {
                    table: "users",
                    targetId: _n(query?.deployment_user_id),
                },
            );

            if (!users_res.singleRes?.id) {
                return `/admin/users`;
            }

            return {
                deployment_user: users_res.singleRes,
            };
        },
    });
};
