import Main from "@/src/components/pages/auth/login";
import Layout from "@/src/layouts/login";
import userAuth from "@/src/utils/user-auth";
import NSQLite from "@moduletrace/nsqlite";
import { GetServerSideProps } from "next";

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

export const getServerSideProps: GetServerSideProps = async (ctx) => {
    const users = await NSQLite.select({ table: "users" });

    if (!users.payload?.[0]) {
        return {
            redirect: {
                destination: `/auth/signup`,
                statusCode: 307,
            },
        };
    }

    const { singleRes: user } = await userAuth({ req: ctx.req });

    if (user?.logged_in_status) {
        return {
            redirect: {
                destination: `/admin`,
                statusCode: 307,
            },
        };
    }

    return {
        props: {},
    };
};
