import Main from "@/src/components/pages/admin/settings";
import { NSQLITE_TURBOCI_ADMIN_USERS } from "@/src/db/types";
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 AdminSettings() {
    return (
        <Layout>
            <Main />
        </Layout>
    );
}

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

            const users_res = await NSQLite.select<NSQLITE_TURBOCI_ADMIN_USERS>(
                {
                    table: "users",
                    targetId: user.id,
                },
            );

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

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