'use server'
import { cookies } from "next/headers";

import LoginForm            from "@/components/client/admin/loginForm";
import AdminPanel           from "@/components/client/admin/adminPanel";
import ClientAuthHandler    from "@/components/client/admin/clientAuthHandler";

import { serverValidateSessionCookie } from "@/app/lib/actions";
import { constructAPIUrl } from "@/util/Utils";
import { ReactNode } from "react";
import { AuthContext, AuthProps } from "@/providers/providers";


interface Props {
    children?: ReactNode;
    params?: any;
    requiredRole?: number;
} // We interfacing lads


export default async function AuthHandler(props: Props) {
    const protoKoek = await cookies().get('auth')?.value;
    const koek = decodeURIComponent(protoKoek ? protoKoek: "");
    console.log("koekje:" + koek)
    let p:AuthProps = {
        test:"not pog"
    };
    if(koek){
        const kd = JSON.parse(koek);
        if(kd.id && kd.token && kd.user_id){
            p = {
                test:"success!",
                auth: {
                    id:kd.id,
                    token:kd.token,
                    user_id:kd.user_id
                }
            }
        }
    }

    return (
        <div className="flex flex-row">
            {!(koek && await serverValidateSessionCookie(koek)) ? <LoginForm>{ }</LoginForm> : <ClientAuthHandler authProps={p}><section>{props.children}</section></ClientAuthHandler>}
        </div>
    );
}
